1 /* Copyright 1986-1992 Emmet P. Gray.
2 * Copyright 1996-1998,2000-2002,2007,2009 Alain Knaff.
3 * This file is part of mtools.
5 * Mtools is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Mtools is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
19 * Change MSDOS file attribute flags
22 #include "sysincludes.h"
26 typedef struct Arg_t {
33 static int attrib_file(direntry_t *entry, MainParam_t *mp)
35 Arg_t *arg=(Arg_t *) mp->arg;
37 if(!isRootEntry(entry)) {
38 /* if not root directory, change it */
39 entry->dir.attr = (entry->dir.attr & arg->remove) | arg->add;
45 static int replay_attrib(direntry_t *entry, MainParam_t *mp UNUSEDP)
47 if ( (IS_ARCHIVE(entry) && IS_DIR(entry)) ||
48 (!IS_ARCHIVE(entry) && !IS_DIR(entry)) ||
49 IS_SYSTEM(entry) || IS_HIDDEN(entry)) {
53 if (IS_ARCHIVE(entry) && IS_DIR(entry)) {
57 if (!IS_ARCHIVE(entry) && !IS_DIR(entry)) {
61 if (IS_SYSTEM(entry)) {
65 if (IS_HIDDEN(entry)) {
69 fprintPwd(stdout, entry, 1);
77 static int view_attrib(direntry_t *entry, MainParam_t *mp UNUSEDP)
93 if(IS_READONLY(entry))
98 fprintPwd(stdout, entry, 0);
104 static int concise_view_attrib(direntry_t *entry, MainParam_t *mp)
106 Arg_t *arg=(Arg_t *) mp->arg;
108 if(IS_ARCHIVE(entry))
116 if(IS_READONLY(entry))
118 if(arg->doPrintName) {
120 fprintPwd(stdout, entry, 0);
126 static int recursive_attrib(direntry_t *entry, MainParam_t *mp)
128 mp->callback(entry, mp);
129 return mp->loop(mp->File, mp, "*");
133 static void usage(int ret) NORETURN;
134 static void usage(int ret)
136 fprintf(stderr, "Mtools version %s, dated %s\n",
139 "Usage: %s [-p] [-a|+a] [-h|+h] [-r|+r] [-s|+s] msdosfile [msdosfiles...]\n",
144 static int letterToCode(int letter)
146 switch (toupper(letter)) {
152 return ATTR_READONLY;
160 void mattrib(int argc, char **argv, int type UNUSEDP) NORETURN;
161 void mattrib(int argc, char **argv, int type UNUSEDP)
164 struct MainParam_t mp;
181 if(helpFlag(argc, argv))
183 while ((c = getopt(argc, argv, "i:/ahrsAHRSXp")) != EOF) {
189 arg.remove &= ~letterToCode(c);
192 set_cmd_line_image(optarg);
208 if(optind == argc && wantUsage) {
212 for(;optind < argc;optind++) {
213 switch(argv[optind][0]) {
215 for(ptr = argv[optind] + 1; *ptr; ptr++)
216 arg.add |= letterToCode(*ptr);
219 for(ptr = argv[optind] + 1; *ptr; ptr++)
220 arg.remove &= ~letterToCode(*ptr);
226 if(arg.remove == 0xff && !arg.add)
235 mp.callback = concise_view_attrib;
236 arg.doPrintName = (argc - optind > 1 ||
238 strpbrk(argv[optind], "*[?") != 0);
240 mp.callback = replay_attrib;
242 mp.callback = view_attrib;
243 mp.openflags = O_RDONLY;
245 mp.callback = attrib_file;
246 mp.openflags = O_RDWR;
250 mp.dirCallback = recursive_attrib;
252 mp.arg = (void *) &arg;
253 mp.lookupflags = ACCEPT_PLAIN | ACCEPT_DIR;
255 mp.lookupflags |= DO_OPEN_DIRS | NO_DOTS;
256 exit(main_loop(&mp, argv + optind, argc - optind));