home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TOOLS2.ZIP / CHMOD.C < prev    next >
C/C++ Source or Header  |  1988-07-05  |  5KB  |  217 lines

  1. /**
  2.  
  3.     CHMOD.C
  4.  
  5.     UNIX style file attribute modification program.  This program 
  6.     uses OS/2 specific code to get and set file attributes.
  7.  
  8. **/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <process.h>
  14. #include <os2.h>
  15.  
  16. /**
  17.     Constants
  18. **/
  19. #define F_READONLY    0x0001        /* read-only attribute */
  20. #define F_HIDDEN    0x0002        /* hidden attribute */
  21. #define F_SYSTEM    0x0004        /* system attribute */
  22. #define F_SUBDIR    0x0010        /* subdirectory attribute */
  23. #define F_ARCHIVE    0x0020        /* archive attribute */
  24.  
  25. /**
  26.     Function Prototypes
  27. **/
  28. static void SetAttribute(char *, unsigned) ;
  29. void main(int, char **) ;
  30.  
  31. /**
  32.  
  33.     SetAttribute()
  34.  
  35.     Set the attribute for a file to a specific number.  Uses OS/2
  36.     dependant code.
  37.  
  38.     This routine will return 0xFFFF if unable to set attribute.
  39.  
  40. **/
  41. static void SetAttribute(szName, uAtt)
  42. char *szName ;
  43. unsigned uAtt ;
  44. {
  45.     printf( "%s: (" , szName ) ;
  46.  
  47.     if ( uAtt == 0 )
  48.         printf( " Normal" ) ;
  49.     if ( uAtt & F_READONLY )
  50.         printf( " Read-Only" ) ;
  51.     if ( uAtt & F_HIDDEN )
  52.         printf( " Hidden" ) ;
  53.     if ( uAtt & F_SYSTEM )
  54.         printf( " System" ) ;
  55.     if ( uAtt & F_ARCHIVE )
  56.         printf( " Archive" ) ;
  57.     if ( uAtt & F_SUBDIR )
  58.         printf( " Subdirectory" ) ;
  59.  
  60.     printf( " )" ) ;
  61.  
  62.     if ( DosSetFileMode( szName, (uAtt & ~F_SUBDIR), 0L ) )
  63.         printf( " - Error!" ) ;
  64.  
  65.     printf( "\n" ) ;
  66. }
  67.  
  68. /**
  69.  
  70.     MAIN ROUTINE
  71.  
  72. **/
  73.  
  74. void main(argc, argv)
  75. int argc ;
  76. char *argv[] ;
  77. {
  78.     unsigned fTurnOn, fTurnOff ;
  79.     unsigned *fFlag ;
  80.     int nArg ;
  81.  
  82.     fprintf( stderr, "CHMOD - A file attribute change utility\n" ) ;
  83.     fprintf( stderr, "By Ralph Brendler\n\n" ) ;
  84.  
  85.     if ( argc < 2 )
  86.     {
  87.         printf( "Type \"chmod -?\" for help\n" ) ;
  88.         exit(1) ;
  89.     }
  90.  
  91.     fTurnOn = 0 ;            /* bits to turn on */
  92.     fTurnOff = 0 ;            /* bits to turn off */
  93.  
  94.     /* parse out the command line switches */
  95.     nArg = 1 ;
  96.     while ( *argv[nArg] == '-' || *argv[nArg] == '+' || *argv[nArg] == '=' )
  97.     {
  98.         char *szTmp ;
  99.  
  100.         if ( nArg >= argc )
  101.         {
  102.             printf( "ERROR: no file names\n" ) ;
  103.             exit(1) ;
  104.         }
  105.         
  106.         szTmp = argv[nArg] ;
  107.         
  108.         if ( *szTmp == '-' )
  109.             fFlag = &fTurnOff ;
  110.         else if ( *szTmp == '+' )
  111.             fFlag = &fTurnOn ;
  112.         else    /* *szTmp == '=' */
  113.         {
  114.             fFlag = &fTurnOn ;
  115.             fTurnOff = 0xFFFF ;
  116.         }
  117.  
  118.         while ( *(++szTmp) )
  119.         {
  120.             switch( *szTmp ) 
  121.             {
  122.             case 'a':        /* archive */
  123.                 *fFlag |= F_ARCHIVE ;
  124.                 break ;
  125.  
  126.             case 'h':        /* hidden */
  127.                 *fFlag |= F_HIDDEN ;
  128.                 break ;
  129.  
  130.             case 'r':        /* read only */
  131.                 *fFlag |= F_READONLY ;
  132.                 break ;
  133.  
  134.             case 's':        /* system */
  135.                 *fFlag |= F_SYSTEM ;
  136.                 break ;
  137.  
  138.             case '?':        /* help */
  139.                 printf( "Usage: chmod {+-=}{ahrs} [file...]\n" ) ;
  140.                 exit(0) ;
  141.                 /* unreachable - break implied */
  142.  
  143.             default:        /* bad input */
  144.                 printf( "ERROR: unrecognized switch %s\n",
  145.                             argv[nArg] ) ;
  146.                 exit(1) ;
  147.                 /* unreachable - break implied */
  148.             }
  149.         }
  150.         ++nArg ;
  151.     }
  152.  
  153.     /* Run down list of files, setting attributes.    */
  154.  
  155.     /* Note that this section uses the DosGetFirst    */
  156.     /* and DosGetNext funcs, rather than linking to */
  157.     /* the SETARGV object, since SETARGV will not    */
  158.     /* find hidden or read-only files.        */
  159.     while ( nArg < argc )
  160.     {
  161.         HDIR hDir = 1 ;
  162.         unsigned uCnt = 1 ;
  163.         FILEFINDBUF FileBuf ;
  164.         unsigned uAtt ;
  165.         char szPath[80] ;
  166.         char *szName ;
  167.  
  168.         /* read the first file name */        
  169.         DosFindFirst( argv[nArg], &hDir, 0x37, &FileBuf, 
  170.                     sizeof(FileBuf), &uCnt, 0L ) ;
  171.  
  172.         /* strip off the path information */
  173.         strcpy( szPath, argv[nArg] ) ;
  174.         szName = strrchr( szPath, '\\' ) ;
  175.         if ( szName )
  176.             ++szName ;
  177.         else
  178.             szName = szPath ;
  179.  
  180.         while ( uCnt > 0 )
  181.         {
  182.             uAtt = FileBuf.attrFile ;
  183.             strncpy(szName, FileBuf.achName, FileBuf.cchName) ;
  184.             szName[FileBuf.cchName] = '\0' ;
  185.  
  186.             if ( uAtt & F_SUBDIR )    /* hidden only */
  187.             {
  188.                 /* ignore '.' and '..' directories */
  189.                 if ( *szName != '.' )
  190.                 {
  191.                     if ( (fTurnOn & ~F_HIDDEN) 
  192.                             || (fTurnOff & ~F_HIDDEN) )
  193.                         printf( "WARNING: Can only change Hidden attribute for a directory\n" ) ;
  194.                     if ( fTurnOn & F_HIDDEN || fTurnOff & F_HIDDEN )
  195.                     {
  196.                         uAtt &= (~(fTurnOff & F_HIDDEN)) ;
  197.                         uAtt |= (fTurnOn & F_HIDDEN) ;
  198.                         SetAttribute( szPath, uAtt ) ;
  199.                     } 
  200.                 }
  201.             }
  202.             else
  203.             {
  204.                 uAtt &= (~fTurnOff) ;
  205.                 uAtt |= fTurnOn ;
  206.                 SetAttribute( szPath, uAtt ) ;
  207.             }
  208.  
  209.             /* read the next file name */
  210.             hDir = 1 ;
  211.             uCnt = 1 ;
  212.             DosFindNext(hDir, &FileBuf, sizeof(FileBuf), &uCnt) ;
  213.         }
  214.         ++nArg ;
  215.     }
  216. }
  217.