summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/chmod.c
blob: d74bef83fe4bdc3e9dd3f44132490a95c053dac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
**  CHMOD.C - Retrieve or change a DOS file's attributes
**
**  public domain demo by Bob Stout
**
**  Notes: To expand command line arguments with wildcards,
**         TC/TC++/BC++  - Link in WILDARGS.OBJ.
**         MSC/QC        - Link in SETARGV.OBJ.
**         ZTC/C++       - Link in _MAINx.OBJ, where 'x' is the memory model.
**
**         Allows file list(s) using standard "@file_list_name" convention.
*/

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <ctype.h>
#include <string.h>

#define LAST_CHAR(s) (s)[strlen(s)-1]

#if defined(__TURBOC__)
 #include <io.h>
 #define FAR far
#else
 #include <stdarg.h>

 #define BOOL(x) (!(!(x)))
 #define FAR _far

 #if (defined(_MSC_VER) && (_MSC_VER >= 700)) || (defined(__SC__))
  // Make FP_xxx macros lvalues as in older versions
  #undef FP_SEG
  #undef FP_OFF
  #define FP_SEG(fp)    ((unsigned)((unsigned long)(fp) >> 16))
  #define FP_OFF(fp)    ((unsigned)(fp && 0xffff))
 #endif

 int _chmod(const char *path, int func, ...)
 {
       union REGS regs;
       struct SREGS sregs;
       int atr = 0;
       va_list args;

       if (0 != (func = BOOL(func)))
       {
            va_start(args, func);
            atr = va_arg(args, int);
       }
       regs.x.ax = 0x4300 + func;
       regs.x.dx = FP_OFF((char FAR *)path);
       regs.x.cx = atr;
       segread(&sregs);
       sregs.ds  = FP_SEG((char FAR *)path);
       intdosx(&regs, &regs, &sregs);
       if (regs.x.cflag)
            return -1;
       if (func)
            return atr;
       else return regs.x.cx;
 }

 #ifndef FA_RDONLY
  #define FA_RDONLY _A_RDONLY
 #endif

 #ifndef FA_HIDDEN
  #define FA_HIDDEN _A_HIDDEN
 #endif

 #ifndef FA_SYSTEM
  #define FA_SYSTEM _A_SYSTEM
 #endif

 #ifndef FA_ARCH
  #define FA_ARCH   _A_ARCH
 #endif

 #ifndef FA_LABEL
  #define FA_LABEL  _A_VOLID
 #endif

 #ifndef FA_DIREC
  #define FA_DIREC  _A_SUBDIR
 #endif
#endif

int attrib,                   /* Set up new attributes here             */
    atr_setmask = 0,
    atr_clrmask = -1,
    flag = 0;                 /* Passed as func to _chmod()             */

void usage(void)              /* Tell 'em they messed up                */
{
      puts("Usage: CHMOD file [file [...file] [+switches] [-switches]");
      puts("Where switches are one or more of:");
      puts("    A: Archive");
      puts("    R: Read only");
      puts("    H: Hidden");
      puts("    S: System");
      puts("File lists may be specified with \"@file_list_name\"");
      puts("With no switches, diplays current attributes.");
      puts("Displayed attributes are as above plus:");
      puts("    D: Subdirectory");
      puts("    V: Volume label");
      exit(1);
}

void setattr(char atr)        /* Set bits in attribute                  */
{
      switch (toupper(atr))
      {
      case 'A':
            atr_setmask |= FA_ARCH;
            break;
      case 'R':
            atr_setmask |= FA_RDONLY;
            break;
      case 'S':
            atr_setmask |= FA_SYSTEM;
            break;
      case 'H':
            atr_setmask |= FA_HIDDEN;
            break;
      default:
            usage();
      }
}

void clrattr(char atr)        /* Clear bits in attribute                */
{
      switch (toupper(atr))
      {
      case 'A':
            atr_clrmask &= ~FA_ARCH;
            break;
      case 'R':
            atr_clrmask &= ~FA_RDONLY;
            break;
      case 'S':
            atr_clrmask &= ~FA_SYSTEM;
            break;
      case 'H':
            atr_clrmask &= ~FA_HIDDEN;
            break;
      default:
            usage();
      }
}

void show_atr(char *path)
{
      char astr[7], *ptr;

      if (-1 == (attrib = _chmod(strupr(path), 0)))
      {
ATR_ERR:    printf("\aCHMOD: Error! (file: %s)", path);
            exit(-1);
      }
      attrib |= atr_setmask;
      attrib &= atr_clrmask;
      if (-1 == (attrib = _chmod(path, flag, attrib)))
            goto ATR_ERR;
      ptr = astr;
      *ptr++ = (char)((attrib & FA_ARCH)   ? 'A' : '.');
      *ptr++ = (char)((attrib & FA_RDONLY) ? 'R' : '.');
      *ptr++ = (char)((attrib & FA_SYSTEM) ? 'S' : '.');
      *ptr++ = (char)((attrib & FA_HIDDEN) ? 'H' : '.');
      *ptr++ = (char)((attrib & FA_DIREC)  ? 'D' : '.');
      *ptr++ = (char)((attrib & FA_LABEL)  ? 'V' : '.');
      *ptr = '\0';
      printf("%-15s %s\n", path, astr);
}

int main (int argc, char *argv[])
{
      int i, j;

      if (2 > argc)
            usage();
      for (i = 1; i < argc; ++i)    /* Build attribute masks            */
      {
            switch (*argv[i])
            {
            case '+':
                  for (j = 1; argv[i][j]; ++j)
                        setattr(argv[i][j]);
                  flag = 1;
                  break;
            case '-':
                  for (j = 1; argv[i][j]; ++j)
                        clrattr(argv[i][j]);
                  flag = 1;
                  break;
            default:
                  break;            /* Assume it's a file name          */
            }
      }
      for (i = 1; i < argc; ++i)    /* Scan filenames                   */
      {
            if (strchr("+-", *argv[i]))
                  continue;
            if ('@' == *argv[i])
            {
                  FILE *fp;
                  char buf[256], *ptr = &argv[i][1];

                  if (NULL == (fp = fopen(ptr, "r")))
                  {
                        printf("\aCHMOD: Error opening %s\n", ptr);
                        return -1;
                  }
                  while (NULL != fgets(buf, 255, fp))
                  {
                        LAST_CHAR(buf) = '\0';  /* Strip '\n'           */
                        show_atr(buf);
                  }
                  fclose(fp);
            }
            else  show_atr(argv[i]);
      }
      return 0;
}