chflags.c (9ddb49cbe45441fa3f3a10f6dd355e9956480b5f) chflags.c (7e81a15205e9d1d64127ec49347430b99897142f)
1/*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 48 unchanged lines hidden (view full) ---

57
58int
59main(int argc, char *argv[])
60{
61 FTS *ftsp;
62 FTSENT *p;
63 u_long clear, set;
64 long val;
1/*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 48 unchanged lines hidden (view full) ---

57
58int
59main(int argc, char *argv[])
60{
61 FTS *ftsp;
62 FTSENT *p;
63 u_long clear, set;
64 long val;
65 int Hflag, Lflag, Rflag, ch, fts_options, oct, rval;
65 int Hflag, Lflag, Rflag, hflag, ch, fts_options, oct, rval;
66 char *flags, *ep;
66 char *flags, *ep;
67 int (*change_flags)(const char *, unsigned long);
67
68
68 Hflag = Lflag = Rflag = 0;
69 while ((ch = getopt(argc, argv, "HLPR")) != -1)
69 Hflag = Lflag = Rflag = hflag = 0;
70 while ((ch = getopt(argc, argv, "HLPRh")) != -1)
70 switch (ch) {
71 case 'H':
72 Hflag = 1;
73 Lflag = 0;
74 break;
75 case 'L':
76 Lflag = 1;
77 Hflag = 0;
78 break;
79 case 'P':
80 Hflag = Lflag = 0;
81 break;
82 case 'R':
83 Rflag = 1;
84 break;
71 switch (ch) {
72 case 'H':
73 Hflag = 1;
74 Lflag = 0;
75 break;
76 case 'L':
77 Lflag = 1;
78 Hflag = 0;
79 break;
80 case 'P':
81 Hflag = Lflag = 0;
82 break;
83 case 'R':
84 Rflag = 1;
85 break;
86 case 'h':
87 hflag = 1;
88 break;
85 case '?':
86 default:
87 usage();
88 }
89 argv += optind;
90 argc -= optind;
91
92 if (argc < 2)
93 usage();
94
95 if (Rflag) {
96 fts_options = FTS_PHYSICAL;
89 case '?':
90 default:
91 usage();
92 }
93 argv += optind;
94 argc -= optind;
95
96 if (argc < 2)
97 usage();
98
99 if (Rflag) {
100 fts_options = FTS_PHYSICAL;
101 if (hflag)
102 errx(1, "the -R and -h options "
103 "may not be specified together");
97 if (Hflag)
98 fts_options |= FTS_COMFOLLOW;
99 if (Lflag) {
100 fts_options &= ~FTS_PHYSICAL;
101 fts_options |= FTS_LOGICAL;
102 }
103 } else
104 fts_options = FTS_LOGICAL;
105
104 if (Hflag)
105 fts_options |= FTS_COMFOLLOW;
106 if (Lflag) {
107 fts_options &= ~FTS_PHYSICAL;
108 fts_options |= FTS_LOGICAL;
109 }
110 } else
111 fts_options = FTS_LOGICAL;
112
113 /* XXX: Why don't chflags and lchflags have compatible prototypes? */
114 if (hflag)
115 change_flags = (int (*)(const char *, unsigned long))lchflags;
116 else
117 change_flags = chflags;
118
106 flags = *argv;
107 if (*flags >= '0' && *flags <= '7') {
108 errno = 0;
109 val = strtol(flags, &ep, 8);
110 if (val < 0)
111 errno = ERANGE;
112 if (errno)
113 err(1, "invalid flags: %s", flags);

--- 28 unchanged lines hidden (view full) ---

142 continue;
143 case FTS_SL: /* Ignore. */
144 case FTS_SLNONE:
145 /*
146 * The only symlinks that end up here are ones that
147 * don't point to anything and ones that we found
148 * doing a physical walk.
149 */
119 flags = *argv;
120 if (*flags >= '0' && *flags <= '7') {
121 errno = 0;
122 val = strtol(flags, &ep, 8);
123 if (val < 0)
124 errno = ERANGE;
125 if (errno)
126 err(1, "invalid flags: %s", flags);

--- 28 unchanged lines hidden (view full) ---

155 continue;
156 case FTS_SL: /* Ignore. */
157 case FTS_SLNONE:
158 /*
159 * The only symlinks that end up here are ones that
160 * don't point to anything and ones that we found
161 * doing a physical walk.
162 */
150 continue;
163 if (!hflag)
164 continue;
165 /* FALLTHROUGH */
151 default:
152 break;
153 }
154 if (oct) {
166 default:
167 break;
168 }
169 if (oct) {
155 if (!chflags(p->fts_accpath, set))
170 if (!(*change_flags)(p->fts_accpath, set))
156 continue;
157 } else {
158 p->fts_statp->st_flags |= set;
159 p->fts_statp->st_flags &= clear;
171 continue;
172 } else {
173 p->fts_statp->st_flags |= set;
174 p->fts_statp->st_flags &= clear;
160 if (!chflags(p->fts_accpath, (u_long)p->fts_statp->st_flags))
175 if (!(*change_flags)(p->fts_accpath,
176 (u_long)p->fts_statp->st_flags))
161 continue;
162 }
163 warn("%s", p->fts_path);
164 rval = 1;
165 }
166 if (errno)
167 err(1, "fts_read");
168 exit(rval);
169}
170
171void
172usage(void)
173{
174 (void)fprintf(stderr,
177 continue;
178 }
179 warn("%s", p->fts_path);
180 rval = 1;
181 }
182 if (errno)
183 err(1, "fts_read");
184 exit(rval);
185}
186
187void
188usage(void)
189{
190 (void)fprintf(stderr,
175 "usage: chflags [-R [-H | -L | -P]] flags file ...\n");
191 "usage: chflags [-h] [-R [-H | -L | -P]] flags file ...\n");
176 exit(1);
177}
192 exit(1);
193}