chflags.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1992, 1993, 1994
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <err.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <fts.h>
  37. #include <signal.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. static volatile sig_atomic_t siginfo;
  43. static void usage(void) __dead2;
  44. static void
  45. siginfo_handler(int sig __unused)
  46. {
  47. siginfo = 1;
  48. }
  49. int
  50. main(int argc, char *argv[])
  51. {
  52. FTS *ftsp;
  53. FTSENT *p;
  54. u_long clear, newflags, set;
  55. long val;
  56. int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
  57. int ch, e, fts_options, oct, rval;
  58. char *flags, *ep;
  59. Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
  60. while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
  61. switch (ch) {
  62. case 'H':
  63. Hflag = 1;
  64. Lflag = 0;
  65. break;
  66. case 'L':
  67. Lflag = 1;
  68. Hflag = 0;
  69. break;
  70. case 'P':
  71. Hflag = Lflag = 0;
  72. break;
  73. case 'R':
  74. Rflag = 1;
  75. break;
  76. case 'f':
  77. fflag = 1;
  78. break;
  79. case 'h':
  80. hflag = 1;
  81. break;
  82. case 'v':
  83. vflag++;
  84. break;
  85. case 'x':
  86. xflag = 1;
  87. break;
  88. case '?':
  89. default:
  90. usage();
  91. }
  92. argv += optind;
  93. argc -= optind;
  94. if (argc < 2)
  95. usage();
  96. (void)signal(SIGINFO, siginfo_handler);
  97. if (Rflag) {
  98. if (hflag)
  99. errx(1, "the -R and -h options may not be "
  100. "specified together.");
  101. if (Lflag) {
  102. fts_options = FTS_LOGICAL;
  103. } else {
  104. fts_options = FTS_PHYSICAL;
  105. if (Hflag) {
  106. fts_options |= FTS_COMFOLLOW;
  107. }
  108. }
  109. } else if (hflag) {
  110. fts_options = FTS_PHYSICAL;
  111. } else {
  112. fts_options = FTS_LOGICAL;
  113. }
  114. if (xflag)
  115. fts_options |= FTS_XDEV;
  116. flags = *argv;
  117. if (*flags >= '0' && *flags <= '7') {
  118. errno = 0;
  119. val = strtol(flags, &ep, 8);
  120. if (val < 0)
  121. errno = ERANGE;
  122. if (errno)
  123. err(1, "invalid flags: %s", flags);
  124. if (*ep)
  125. errx(1, "invalid flags: %s", flags);
  126. set = val;
  127. oct = 1;
  128. } else {
  129. if (strtofflags(&flags, &set, &clear))
  130. errx(1, "invalid flag: %s", flags);
  131. clear = ~clear;
  132. oct = 0;
  133. }
  134. if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
  135. err(1, NULL);
  136. for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
  137. int atflag;
  138. if ((fts_options & FTS_LOGICAL) ||
  139. ((fts_options & FTS_COMFOLLOW) &&
  140. p->fts_level == FTS_ROOTLEVEL))
  141. atflag = 0;
  142. else
  143. atflag = AT_SYMLINK_NOFOLLOW;
  144. switch (p->fts_info) {
  145. case FTS_D: /* Change it at FTS_DP if we're recursive. */
  146. if (!Rflag)
  147. fts_set(ftsp, p, FTS_SKIP);
  148. continue;
  149. case FTS_DNR: /* Warn, chflags. */
  150. warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
  151. rval = 1;
  152. break;
  153. case FTS_ERR: /* Warn, continue. */
  154. case FTS_NS:
  155. warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
  156. rval = 1;
  157. continue;
  158. default:
  159. break;
  160. }
  161. if (oct)
  162. newflags = set;
  163. else
  164. newflags = (p->fts_statp->st_flags | set) & clear;
  165. if (newflags == p->fts_statp->st_flags)
  166. continue;
  167. if (chflagsat(AT_FDCWD, p->fts_accpath, newflags,
  168. atflag) == -1) {
  169. e = errno;
  170. if (!fflag) {
  171. warnc(e, "%s", p->fts_path);
  172. rval = 1;
  173. }
  174. if (siginfo) {
  175. (void)printf("%s: %s\n", p->fts_path,
  176. strerror(e));
  177. siginfo = 0;
  178. }
  179. } else if (vflag || siginfo) {
  180. (void)printf("%s", p->fts_path);
  181. if (vflag > 1 || siginfo)
  182. (void)printf(": 0%lo -> 0%lo",
  183. (u_long)p->fts_statp->st_flags,
  184. newflags);
  185. (void)printf("\n");
  186. siginfo = 0;
  187. }
  188. }
  189. if (errno)
  190. err(1, "fts_read");
  191. exit(rval);
  192. }
  193. static void
  194. usage(void)
  195. {
  196. (void)fprintf(stderr,
  197. "usage: chflags [-fhvx] [-R [-H | -L | -P]] flags file ...\n");
  198. exit(1);
  199. }