home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / C_DISK5.ZIP / CHMOD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-29  |  2.7 KB  |  141 lines

  1. /*_ chmod.c   Mon Feb 29 1988   Modified by: Walter Bright */
  2. /* Copyright (C) 1987-1988 by Walter Bright    */
  3. /* All Rights Reserved                */
  4. /* Written by Walter Bright            */
  5. /* To compile with Zortech C:            */
  6. /*    ztc -mti chmod                */
  7.  
  8. #include <stdio.h>
  9. #include <dos.h>
  10.  
  11. /* Attribute bits:    */
  12. #define R    1
  13. #define H    2
  14. #define S    4
  15. #define V    8
  16. #define D    0x10
  17. #define A    0x20
  18.  
  19. main(argc,argv)
  20. int argc;
  21. char *argv[];
  22. {    int i,j,c;
  23.     char *p;
  24.     struct FIND *find;
  25.     unsigned onmask,offmask;
  26.     static char flag[9] = "RHSVDAUU";
  27.  
  28.     if (argc <= 1)
  29.         usage();
  30.     p = argv[1];
  31.     if (*p == '+' || *p == '-')
  32.     {
  33.         onmask = 0;
  34.         offmask = 0;
  35.         while (1)
  36.         {
  37.         c = toupper(p[1]);
  38.         if (*p == '+')
  39.         {    switch (c)
  40.              {    case 'R':    onmask |= R;    break;
  41.             case 'H':    onmask |= H;    break;
  42.             case 'S':    onmask |= S;    break;
  43.             case 'V':    onmask |= V;    break;
  44.             case 'D':    onmask |= D;    break;
  45.             case 'A':    onmask |= A;    break;
  46.             default:
  47.                 faterr("unrecognized flag '%s'",p);
  48.             }
  49.         }
  50.         else if (*p == '-')
  51.         {    switch (c)
  52.              {    case 'R':    offmask |= R;    break;
  53.             case 'H':    offmask |= H;    break;
  54.             case 'S':    offmask |= S;    break;
  55.             case 'V':    offmask |= V;    break;
  56.             case 'D':    offmask |= D;    break;
  57.             case 'A':    offmask |= A;    break;
  58.             default:
  59.                 faterr("unrecognized flag '%s'",p);
  60.             }
  61.         }
  62.         else if (*p == 0)
  63.             break;
  64.         else
  65.             faterr("unrecognized flag '%s'",p);
  66.         p += 2;
  67.         }
  68.         for (i = 2; i < argc; i++)
  69.         {
  70.         find = findfirst(argv[i],0xFF);
  71.         while (find)
  72.         {    c = find->attribute;
  73.             c = (c & ~offmask) | onmask;
  74.             chmod(find->name,c);
  75.             find = findnext();
  76.         }
  77.         }
  78.     }
  79.     else /* merely print the attributes of the files */
  80.     {
  81.         for (i = 1; i < argc; i++)
  82.         {
  83.         find = findfirst(argv[i],0xFF);
  84.         while (find)
  85.         {   c = find->attribute;
  86.             for (j = 7; j >= 0; j--)
  87.             if ((1 << j) & c)
  88.                 printf("%c",flag[j]);
  89.             else
  90.                 printf("-");
  91.             printf("\t0x%02x\t%s\n",c,find->name);
  92.             find = findnext();
  93.         }
  94.         }
  95.     }
  96. }
  97.  
  98. /********************
  99.  * Change attribute of file.
  100.  */
  101.  
  102. int chmod(filename,mode)
  103. char *filename;
  104. int mode;
  105. {    union REGS regs;
  106.  
  107.     regs.x.ax = 0x4301;
  108.     regs.x.cx = mode;
  109.     regs.x.dx = (int) filename;
  110.     intdos(®s,®s);
  111. }
  112.  
  113. faterr(f,p)
  114. char *f,*p;
  115. {
  116.     printf("Fatal error: ");
  117.     printf(f,p);
  118.     printf("\n");
  119.     exit(1);
  120. }
  121.  
  122. usage()
  123. {
  124.     printf("\
  125. File attribute byte display/modification program.\n\
  126. Use:\n\
  127.     chmod {(+|-)flag} file...\n\
  128. Where:\n\
  129.     flag is one of R,H,S,V,D,A\n\
  130.     R    Read-only\n\
  131.     H    Hidden\n\
  132.     S    System\n\
  133.     V    Volume label\n\
  134.     D    Sub-directory\n\
  135.     A    Archive\n\
  136. \n\
  137. If no flags are given, the attributes are displayed.\n\
  138. ");
  139. }
  140.  
  141.