chmode.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*** chmode.c ***/
  2. /* New version of the chmod program: accepts inputs of the
  3. same format as given by the ls -l command, ie it can
  4. be invoked like 'chmode rwxrw-r-- *' to make the modes
  5. of each file specified match the template!
  6. (C) 1984 Dave Taylor - HP Colorado Networks Operation
  7. Permission is hereby given for NON-PROFIT distribution of
  8. this software...all donations cheerily accepted!!!
  9. If automatic aggregate initialization is available, compile with;
  10. cc -Dauto chmode.c -o chmode
  11. otherwise, use;
  12. cc chmode.c -o chmode
  13. */
  14. #include <stdio.h>
  15. #define ERROR -1
  16. #define O_RD 256 /** 400 octal **/
  17. #define O_WR 128 /** 200 octal **/
  18. #define O_EX 64 /** 100 octal **/
  19. #define G_RD 32 /** 40 octal **/
  20. #define G_WR 16 /** 20 octal **/
  21. #define G_EX 8 /** 10 octal **/
  22. #define E_RD 4
  23. #define E_WR 2
  24. #define E_EX 1
  25. main(argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29. register int newmode = 0, j = 1;
  30. char buffer[9];
  31. if (argc < 3)
  32. exit(printf("Usage: %s <graphic mode> <file(s)>\n",argv[0]));
  33. --argc;
  34. strncpy(buffer,argv[j++], 9);
  35. if (strlen(buffer) != 9)
  36. exit(printf("Usage: %s <9 char graphic mode> <file(s)>\n",argv[0]));
  37. /** lets figure out the graphic mode translation! **/
  38. if ((newmode = translate(buffer)) == ERROR) {
  39. printf("Bad graphic mode designator! Please use 'rwxrwxrwx' as a template, \n");
  40. printf("indicating those accesses that you desire to prevent with a dash\n");
  41. printf(" For example: 'chmode rw-r--r-- test.c'\n");
  42. exit(1);
  43. }
  44. while (--argc > 0)
  45. chmod(argv[j++], newmode);
  46. }
  47. int
  48. translate(buffer)
  49. char buffer[];
  50. {
  51. /** translate a graphic representation of file access to
  52. an equivalent number as defined in CHMOD(2) **/
  53. register int loc = 0, sum = 0, val;
  54. #ifdef auto
  55. char type[] = "rwxrwxrwx";
  56. int mode[] = { O_RD, O_WR, O_EX, G_RD, G_WR, G_EX, E_RD, E_WR, E_EX };
  57. #else
  58. char type[9];
  59. int mode[9];
  60. mode[0] = O_RD; mode[1] = O_WR; mode[2] = O_EX;
  61. mode[3] = G_RD; mode[4] = G_WR; mode[5] = G_EX;
  62. mode[6] = E_RD; mode[7] = E_WR; mode[8] = E_EX;
  63. strcpy(type,"rwxrwxrwx");
  64. #endif
  65. for (loc = 0; loc < 9; loc++)
  66. if ((val = check(buffer[loc], type[loc], mode[loc])) == ERROR)
  67. return(ERROR);
  68. else sum += val;
  69. return(sum);
  70. }
  71. int
  72. check(ch, type, mask)
  73. char ch;
  74. int mask;
  75. {
  76. /** check to see if ch is either type or '-', returning
  77. either mask or ERROR **/
  78. if (ch == type) return(mask);
  79. else if (ch == '-') return(0);
  80. else return(ERROR);
  81. }