home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / ATTRIB.CMM < prev    next >
Text File  |  1996-01-30  |  8KB  |  249 lines

  1. /*
  2.  * Attrib.cmm
  3.  *
  4.  * CMM Attrib. This program allows you to view or set file attribute bits.
  5.  *
  6.  * Runs on any version of CEnvi
  7.  */
  8.  
  9. #include "netware.lib"
  10.  
  11. /* ---------------------------------------------------------------------
  12.    The following variables allow us to keep track of which bits the user
  13.    wants us to twiddle.
  14.    --------------------------------------------------------------------- */
  15.  
  16. #define BITS "RHSEDA"
  17.  
  18. flags = 0;          // 1 if user wants to change the values
  19. flagor = 0;         // stores the +A, etc options
  20. flagand = 0;        // stores the -A, etc options
  21.  
  22.  
  23. /* ---------------------------------------------------------------------
  24.    These variables tell us what command line options the user selected
  25.    --------------------------------------------------------------------- */
  26.  
  27. filename = "";      // filename to be worked on - can have wildcards
  28. recursive = 0;      // should we do recursive stuff, like *.cmm in all
  29.                     // subdirectories
  30. help = 0;           // If so, we print the usage display
  31.  
  32. /*
  33.  * A standard usage help display.
  34.  */
  35. usage()
  36. {
  37.   printf("Displays or changes file attributes.\n\n");
  38.   printf("ATTRIB [+ | -] bit ... [[drive:][path]filename] [/S]\n");
  39.   printf("\n  +   Sets an attribute.\n");
  40.   printf("  -   Clears an attribute.\n");
  41.   printf("  R   Read-only file attribute.\n");
  42.   printf("  A   Archive file attribute.\n");
  43.   printf("  S   System file attribute.\n");
  44.   printf("  H   Hidden file attribute.\n");
  45.   printf("  E   Executable file attribute.\n");
  46.   printf("  /S  Processes files in all directories in the specified path.\n");
  47. }
  48.  
  49. /*
  50.  * Examine the command line and set appropriate flags
  51.  */
  52. parse_args(argc,argv)
  53. {
  54.    for( i=1;i<argc;i++ )
  55.    {
  56.       if( argv[i][0]=='/' )
  57.       {
  58.          switch( argv[i][1] )
  59.          {
  60.             case 's': recursive = 1; break;
  61.             case '?': help = 1; break;
  62.             default: usage();
  63.          }
  64.          continue;
  65.       }
  66.  
  67.       if( argv[i][0]=='-' || argv[i][0]=='+' )
  68.       {
  69.          chr = toupper(argv[i][1]); search = "1"; search[0] = chr;
  70.          if( chr=='D' )
  71.          {
  72.             printf("You may not set or clear the directory bit.\n");
  73.             exit(EXIT_FAILURE);
  74.          }
  75.          if( (bit = strcspn(BITS,search))==strlen(BITS) )
  76.          {
  77.             printf("Unrecognized atribute: %c\n",argv[i][1]);
  78.             exit(EXIT_FAILURE);
  79.          }
  80.  
  81.          if( argv[i][0]=='+' )
  82.          {
  83.             flagor |= (1<<bit);
  84.          } else {
  85.             // If you turn on read-only, netware automatically sets these bits. So,
  86.             // if you turn it off, we should remove them
  87.             if( defined(_NWNLM_) && (chr=='R') )
  88.                flagand |= _A_NODELET | _A_NORENAM;
  89.  
  90.             flagand |= (1<<bit);
  91.          }
  92.          flags = 1;
  93.          continue;
  94.       }
  95.       if( strlen(filename)>0 )
  96.       {
  97.          printf("Too many files on command line.\n");
  98.          exit(EXIT_FAILURE);
  99.       }
  100.       filename = argv[i];
  101.    }
  102. }
  103.  
  104.  
  105. /*
  106.  * This is a Netware function. It won't work on the other systems.
  107.  */
  108. DOSTimeFromCalendar(time)
  109. {
  110.    undefine(field1);
  111.    undefine(field2);
  112.    NLMLink("_ConvertTimeToDOS",time,field1,field2);
  113.    return (field2<<16) | field1;
  114. }
  115.  
  116. /*
  117.  * This is a DOS/Windows only function
  118.  */
  119. SetFileAttributes(pFileName,pAttributes)
  120. {
  121.    lReg.ah = 0x43;
  122.    lReg.al = 1;
  123.    lReg.cx = pAttributes;
  124.    if( !defined(_DOS32_) )  {
  125.       lReg.ds = segment(pFileName); lReg.dx = offset(pFileName);
  126.    } else {
  127.       lReg.dx = pointer(pFileName);
  128.    }
  129.    return interrupt(0x21,lReg);
  130. }
  131. /*
  132.  * We process the arguments, then do the work. This should act as an exact
  133.  * clone of the DOS attrib command.
  134.  */
  135. main(argc,argv)
  136. {
  137.    parse_args(argc,argv);
  138.  
  139.    if( help ) usage();
  140.  
  141. // Get an array of the files we are to work with.
  142.  
  143.    if( filename[0]!='\0' )
  144.      {
  145.        files = Directory(filename);
  146.        if( !strcmp(filename,".") || !stricmp(filename,"..") ||
  147.           (files && GetarraySpan(files)==0 && (files[0].attrib & _A_SUBDIR))
  148.           )
  149.          strcat(filename,defined(_NWNLM_)?"/":"\\");
  150.      }
  151.  
  152.    s = strlen(filename);
  153.    if( s && filename[s-1]==':' )
  154.      {
  155.        strcat(filename,defined(_NWNLM_)?"/":"\\"); s++;
  156.      }
  157.    if( s && (filename[s-1]=='/' || filename[s-1]=='\\') )
  158.      strcat(filename,"*.*");
  159.    if( s==0 ) filename = "*.*";
  160.  
  161.    files = Directory(filename,recursive,~_A_SUBDIR);
  162.    if( files==NULL ) {
  163.       printf("File not found - %s\n",filename);
  164.       exit(EXIT_FAILURE);
  165.    }
  166.    number = GetArraySpan(files)+1;
  167.  
  168.  
  169.    // In this case, the user just wants us to list the atributes for the
  170.    // specified filespec.
  171.  
  172.    if( flags==0 )
  173.    {
  174.       for( i=0;i<number;i++ )
  175.         if( defined(_NWNLM_) )
  176.           printf(" %s %s %s %s %s %s %s %s %s %s %s     %s\n",
  177.                  (files[i].attrib & _A_SUBDIR)?"D":"-",
  178.                  (files[i].attrib & _A_RDONLY)?"RO":"RW",
  179.                  (files[i].attrib & _A_SHARE)?"SH":"--",
  180.                  (files[i].attrib & _A_HIDDEN)?"H":"-",
  181.                  (files[i].attrib & _A_SYSTEM)?"SY":"--",
  182.                  (files[i].attrib & _A_TRANS)?"T":"-",
  183.                  (files[i].attrib & _A_IMMPURG)?"P":"-",
  184.                  (files[i].attrib & _A_ARCH)?"A":"-",
  185.                  (files[i].attrib & _A_NOCOPY)?"CI":"--",
  186.                  (files[i].attrib & _A_NODELET)?"DI":"--",
  187.                  (files[i].attrib & _A_NORENAM)?"RI":"--",
  188.                  FullPath(files[i].name));
  189.         else
  190.           printf("  %c%c%c%c%c%c   %s\n",
  191.                  (files[i].attrib & _A_ARCH)?'A':' ',
  192.                  (files[i].attrib & _A_SUBDIR)?'D':' ',
  193.                  (files[i].attrib & _A_EXECUTE)?'E':' ',
  194.                  (files[i].attrib & _A_SYSTEM)?'S':' ',
  195.                  (files[i].attrib & _A_HIDDEN)?'H':' ',
  196.                  (files[i].attrib & _A_RDONLY)?'R':' ',
  197.                  FullPath(files[i].name));
  198.       exit(EXIT_SUCCESS);
  199.     }
  200.  
  201.  
  202.   // At this point, he doesn't want a listing of those files, he wants to
  203.   // do some changes to them.
  204.  
  205.   for( i=0;i<number;i++ )
  206.     {
  207.       if( defined(_NWNLM_) )
  208.         {
  209. // For this to work, we must tell Netware to change the bits for file
  210. // 'files[i].name' to the value (file[i].attrib &flagand)|flagor.
  211.  
  212.           tempcreate[0] = DOSTimeFromCalendar(files[i].Create);
  213.           tempaccess[0] = DOSTimeFromCalendar(files[i].Access);
  214.           tempdate[0] =   DOSTimeFromCalendar(files[i].Write);
  215.           tempbackup[0] = DOSTimeFromCalendar(files[i].Backup);
  216.  
  217.           code = NLMLink("SetFileInfo",
  218.                          files[i].name,
  219.                          0x06,
  220.                          (files[i].attrib & ~flagand) | flagor,
  221.                          tempcreate,
  222.                          tempaccess,
  223.                          tempdate,
  224.                          tempbackup,
  225.                          files[i].uid);
  226.           if( code )
  227.             printf("\nError setting attributes for file %s\n",file[i].name);
  228.           continue;
  229.         }
  230.       if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
  231.         {
  232.           SetFileAttributes(files[i].name,
  233.                             (files[i].attrib & ~flagand) | flagor);
  234.           continue;
  235.         }
  236.       if( defined(_NTCON_) || defined(_NTWIN_) || defined(_95CON_) ||
  237.           defined(_95WIN_) )
  238.         {
  239.           DynamicLink("KERNEL32","SetFileAttributesA",STDCALL,files[i].name,
  240.                       (files[i].attrib & ~flagand) | flagor);
  241.           continue;
  242.         }
  243.  
  244.       printf("I do not know how to set file attributes on this system.\n");
  245.       exit(EXIT_FAILURE);
  246.     }
  247.   exit(EXIT_SUCCESS);
  248. }
  249.