source: trunk/minix/commands/i386/mtools-3.9.7/mattrib.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 4.1 KB
Line 
1/*
2 * mattrib.c
3 * Change MSDOS file attribute flags
4 */
5
6#include "sysincludes.h"
7#include "msdos.h"
8#include "mtools.h"
9#include "mainloop.h"
10
11typedef struct Arg_t {
12 char add;
13 unsigned char remove;
14 struct MainParam_t mp;
15 int recursive;
16 int doPrintName;
17} Arg_t;
18
19int concise;
20
21static int attrib_file(direntry_t *entry, MainParam_t *mp)
22{
23 Arg_t *arg=(Arg_t *) mp->arg;
24
25 if(entry->entry != -3) {
26 /* if not root directory, change it */
27 entry->dir.attr = (entry->dir.attr & arg->remove) | arg->add;
28 dir_write(entry);
29 }
30 return GOT_ONE;
31}
32
33static int replay_attrib(direntry_t *entry, MainParam_t *mp)
34{
35 if ( (IS_ARCHIVE(entry) && IS_DIR(entry)) ||
36 (!IS_ARCHIVE(entry) && !IS_DIR(entry)) ||
37 IS_SYSTEM(entry) || IS_HIDDEN(entry)) {
38
39 printf("mattrib ");
40
41 if (IS_ARCHIVE(entry) && IS_DIR(entry)) {
42 printf("+a ");
43 }
44
45 if (!IS_ARCHIVE(entry) && !IS_DIR(entry)) {
46 printf("-a ");
47 }
48
49 if (IS_SYSTEM(entry)) {
50 printf("+s ");
51 }
52
53 if (IS_HIDDEN(entry)) {
54 printf("+h ");
55 }
56
57 fprintPwd(stdout, entry, 1);
58 printf("\n");
59 }
60 return GOT_ONE;
61}
62
63
64
65static int view_attrib(direntry_t *entry, MainParam_t *mp)
66{
67 printf(" ");
68 if(IS_ARCHIVE(entry))
69 putchar('A');
70 else
71 putchar(' ');
72 fputs(" ",stdout);
73 if(IS_SYSTEM(entry))
74 putchar('S');
75 else
76 putchar(' ');
77 if(IS_HIDDEN(entry))
78 putchar('H');
79 else
80 putchar(' ');
81 if(IS_READONLY(entry))
82 putchar('R');
83 else
84 putchar(' ');
85 printf(" ");
86 fprintPwd(stdout, entry, 0);
87 printf("\n");
88 return GOT_ONE;
89}
90
91
92static int concise_view_attrib(direntry_t *entry, MainParam_t *mp)
93{
94 Arg_t *arg=(Arg_t *) mp->arg;
95
96 if(IS_ARCHIVE(entry))
97 putchar('A');
98 if(IS_DIR(entry))
99 putchar('D');
100 if(IS_SYSTEM(entry))
101 putchar('S');
102 if(IS_HIDDEN(entry))
103 putchar('H');
104 if(IS_READONLY(entry))
105 putchar('R');
106 if(arg->doPrintName) {
107 putchar(' ');
108 fprintPwd(stdout, entry, 0);
109 }
110 putchar('\n');
111 return GOT_ONE;
112}
113
114static int recursive_attrib(direntry_t *entry, MainParam_t *mp)
115{
116 mp->callback(entry, mp);
117 return mp->loop(mp->File, mp, "*");
118}
119
120
121static void usage(void) NORETURN;
122static void usage(void)
123{
124 fprintf(stderr, "Mtools version %s, dated %s\n",
125 mversion, mdate);
126 fprintf(stderr,
127 "Usage: %s [-p/X] [-a|+a] [-h|+h] [-r|+r] [-s|+s] msdosfile [msdosfiles...]\n"
128 "\t-p Replay how mattrib would set up attributes\n"
129 "\t-/ Recursive\n"
130 "\t-X Concise\n",
131 progname);
132 exit(1);
133}
134
135static int letterToCode(int letter)
136{
137 switch (toupper(letter)) {
138 case 'A':
139 return ATTR_ARCHIVE;
140 case 'H':
141 return ATTR_HIDDEN;
142 case 'R':
143 return ATTR_READONLY;
144 case 'S':
145 return ATTR_SYSTEM;
146 default:
147 usage();
148 }
149}
150
151
152void mattrib(int argc, char **argv, int type)
153{
154 Arg_t arg;
155 int view;
156 int c;
157 int concise;
158 int replay;
159 char *ptr;
160
161 arg.add = 0;
162 arg.remove = 0xff;
163 arg.recursive = 0;
164 arg.doPrintName = 1;
165 view = 0;
166 concise = 0;
167 replay = 0;
168
169 while ((c = getopt(argc, argv, "/ahrsAHRSXp")) != EOF) {
170 switch (c) {
171 default:
172 arg.remove &= ~letterToCode(c);
173 break;
174 case 'p':
175 replay = 1;
176 break;
177 case '/':
178 arg.recursive = 1;
179 break;
180 case 'X':
181 concise = 1;
182 break;
183 case '?':
184 usage();
185 }
186 }
187
188 for(;optind < argc;optind++) {
189 switch(argv[optind][0]) {
190 case '+':
191 for(ptr = argv[optind] + 1; *ptr; ptr++)
192 arg.add |= letterToCode(*ptr);
193 continue;
194 case '-':
195 for(ptr = argv[optind] + 1; *ptr; ptr++)
196 arg.remove &= ~letterToCode(*ptr);
197 continue;
198 }
199 break;
200 }
201
202 if(arg.remove == 0xff && !arg.add)
203 view = 1;
204
205 if (optind >= argc)
206 usage();
207
208 init_mp(&arg.mp);
209 if(view){
210 if(concise) {
211 arg.mp.callback = concise_view_attrib;
212 arg.doPrintName = (argc - optind > 1 ||
213 arg.recursive ||
214 strpbrk(argv[optind], "*[?") != 0);
215 } else if (replay) {
216 arg.mp.callback = replay_attrib;
217 } else
218 arg.mp.callback = view_attrib;
219 arg.mp.openflags = O_RDONLY;
220 } else {
221 arg.mp.callback = attrib_file;
222 arg.mp.openflags = O_RDWR;
223 }
224
225 if(arg.recursive)
226 arg.mp.dirCallback = recursive_attrib;
227
228 arg.mp.arg = (void *) &arg;
229 arg.mp.lookupflags = ACCEPT_PLAIN | ACCEPT_DIR;
230 if(arg.recursive)
231 arg.mp.lookupflags |= DO_OPEN_DIRS | NO_DOTS;
232 exit(main_loop(&arg.mp, argv + optind, argc - optind));
233}
Note: See TracBrowser for help on using the repository browser.