Debian Codegraph @

restore/main.c

raw symbols SCIP
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <[email protected]>, 1994-1997
5 * Stelian Pop <[email protected]>, 1999-2000
6 * Stelian Pop <[email protected]> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include "config.h" // IWYU pragma: keep
39 #include "restore_globals.h"
40 #include <sys/types.h>
41 #include <fcntl.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44
45 #include "compat/include/bsdcompat.h"
46 #include <signal.h>
47 #include <string.h>
48 #include "compat/include/protocols/dumprestore.h"
49
50 #include "compat/include/compaterr.h" // IWYU pragma: keep
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <inttypes.h>
55 #include <paths.h>
56 #include <time.h>
57
58 #include <ext2fs/ext2fs.h>
59 #include <getopt.h>
60
61 #include "compat/include/pathnames.h"
62 #include "restore.h"
63 #include "readtape.h"
64 #include "interactive.h"
65 #include "dirs.h"
66 #include "tape.h"
67 #include "symtab.h"
68 #include "utilities.h"
69
70 #ifdef USE_QFA
71 #define QFA_MAGIC "495115637697"
72 #define QFA_VERSION "1.0"
73 #endif
74
75 static const char *stdin_opt = NULL;
76
77 #ifdef USE_QFA
78 static const char *gTapeposfile;
79 #endif /* USE_QFA */
80
81 extern char *__progname;
82
83 static void obsolete (int *, char **[]);
84 static void usage (void);
85 static void use_stdin (const char *);
86
87 #define FORCED_UMASK (077)
88
89 int
90 main(int argc, char *argv[])
91 {
92 int ch;
93 dump_ino_t ino;
94 const char *inputdev = _PATH_DEFTAPE;
95 const char *symtbl = "./restoresymtable";
96 const char *p;
97 char name[MAXPATHLEN];
98 FILE *filelist = NULL;
99 char fname[MAXPATHLEN];
100 mode_t orig_umask;
101 time_t tistart, tiend, titaken;
102 #ifdef USE_QFA
103 tapeposflag = 0;
104 createtapeposflag = 0;
105 #endif /* USE_QFA */
106 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
107 char transselinuxopt = 0;
108 #endif
109
110 msgprefix = "RESTORE";
111 /* Temp files should *not* be readable. We set permissions later. */
112 orig_umask = umask(FORCED_UMASK);
113 filesys[0] = '\0';
114 __progname = argv[0];
115
116 if (argc < 2)
117 usage();
118
119 if ((inputdev = getenv("TAPE")) == NULL)
120 inputdev = _PATH_DEFTAPE;
121 if ((tmpdir = getenv("TMPDIR")) == NULL)
122 tmpdir = strdup(_PATH_TMP);
123 else
124 tmpdir = strdup(tmpdir);
125 if (tmpdir == NULL)
126 err(1, "malloc tmpdir");
127 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
128 ;
129 obsolete(&argc, &argv);
130
131 int lopt_value = 0;
132 struct option lopts [] = {
133 { .name = "ignore-compression-flag-for-0.4b45-dumps", .has_arg = no_argument, .flag = &lopt_value, .val = 1 },
134 { .name = "no-set-curr-dir-perms", .has_arg = no_argument, .flag = &lopt_value, .val = 2 },
135
136 { .name = "archive-file", .has_arg = required_argument, .flag = NULL, .val = 'A' },
137 { .name = "no-ask-volume", .has_arg = no_argument, .flag = NULL, .val = 'a' },
138 { .name = "block-size", .has_arg = required_argument, .flag = NULL, .val = 'b' },
139 { .name = "old-format", .has_arg = no_argument, .flag = NULL, .val = 'c' },
140 { .name = "debug", .has_arg = optional_argument, .flag = NULL, .val = 'd' },
141 { .name = "filesystem-name", .has_arg = required_argument, .flag = NULL, .val = 'D' },
142 { .name = "input-file", .has_arg = required_argument, .flag = NULL, .val = 'f' },
143 { .name = "sot-script", .has_arg = required_argument, .flag = NULL, .val = 'F' },
144 { .name = "no-hierarchical-restore", .has_arg = no_argument, .flag = NULL, .val = 'h' },
145 { .name = "hash-size", .has_arg = required_argument, .flag = NULL, .val = 'H' },
146 { .name = "max-miscompares", .has_arg = required_argument, .flag = NULL, .val = 'L' },
147 { .name = "extract-by-inode", .has_arg = no_argument, .flag = NULL, .val = 'm' },
148 { .name = "multivolume", .has_arg = no_argument, .flag = NULL, .val = 'M' },
149 { .name = "no-write", .has_arg = no_argument, .flag = NULL, .val = 'N' },
150 { .name = "set-curr-dir-perms", .has_arg = no_argument, .flag = NULL, .val = 'o' },
151 { .name = "abort-on-intervention", .has_arg = no_argument, .flag = NULL, .val = 'q' },
152 #ifdef USE_QFA
153 { .name = "QFAfile", .has_arg = required_argument, .flag = NULL, .val = 'Q' },
154 #endif
155 { .name = "read-from-tape-fileno", .has_arg = required_argument, .flag = NULL, .val = 's' },
156 { .name = "tmp-dir", .has_arg = required_argument, .flag = NULL, .val = 'T' },
157 { .name = "unlink-existing", .has_arg = no_argument, .flag = NULL, .val = 'u' },
158 { .name = "verbose", .has_arg = no_argument, .flag = NULL, .val = 'v' },
159 { .name = "multi-volume-non-tape-medium", .has_arg = no_argument, .flag = NULL, .val = 'V' },
160 { .name = "extractlist-file", .has_arg = required_argument, .flag = NULL, .val = 'X' },
161 { .name = "no-abort-on-error", .has_arg = no_argument, .flag = NULL, .val = 'y' },
162 { .name = "zero-terminator", .has_arg = no_argument, .flag = NULL, .val = '0' },
163
164 {0},
165 };
166
167 while ((ch = getopt_long(argc, argv,
168 "aA:b:CcdD:"
169 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
170 "eE:"
171 #endif
172 "f:F:hH:i"
173 "L:mMNo"
174 #ifdef USE_QFA
175 "P:Q:"
176 #endif
177 "qRrs:tT:uvVxX:y0"
178 , lopts, NULL)) != -1)
179 switch(ch) {
180 case 0:
181 switch (lopt_value) {
182 case 1: /* ignore compression flag for 0.4b45 buggy dumps */
183 set_ignoreCompressionFlag();
184 break;
185 case 2: /* no-set-curr-dir-perms */
186 oflag |= FORCENO;
187 break;
188 }
189 break;
190 case 'a':
191 aflag = 1;
192 break;
193 case 'A':
194 Afile = optarg;
195 aflag = 1;
196 break;
197 case 'b': {
198 /* Change default tape blocksize. */
199 char* endp;
200 bflag = 1;
201 ntrec = strtol(optarg, &endp, 10);
202 if (*endp)
203 errx(1, "illegal blocksize -- %s", optarg);
204 if (ntrec <= 0)
205 errx(1, "block size must be greater than 0");
206 break;
207 }
208 case 'c':
209 cvtflag = 1;
210 break;
211 case 'D':
212 strncpy(filesys, optarg, sizeof(filesys));
213 filesys[sizeof(filesys) - 1] = '\0';
214 break;
215 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
216 case 'e':
217 transselinuxflag = 1;
218 transselinuxopt = ch;
219 break;
220 case 'E':
221 transselinuxflag = 1;
222 transselinuxarg = optarg;
223 transselinuxopt = ch;
224 break;
225 #endif
226 case 'T':
227 tmpdir = optarg;
228 break;
229 case 'd':
230 if (optarg) {
231 char* endp;
232 dflag = strtol(optarg, &endp, 16);
233 if (*endp)
234 errx(1, "illegal --debug value -- %s", optarg);
235 }
236 else
237 dflag = 1;
238 break;
239 case 'f':
240 if( !strcmp(optarg,"-") )
241 use_stdin("-f");
242 inputdev = optarg;
243 break;
244 case 'F':
245 bot_script = optarg;
246 break;
247 case 'h':
248 hflag = 0;
249 break;
250 case 'H': {
251 char* endp;
252 dirhash_size = strtol(optarg, &endp, 10);
253 if (*endp)
254 errx(1, "illegal hash size -- %s", optarg);
255 if (dirhash_size < 1)
256 errx(1, "hash size must be greater than 0");
257 break;
258 }
259 case 'C':
260 case 'i':
261 #ifdef USE_QFA
262 case 'P':
263 #endif
264 case 'R':
265 case 'r':
266 case 't':
267 case 'x':
268 if (command != '\0')
269 errx(1,
270 "%c and %c options are mutually exclusive",
271 ch, command);
272 command = ch;
273 #ifdef USE_QFA
274 if (ch == 'P') {
275 Nflag = 1;
276 gTapeposfile = optarg;
277 createtapeposflag = 1;
278 }
279 #endif
280
281 break;
282 case 'L': {
283 char* endp;
284 Lflag = strtol(optarg, &endp, 10);
285 if (*endp)
286 errx(1, "illegal limit -- %s", optarg);
287 if (Lflag < 0)
288 errx(1, "limit must be greater than 0");
289 break;
290 }
291 case 'm':
292 mflag = 0;
293 break;
294 case 'M':
295 Mflag = 1;
296 break;
297 case 'N':
298 Nflag = 1;
299 break;
300 case 'o':
301 oflag |= FORCE;
302 break;
303 case 'q':
304 qflag = 1;
305 break;
306 #ifdef USE_QFA
307 case 'Q':
308 gTapeposfile = optarg;
309 tapeposflag = 1;
310 aflag = 1;
311 break;
312 #endif
313 case 's': {
314 /* Dumpnum (skip to) for multifile dump tapes. */
315 char* endp;
316 dumpnum = strtol(optarg, &endp, 10);
317 if (*endp)
318 errx(1, "illegal dump number -- %s", optarg);
319 if (dumpnum <= 0)
320 errx(1, "dump number must be greater than 0");
321 break;
322 }
323 case 'u':
324 uflag = 1;
325 break;
326 case 'v':
327 vflag = 1;
328 break;
329 case 'V':
330 Vflag = 1;
331 break;
332 case 'X':
333 if( !strcmp(optarg,"-") ) {
334 use_stdin("-X");
335 filelist = stdin;
336 }
337 else
338 if ( !(filelist = fopen(optarg,"r")) )
339 errx(1, "can't open file for reading -- %s", optarg);
340 break;
341 case 'y':
342 yflag = 1;
343 break;
344 case '0':
345 nullflag = 1;
346 break;
347 default:
348 usage();
349 }
350 argc -= optind;
351 argv += optind;
352
353 if (command == '\0')
354 errx(1, "none of C, i, R, r, t or x options specified");
355
356 #ifdef USE_QFA
357 if (!mflag && tapeposflag)
358 errx(1, "m and Q options are mutually exclusive");
359
360 if (tapeposflag && command != 'i' && command != 'x' && command != 't')
361 errx(1, "Q option is not valid for %c command", command);
362 #endif
363
364 if (Afile && command != 'i' && command != 'x' && command != 't')
365 errx(1, "A option is not valid for %c command", command);
366
367 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
368 if (transselinuxflag && !strchr("CirRx", command))
369 errx(1, "%c option is not valid for %c command", transselinuxopt, command);
370 #endif
371
372 if (signal(SIGINT, onintr) == SIG_IGN)
373 (void) signal(SIGINT, SIG_IGN);
374 if (signal(SIGTERM, onintr) == SIG_IGN)
375 (void) signal(SIGTERM, SIG_IGN);
376 setlinebuf(stderr);
377
378 atexit(cleanup);
379
380 if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
381 && !strchr(inputdev, ':')) {
382 /* since we chdir into the directory we are comparing
383 * to, we must retain the full tape path */
384 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
385 if (!getcwd(wd, MAXPATHLEN))
386 err(1, "can't get current directory");
387 size_t len = snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
388 if (len >= MAXPATHLEN)
389 err(1, "%s/%s is more than %d characters", wd, inputdev, MAXPATHLEN);
390 fullpathinput[MAXPATHLEN - 1] = '\0';
391 setinput(fullpathinput);
392 }
393 else
394 setinput(inputdev);
395
396 wdfd = open(".", O_RDONLY);
397 if (wdfd < 0)
398 err(1, "can't get current directory");
399
400 if (argc == 0 && !filelist) {
401 argc = 1;
402 static char dot[] = ".";
403 *--argv = dot;
404 }
405
406 #ifdef USE_QFA
407 if (tapeposflag) {
408 msg("reading QFA positions from %s\n", gTapeposfile);
409 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
410 errx(1, "can't open file for reading -- %s",
411 gTapeposfile);
412 /* start reading header info */
413 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
414 errx(1, "not requested format of -- %s", gTapeposfile);
415 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
416 if (strcmp(gTps, QFA_MAGIC) != 0)
417 errx(1, "not requested format of -- %s", gTapeposfile);
418 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
419 errx(1, "not requested format of -- %s", gTapeposfile);
420 gTps[strlen(gTps) - 1] = 0;
421 if (strcmp(gTps, QFA_VERSION) != 0)
422 errx(1, "not requested format of -- %s", gTapeposfile);
423 /* read dumpdate */
424 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
425 errx(1, "not requested format of -- %s", gTapeposfile);
426 gTps[strlen(gTps) - 1] = 0;
427 qfadumpdate = atol(gTps);
428 /* read empty line */
429 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
430 errx(1, "not requested format of -- %s", gTapeposfile);
431 gTps[strlen(gTps) - 1] = 0;
432 /* read table header line */
433 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
434 errx(1, "not requested format of -- %s", gTapeposfile);
435 gTps[strlen(gTps) - 1] = 0;
436 /* end reading header info */
437 /* tape position table starts here */
438 gSeekstart = ftell(gTapeposfp); /* remember for later use */
439 }
440 #endif /* USE_QFA */
441
442 switch (command) {
443 /*
444 * Compare contents of tape.
445 */
446 case 'C': {
447 struct stat stbuf;
448
449 Vmsg("Begin compare restore\n");
450 compare_ignore_not_found = 0;
451 compare_errors = 0;
452 Nflag = 1;
453 aflag = 1;
454 setup();
455 printf("filesys = %s\n", filesys);
456 if (stat(filesys, &stbuf) < 0)
457 err(1, "cannot stat directory %s", filesys);
458 if (chdir(filesys) < 0)
459 err(1, "cannot cd to %s", filesys);
460 compare_ignore_not_found = dumptime > 0;
461 initsymtable((char *)0);
462 extractdirs(1);
463 treescan(".", ROOTINO, nodeupdates);
464 compareleaves();
465 comparedirmodes();
466 checkrestore();
467 if (compare_errors) {
468 printf("Some files were modified! %d compare errors\n", compare_errors);
469 exit(2);
470 }
471 break;
472 }
473
474 /*
475 * Interactive mode.
476 */
477 case 'i':
478 setup();
479 extractdirs(1);
480 initsymtable(NULL);
481 runcmdshell();
482 break;
483 /*
484 * Incremental restoration of a file system.
485 */
486 case 'r':
487 aflag = 1; /* in -r or -R mode, -a is default */
488 setup();
489 if (dumptime > 0) {
490 /*
491 * This is an incremental dump tape.
492 */
493 Vmsg("Begin incremental restore\n");
494 initsymtable(symtbl);
495 extractdirs(1);
496 removeoldleaves();
497 Vmsg("Calculate node updates.\n");
498 treescan(".", ROOTINO, nodeupdates);
499 findunreflinks();
500 removeoldnodes();
501 } else {
502 /*
503 * This is a level zero dump tape.
504 */
505 Vmsg("Begin level 0 restore\n");
506 initsymtable((char *)0);
507 extractdirs(1);
508 Vmsg("Calculate extraction list.\n");
509 treescan(".", ROOTINO, nodeupdates);
510 }
511 createleaves(symtbl);
512 createlinks();
513 setdirmodes(FORCE);
514 checkrestore();
515 if (dflag) {
516 Vmsg("Verify the directory structure\n");
517 treescan(".", ROOTINO, verifyfile);
518 }
519 dumpsymtable(symtbl, 1);
520 break;
521 /*
522 * Resume an incremental file system restoration.
523 */
524 case 'R':
525 aflag = 1; /* in -r or -R mode, -a is default */
526 initsymtable(symtbl);
527 skipmaps();
528 skipdirs();
529 createleaves(symtbl);
530 createlinks();
531 setdirmodes(FORCE);
532 checkrestore();
533 dumpsymtable(symtbl, 1);
534 break;
535
536 /* handle file names from either text file (-X) or the command line */
537 #define NEXTFILE(p) \
538 p = NULL; \
539 if (argc) { \
540 --argc; \
541 p = *argv++; \
542 } \
543 else if (filelist) { \
544 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
545 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
546 *(p + strlen(p) - 1) = '\0'; \
547 if ( !*p ) /* skip empty lines */ \
548 continue; \
549 } \
550 }
551
552 /*
553 * List contents of tape.
554 */
555 case 't':
556 setup();
557 extractdirs(0);
558 initsymtable((char *)0);
559 printvolinfo();
560 for (;;) {
561 char *ptr;
562 NEXTFILE(ptr);
563 if (!ptr)
564 break;
565 canon(ptr, name, sizeof(name));
566 ino = dirlookup(name);
567 if (ino == 0)
568 continue;
569 if (nullflag)
570 fprintf(stdout, "%c", 0);
571 treescan(name, ino, listfile);
572 }
573 break;
574 /*
575 * Batch extraction of tape contents.
576 */
577 case 'x':
578 tistart = time(NULL);
579 setup();
580 extractdirs(1);
581 initsymtable((char *)0);
582 for (;;) {
583 char *ptr;
584 NEXTFILE(ptr);
585 if (!ptr)
586 break;
587 canon(ptr, name, sizeof(name));
588 ino = dirlookup(name);
589 if (ino == 0)
590 continue;
591 if (mflag)
592 pathcheck(name);
593 treescan(name, ino, addfile);
594 }
595 createfiles();
596 createlinks();
597 setdirmodes(oflag);
598 if (dflag)
599 checkrestore();
600 tiend = time(NULL);
601 titaken = tiend - tistart;
602 Dmsg(DEBUG_QFA, "restore took %jd:%02jd:%02jd\n", (intmax_t)titaken / 3600,
603 ((intmax_t)titaken % 3600) / 60, (intmax_t)titaken % 60);
604 break;
605 #ifdef USE_QFA
606 case 'P':
607 tistart = time(NULL);
608 setup();
609 msg("writing QFA positions to %s\n", gTapeposfile);
610 (void) umask(orig_umask);
611 if ((gTapeposfd = open(gTapeposfile, O_WRONLY|O_CREAT|O_TRUNC,
612 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
613 |S_IROTH|S_IWOTH)) < 0)
614 errx(1, "can't create tapeposfile\n");
615 (void) umask(FORCED_UMASK);
616 /* print QFA-file header */
617 sprintf(gTps, "%s\n%s\n%" PRIu32 "\n\n", QFA_MAGIC, QFA_VERSION, spcl.c_date);
618 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
619 errx(1, "can't write tapeposfile\n");
620 sprintf(gTps, "ino\ttapeno\ttapepos\n");
621 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
622 errx(1, "can't write tapeposfile\n");
623
624 extractdirs(1);
625 initsymtable((char *)0);
626 for (;;) {
627 char *ptr;
628 NEXTFILE(ptr);
629 if (!ptr)
630 break;
631 canon(ptr, name, sizeof(name));
632 ino = dirlookup(name);
633 if (ino == 0)
634 continue;
635 if (mflag)
636 pathcheck(name);
637 treescan(name, ino, addfile);
638 }
639 createfiles();
640 tiend = time(NULL);
641 titaken = tiend - tistart;
642 Dmsg(DEBUG_QFA, "writing QFA positions took %jd:%02jd:%02jd\n", (intmax_t)titaken / 3600,
643 ((intmax_t)titaken % 3600) / 60, (intmax_t)titaken % 60);
644 break;
645 #endif /* USE_QFA */
646 }
647 freesymtable();
648 exit(0);
649 /* NOTREACHED */
650 return 0; /* gcc shut up */
651 }
652
653 static void
654 usage(void)
655 {
656 char white[MAXPATHLEN];
657 const char *ext2ver, *ext2date;
658
659 memset(white, ' ', MAXPATHLEN);
660 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
661
662 ext2fs_get_library_version(&ext2ver, &ext2date);
663 (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
664 __progname, _DUMP_VERSION, ext2ver, ext2date);
665
666 #ifdef USE_QFA
667 #define qfaflag "[-Q file] "
668 #else
669 #define qfaflag
670 #endif
671
672 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
673 # define tseflag "e"
674 # define tsEflag "[-E mls] "
675 #else
676 # define tseflag
677 # define tsEflag
678 #endif
679 fprintf(stderr,
680 "usage:"
681 "\t%s -C [-cd" tseflag "HMqvVy] [-b blocksize] [-D filesystem] " tsEflag"\n"
682 "\t%s [-f file] [-F script] [-L limit] [-s fileno]\n"
683 "\t%s -i [-acd" tseflag "hHmMoquvVy] [-A file] [-b blocksize] " tsEflag"\n"
684 "\t%s [-f file] [-F script] " qfaflag "[-s fileno]\n"
685 #ifdef USE_QFA
686 "\t%s -P file [-acdhHmMquvVy] [-b blocksize]\n"
687 "\t%s [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
688 #endif
689 "\t%s -r [-cd" tseflag "HMquvVy] [-b blocksize] " tsEflag"\n"
690 "\t%s [-f file] [-F script] [-s fileno] [-T directory]\n"
691 "\t%s -R [-cd" tseflag "HMquvVy] [-b blocksize] " tsEflag"\n"
692 "\t%s [-f file] [-F script] [-s fileno] [-T directory]\n"
693 "\t%s -t [-cdhHMquvVy0] [-A file] [-b blocksize]\n"
694 "\t%s [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
695 "\t%s -x [-acd" tseflag "hHmMoquvVy] [-A file] [-b blocksize] " tsEflag"\n"
696 "\t%s [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
697 __progname, white,
698 __progname, white,
699 #ifdef USE_QFA
700 __progname, white,
701 #endif
702 __progname, white,
703 __progname, white,
704 __progname, white,
705 __progname, white);
706 exit(1);
707 }
708
709 /*
710 * obsolete --
711 * Change set of key letters and ordered arguments into something
712 * getopt(3) will like.
713 */
714 static void
715 obsolete(int *argcp, char **argvp[])
716 {
717 int argc, flags;
718 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
719
720 /* Setup. */
721 argv = *argvp;
722 argc = *argcp;
723
724 /* Return if no arguments or first argument has leading dash. */
725 ap = argv[1];
726 if (argc == 1 || *ap == '-')
727 return;
728
729 /* Allocate space for new arguments. */
730 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
731 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
732 err(1, "malloc args");
733
734 *nargv++ = *argv;
735 argv += 2, argc -= 2;
736
737 for (flags = 0; *ap; ++ap) {
738 switch (*ap) {
739 case 'A':
740 case 'b':
741 case 'D':
742 case 'f':
743 case 'F':
744 case 'H':
745 case 'L':
746 case 'Q':
747 case 's':
748 case 'T':
749 case 'X':
750 if (*argv == NULL) {
751 warnx("option requires an argument -- %c", *ap);
752 usage();
753 }
754 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
755 err(1, "malloc arg");
756 nargv[0][0] = '-';
757 nargv[0][1] = *ap;
758 (void)strcpy(&nargv[0][2], *argv);
759 ++argv;
760 ++nargv;
761 break;
762 default:
763 if (!flags) {
764 *p++ = '-';
765 flags = 1;
766 }
767 *p++ = *ap;
768 break;
769 }
770 }
771
772 /* Terminate flags. */
773 if (flags) {
774 *p = '\0';
775 *nargv++ = flagsp;
776 }
777
778 /* Copy remaining arguments. */
779 while ((*nargv++ = *argv++));
780
781 /* Update argument count. */
782 *argcp = nargv - *argvp - 1;
783 }
784
785 /*
786 * use_stdin --
787 * reserve stdin for opt (avoid conflicts)
788 */
789 void
790 use_stdin(const char *opt)
791 {
792 if (stdin_opt)
793 errx(1, "can't handle standard input for both %s and %s",
794 stdin_opt, opt);
795 stdin_opt = opt;
796 }