|
Lines 64-76
Link Here
|
| 64 |
{ |
64 |
{ |
| 65 |
FTS *ftsp; |
65 |
FTS *ftsp; |
| 66 |
FTSENT *p; |
66 |
FTSENT *p; |
| 67 |
u_long clear, set; |
67 |
u_long clear, set, newflags; |
| 68 |
long val; |
68 |
long val; |
| 69 |
int Hflag, Lflag, Pflag, Rflag, ch, fts_options, oct, rval; |
69 |
int Hflag, Lflag, Pflag, Rflag, hflag, ch, fts_options, oct, rval; |
| 70 |
char *flags, *ep; |
70 |
char *flags, *ep; |
|
|
71 |
int (*change_flags) __P((const char *, u_long)); |
| 71 |
|
72 |
|
| 72 |
Hflag = Lflag = Pflag = Rflag = 0; |
73 |
Hflag = Lflag = Pflag = Rflag = hflag = 0; |
| 73 |
while ((ch = getopt(argc, argv, "HLPR")) != -1) |
74 |
while ((ch = getopt(argc, argv, "HLPRh")) != -1) |
| 74 |
switch (ch) { |
75 |
switch (ch) { |
| 75 |
case 'H': |
76 |
case 'H': |
| 76 |
Hflag = 1; |
77 |
Hflag = 1; |
|
Lines 87-92
Link Here
|
| 87 |
case 'R': |
88 |
case 'R': |
| 88 |
Rflag = 1; |
89 |
Rflag = 1; |
| 89 |
break; |
90 |
break; |
|
|
91 |
case 'h': |
| 92 |
hflag = 1; |
| 93 |
break; |
| 90 |
case '?': |
94 |
case '?': |
| 91 |
default: |
95 |
default: |
| 92 |
usage(); |
96 |
usage(); |
|
Lines 106-112
Link Here
|
| 106 |
fts_options |= FTS_LOGICAL; |
110 |
fts_options |= FTS_LOGICAL; |
| 107 |
} |
111 |
} |
| 108 |
} else |
112 |
} else |
| 109 |
fts_options = FTS_LOGICAL; |
113 |
fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL; |
| 110 |
|
114 |
|
| 111 |
flags = *argv; |
115 |
flags = *argv; |
| 112 |
if (*flags >= '0' && *flags <= '7') { |
116 |
if (*flags >= '0' && *flags <= '7') { |
|
Lines 131-136
Link Here
|
| 131 |
err(1, NULL); |
135 |
err(1, NULL); |
| 132 |
|
136 |
|
| 133 |
for (rval = 0; (p = fts_read(ftsp)) != NULL;) { |
137 |
for (rval = 0; (p = fts_read(ftsp)) != NULL;) { |
|
|
138 |
change_flags = chflags; |
| 134 |
switch (p->fts_info) { |
139 |
switch (p->fts_info) { |
| 135 |
case FTS_D: |
140 |
case FTS_D: |
| 136 |
if (Rflag) /* Change it at FTS_DP. */ |
141 |
if (Rflag) /* Change it at FTS_DP. */ |
|
Lines 146-173
Link Here
|
| 146 |
warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); |
151 |
warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); |
| 147 |
rval = 1; |
152 |
rval = 1; |
| 148 |
continue; |
153 |
continue; |
| 149 |
case FTS_SL: /* Ignore. */ |
154 |
case FTS_SL: /* Ignore unless -h. */ |
|
|
155 |
/* |
| 156 |
* All symlinks we found while doing a physical |
| 157 |
* walk end up here. |
| 158 |
*/ |
| 159 |
if (!hflag) |
| 160 |
continue; |
| 161 |
/* |
| 162 |
* Note that if we follow a symlink, fts_info is |
| 163 |
* not FTS_SL but FTS_F or whatever. And we should |
| 164 |
* use lchflags only for FTS_SL and should use chflags |
| 165 |
* for others. |
| 166 |
*/ |
| 167 |
change_flags = lchflags; |
| 168 |
break; |
| 169 |
|
| 150 |
case FTS_SLNONE: |
170 |
case FTS_SLNONE: |
| 151 |
/* |
171 |
/* |
| 152 |
* The only symlinks that end up here are ones that |
172 |
* The only symlinks that end up here are ones that |
| 153 |
* don't point to anything and ones that we found |
173 |
* don't point to anything. Note that if we are |
| 154 |
* doing a physical walk. |
174 |
* doing a physical walk, we never reach here unless |
|
|
175 |
* we asked to follow explicitly (with -H or -L). |
| 155 |
*/ |
176 |
*/ |
| 156 |
continue; |
177 |
continue; |
| 157 |
default: |
178 |
default: |
| 158 |
break; |
179 |
break; |
| 159 |
} |
180 |
} |
| 160 |
if (oct) { |
181 |
if (oct) |
| 161 |
if (!chflags(p->fts_accpath, set)) |
182 |
newflags = set; |
| 162 |
continue; |
183 |
else { |
| 163 |
} else { |
184 |
newflags = p->fts_statp->st_flags; |
| 164 |
p->fts_statp->st_flags |= set; |
185 |
newflags |= set; |
| 165 |
p->fts_statp->st_flags &= clear; |
186 |
newflags &= clear; |
| 166 |
if (!chflags(p->fts_accpath, (u_long)p->fts_statp->st_flags)) |
187 |
} |
| 167 |
continue; |
188 |
if ((*change_flags)(p->fts_accpath, newflags)) { |
|
|
189 |
warn("%s", p->fts_path); |
| 190 |
rval = 1; |
| 168 |
} |
191 |
} |
| 169 |
warn("%s", p->fts_path); |
|
|
| 170 |
rval = 1; |
| 171 |
} |
192 |
} |
| 172 |
if (errno) |
193 |
if (errno) |
| 173 |
err(1, "fts_read"); |
194 |
err(1, "fts_read"); |
|
Lines 178-183
Link Here
|
| 178 |
usage(void) |
199 |
usage(void) |
| 179 |
{ |
200 |
{ |
| 180 |
(void)fprintf(stderr, |
201 |
(void)fprintf(stderr, |
| 181 |
"usage: chflags [-R [-H | -L | -P]] flags file ...\n"); |
202 |
"usage: chflags [-R [-H | -L | -P]] [-h] flags file ...\n"); |
| 182 |
exit(1); |
203 |
exit(1); |
| 183 |
} |
204 |
} |