flags.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * cpio - copy file archives in and out
  3. *
  4. * Gunnar Ritter, Freiburg i. Br., Germany, April 2003.
  5. */
  6. /*
  7. * Copyright (c) 2003 Gunnar Ritter
  8. *
  9. * This software is provided 'as-is', without any express or implied
  10. * warranty. In no event will the authors be held liable for any damages
  11. * arising from the use of this software.
  12. *
  13. * Permission is granted to anyone to use this software for any purpose,
  14. * including commercial applications, and to alter it and redistribute
  15. * it freely, subject to the following restrictions:
  16. *
  17. * 1. The origin of this software must not be misrepresented; you must not
  18. * claim that you wrote the original software. If you use this software
  19. * in a product, an acknowledgment in the product documentation would be
  20. * appreciated but is not required.
  21. *
  22. * 2. Altered source versions must be plainly marked as such, and must not be
  23. * misrepresented as being the original software.
  24. *
  25. * 3. This notice may not be removed or altered from any source distribution.
  26. */
  27. /* Sccsid @(#)flags.c 1.6 (gritter) 3/26/07 */
  28. #include <unistd.h>
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <stdlib.h>
  32. #include "cpio.h"
  33. void
  34. flags(int ac, char **av)
  35. {
  36. const char optstring[] =
  37. "iopaAbBcC:dDeE:fH:I:kKlLmM:O:PrR:sStTuvV6";
  38. int i, illegal = 0;
  39. if (getenv("SYSV3") != NULL)
  40. sysv3 = printsev = 1;
  41. while ((i = getopt(ac, av, optstring)) != EOF) {
  42. switch (i) {
  43. case 'i':
  44. case 'o':
  45. case 'p':
  46. if (action && action != i)
  47. illegal = 1;
  48. action = i;
  49. break;
  50. case 'a':
  51. aflag = 1;
  52. break;
  53. case 'A':
  54. Aflag = 1;
  55. break;
  56. case 'b':
  57. bflag = 1;
  58. break;
  59. case 'B':
  60. blksiz = 5120;
  61. Bflag = 1;
  62. break;
  63. case 'c':
  64. fmttype = sysv3 ? FMT_ODC : FMT_ASC;
  65. cflag = 1;
  66. break;
  67. case 'C':
  68. if ((blksiz = atol(optarg)) <= 0)
  69. msg(4, -2,
  70. "Illegal size given for -C option.\n");
  71. Cflag = 1;
  72. break;
  73. case 'd':
  74. dflag = 1;
  75. break;
  76. case 'D':
  77. Dflag = 1;
  78. break;
  79. case 'e':
  80. /*
  81. * This option is accepted for compatibility only,
  82. * -Hdec should be used instead.
  83. */
  84. fmttype = FMT_DEC;
  85. eflag = 1;
  86. break;
  87. case 'E':
  88. Eflag = optarg;
  89. break;
  90. case 'f':
  91. fflag = 1;
  92. break;
  93. case 'H':
  94. if (setfmt(optarg) < 0)
  95. illegal = 1;
  96. Hflag = 1;
  97. break;
  98. case 'I':
  99. Iflag = optarg;
  100. break;
  101. case 'k':
  102. kflag = 1;
  103. break;
  104. case 'K':
  105. /*
  106. * This option is accepted for compatibility only,
  107. * -Hsgi should be used instead.
  108. */
  109. fmttype = FMT_SGIBE;
  110. Kflag = 1;
  111. break;
  112. case 'l':
  113. lflag = 1;
  114. break;
  115. case 'L':
  116. Lflag = 1;
  117. break;
  118. case 'm':
  119. mflag = 1;
  120. break;
  121. case 'M':
  122. Mflag = oneintfmt(optarg);
  123. break;
  124. case 'O':
  125. Oflag = optarg;
  126. break;
  127. case 'P':
  128. Pflag = 1;
  129. break;
  130. case 'r':
  131. rflag = 1;
  132. break;
  133. case 'R':
  134. if (setreassign(Rflag = optarg) < 0)
  135. illegal = 1;
  136. break;
  137. case 's':
  138. sflag = 1;
  139. break;
  140. case 'S':
  141. Sflag = 1;
  142. break;
  143. case 't':
  144. tflag = 1;
  145. break;
  146. case 'u':
  147. uflag = 1;
  148. break;
  149. case 'v':
  150. vflag++;
  151. break;
  152. case 'V':
  153. Vflag = 1;
  154. break;
  155. case '6':
  156. sixflag = 1;
  157. fmttype = FMT_BINLE;
  158. break;
  159. default:
  160. if (sysv3)
  161. usage();
  162. illegal = 1;
  163. }
  164. }
  165. switch (action) {
  166. case 'i':
  167. if (Oflag || Kflag || eflag || Lflag || lflag || aflag ||
  168. Aflag || Pflag)
  169. illegal = 1;
  170. for (i = optind; i < ac; i++)
  171. addg(av[i], 0);
  172. break;
  173. case 'o':
  174. if (Iflag || dflag || fflag || kflag || mflag ||
  175. rflag || tflag || uflag ||
  176. sixflag || Eflag || Rflag)
  177. illegal = 1;
  178. if (optind != ac)
  179. illegal = 1;
  180. break;
  181. case 'p':
  182. if (Iflag || Oflag || blksiz || Eflag || fmttype != FMT_NONE ||
  183. Mflag || bflag || fflag || kflag || sflag ||
  184. tflag || Sflag || sixflag)
  185. illegal = 1;
  186. if (optind + 1 != ac)
  187. illegal = 1;
  188. break;
  189. default:
  190. if (sysv3 == 0)
  191. msg(3, 0, "One of -i, -o or -p must be specified.\n");
  192. else if (ac > 1)
  193. msg(3, -2, "Options must include one: -o, -i, -p.\n");
  194. illegal = 1;
  195. }
  196. /*
  197. * Sanity checks. No check for multiple occurences of options
  198. * since they can make sense, behave as other programs and use
  199. * the latter one.
  200. */
  201. /*if (aflag && mflag) {
  202. msg(3, 0, "-a and -m are mutually exclusive.\n");
  203. illegal = 1;
  204. } why? */
  205. /*if (cflag && (Hflag || Kflag || eflag)) {
  206. msg(3, 0, "-c and -H are mutually exclusive.\n");
  207. illegal = 1;
  208. } allow overriding -c with -H and vice versa */
  209. if ((vflag || tflag) && Vflag) {
  210. msg(3, 0, "-v and -V are mutually exclusive.\n");
  211. illegal = 1;
  212. }
  213. /*if (Bflag && Cflag) {
  214. msg(3, 0, "-B and -C are mutually exclusive.\n");
  215. illegal = 1;
  216. } allow overriding of block sizes */
  217. if ((Hflag || cflag || Kflag || eflag) && sixflag) {
  218. msg(3, 0, "-H and -6 are mutually exclusive.\n");
  219. illegal = 1;
  220. }
  221. if (!sysv3 && Mflag && Oflag == NULL && Iflag == NULL) {
  222. msg(3, 0, "-M not meaningful without -O or -I.\n");
  223. illegal = 1;
  224. }
  225. if (!sysv3 && Aflag && Oflag == NULL) {
  226. msg(3, 0, "-A requires the -O option\n");
  227. illegal = 1;
  228. }
  229. if (illegal)
  230. usage();
  231. }
  232. void
  233. usage(void)
  234. {
  235. if (sysv3)
  236. fprintf(stderr, "\
  237. Usage: %s -o[acvVABL] [-Csize] [-Hhdr] [-Mmsg] <name-list >collection\n\
  238. \t%s -o[acvVABL] -Ocollection [-Csize] [-Hhdr] [-Mmsg] <name-list\n\
  239. \t%s -i[bcdkmrsStuvVfB6] [-Csize] [-Efile] [-Hhdr] [-Mmsg] [-Rid] [pattern ...] <collection\n\
  240. \t%s -i[bcdkmrsStuvVfB6] -Icollection [-Csize] [-Efile] [-Hhdr] [-Mmsg] [-Rid] [pattern ...]\n\
  241. \t%s -p[adlmruvVL] [-Rid] directory <name-list\n",
  242. progname, progname, progname, progname, progname);
  243. else
  244. fprintf(stderr, "USAGE:\n\
  245. \t%s -i[bcdfkmrstuvBSV6] [-C size] [-E file] [-H hdr] [[-I file] [-M msg]] \
  246. [-R id] [patterns]\n\
  247. \t%s -o[acvABLV] [-C size] [-H hdr] [[-M msg] [-O file]]\n\
  248. \t%s -p[adlmuvLV] [-R id] directory\n",
  249. progname, progname, progname);
  250. exit(1);
  251. }