]> git.wh0rd.org - dump.git/blob - restore/main.c
0f43cb7e1de0f64d4f7637d1dce2032708aa8054
[dump.git] / restore / main.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - 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 #ifndef lint
39 static const char rcsid[] =
40 "$Id: main.c,v 1.48 2005/01/13 15:41:06 stelian Exp $";
41 #endif /* not lint */
42
43 #include <config.h>
44 #include <compatlfs.h>
45 #include <sys/types.h>
46 #include <fcntl.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #include <errno.h>
50
51 #ifdef __linux__
52 #include <sys/time.h>
53 #include <time.h>
54 #ifdef HAVE_EXT2FS_EXT2_FS_H
55 #include <ext2fs/ext2_fs.h>
56 #else
57 #include <linux/ext2_fs.h>
58 #endif
59 #include <bsdcompat.h>
60 #include <signal.h>
61 #include <string.h>
62 #else /* __linux__ */
63 #ifdef sunos
64 #include <signal.h>
65 #include <string.h>
66 #include <sys/fcntl.h>
67 #include <bsdcompat.h>
68 #include <sys/mtio.h>
69 #else
70 #include <ufs/ufs/dinode.h>
71 #endif
72 #endif /* __linux__ */
73 #include <protocols/dumprestore.h>
74
75 #include <compaterr.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <unistd.h>
79
80 #ifdef __linux__
81 #include <ext2fs/ext2fs.h>
82 #include <getopt.h>
83 #endif
84
85 #include "pathnames.h"
86 #include "restore.h"
87 #include "extern.h"
88
89 int abortifconnerr = 1; /* set to 1 if lib dumprmt.o should exit on connection errors
90 otherwise just print a message using msg */
91
92 int aflag = 0, bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
93 int hflag = 1, mflag = 1, Mflag = 0, Nflag = 0, Vflag = 0, zflag = 0;
94 int uflag = 0, lflag = 0, Lflag = 0, oflag = 0;
95 int ufs2flag = 0;
96 char *Afile = NULL;
97 int dokerberos = 0;
98 char command = '\0';
99 long dumpnum = 1;
100 long volno = 0;
101 long ntrec;
102 char *dumpmap = NULL;
103 char *usedinomap = NULL;
104 dump_ino_t maxino;
105 time_t dumptime;
106 time_t dumpdate;
107 FILE *terminal;
108 char *tmpdir;
109 int compare_ignore_not_found;
110 int compare_errors;
111 char filesys[NAMELEN];
112 static const char *stdin_opt = NULL;
113 char *bot_script = NULL;
114 dump_ino_t volinfo[TP_NINOS];
115 int wdfd;
116
117 #ifdef USE_QFA
118 FILE *gTapeposfp;
119 char *gTapeposfile;
120 char gTps[255];
121 long gSeekstart;
122 int tapeposflag;
123 int gTapeposfd;
124 int createtapeposflag;
125 unsigned long qfadumpdate;
126 long long curtapepos;
127 #endif /* USE_QFA */
128
129 long smtc_errno;
130
131 #if defined(__linux__) || defined(sunos)
132 char *__progname;
133 #endif
134
135 static void obsolete __P((int *, char **[]));
136 static void usage __P((void));
137 static void use_stdin __P((const char *));
138
139 #define FORCED_UMASK (077)
140
141 int
142 main(int argc, char *argv[])
143 {
144 int ch;
145 dump_ino_t ino;
146 char *inputdev = _PATH_DEFTAPE;
147 char *symtbl = "./restoresymtable";
148 char *p, name[MAXPATHLEN];
149 FILE *filelist = NULL;
150 char fname[MAXPATHLEN];
151 mode_t orig_umask;
152 #ifdef DEBUG_QFA
153 time_t tistart, tiend, titaken;
154 #endif
155 #ifdef USE_QFA
156 tapeposflag = 0;
157 createtapeposflag = 0;
158 #endif /* USE_QFA */
159
160 /* Temp files should *not* be readable. We set permissions later. */
161 orig_umask = umask(FORCED_UMASK);
162 filesys[0] = '\0';
163 #if defined(__linux__) || defined(sunos)
164 __progname = argv[0];
165 #endif
166
167 if (argc < 2)
168 usage();
169
170 if ((inputdev = getenv("TAPE")) == NULL)
171 inputdev = _PATH_DEFTAPE;
172 if ((tmpdir = getenv("TMPDIR")) == NULL)
173 tmpdir = _PATH_TMP;
174 if ((tmpdir = strdup(tmpdir)) == NULL)
175 err(1, "malloc tmpdir");
176 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
177 ;
178 obsolete(&argc, &argv);
179 while ((ch = getopt(argc, argv,
180 "aA:b:CcdD:f:F:hi"
181 #ifdef KERBEROS
182 "k"
183 #endif
184 "lL:mMNo"
185 #ifdef USE_QFA
186 "P:Q:"
187 #endif
188 "Rrs:tT:uvVxX:y")) != -1)
189 switch(ch) {
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 bflag = 1;
200 ntrec = strtol(optarg, &p, 10);
201 if (*p)
202 errx(1, "illegal blocksize -- %s", optarg);
203 if (ntrec <= 0)
204 errx(1, "block size must be greater than 0");
205 break;
206 case 'c':
207 cvtflag = 1;
208 break;
209 case 'D':
210 strncpy(filesys, optarg, NAMELEN);
211 filesys[NAMELEN - 1] = '\0';
212 break;
213 case 'T':
214 tmpdir = optarg;
215 break;
216 case 'd':
217 dflag = 1;
218 break;
219 case 'f':
220 if( !strcmp(optarg,"-") )
221 use_stdin("-f");
222 inputdev = optarg;
223 break;
224 case 'F':
225 bot_script = optarg;
226 break;
227 case 'h':
228 hflag = 0;
229 break;
230 #ifdef KERBEROS
231 case 'k':
232 dokerberos = 1;
233 break;
234 #endif
235 case 'C':
236 case 'i':
237 #ifdef USE_QFA
238 case 'P':
239 #endif
240 case 'R':
241 case 'r':
242 case 't':
243 case 'x':
244 if (command != '\0')
245 errx(1,
246 "%c and %c options are mutually exclusive",
247 ch, command);
248 command = ch;
249 #ifdef USE_QFA
250 if (ch == 'P') {
251 gTapeposfile = optarg;
252 createtapeposflag = 1;
253 }
254 #endif
255
256 break;
257 case 'l':
258 lflag = 1;
259 break;
260 case 'L':
261 Lflag = strtol(optarg, &p, 10);
262 if (*p)
263 errx(1, "illegal limit -- %s", optarg);
264 if (Lflag < 0)
265 errx(1, "limit must be greater than 0");
266 break;
267 case 'm':
268 mflag = 0;
269 break;
270 case 'M':
271 Mflag = 1;
272 break;
273 case 'N':
274 Nflag = 1;
275 break;
276 case 'o':
277 oflag = 1;
278 break;
279 #ifdef USE_QFA
280 case 'Q':
281 gTapeposfile = optarg;
282 tapeposflag = 1;
283 aflag = 1;
284 break;
285 #endif
286 case 's':
287 /* Dumpnum (skip to) for multifile dump tapes. */
288 dumpnum = strtol(optarg, &p, 10);
289 if (*p)
290 errx(1, "illegal dump number -- %s", optarg);
291 if (dumpnum <= 0)
292 errx(1, "dump number must be greater than 0");
293 break;
294 case 'u':
295 uflag = 1;
296 break;
297 case 'v':
298 vflag = 1;
299 break;
300 case 'V':
301 Vflag = 1;
302 break;
303 case 'X':
304 if( !strcmp(optarg,"-") ) {
305 use_stdin("-X");
306 filelist = stdin;
307 }
308 else
309 if ( !(filelist = fopen(optarg,"r")) )
310 errx(1, "can't open file for reading -- %s", optarg);
311 break;
312 case 'y':
313 yflag = 1;
314 break;
315 default:
316 usage();
317 }
318 argc -= optind;
319 argv += optind;
320
321 if (command == '\0')
322 errx(1, "none of C, i, R, r, t or x options specified");
323
324 #ifdef USE_QFA
325 if (!mflag && tapeposflag)
326 errx(1, "m and Q options are mutually exclusive");
327
328 if (tapeposflag && command != 'i' && command != 'x' && command != 't')
329 errx(1, "Q option is not valid for %c command", command);
330 #endif
331
332 if (Afile && command != 'i' && command != 'x' && command != 't')
333 errx(1, "A option is not valid for %c command", command);
334
335 if (signal(SIGINT, onintr) == SIG_IGN)
336 (void) signal(SIGINT, SIG_IGN);
337 if (signal(SIGTERM, onintr) == SIG_IGN)
338 (void) signal(SIGTERM, SIG_IGN);
339 setlinebuf(stderr);
340
341 atexit(cleanup);
342
343 if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
344 #ifdef RRESTORE
345 && !strchr(inputdev, ':')
346 #endif
347 ) {
348 /* since we chdir into the directory we are comparing
349 * to, we must retain the full tape path */
350 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
351 if (!getcwd(wd, MAXPATHLEN))
352 err(1, "can't get current directory");
353 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
354 fullpathinput[MAXPATHLEN - 1] = '\0';
355 setinput(fullpathinput);
356 }
357 else
358 setinput(inputdev);
359
360 wdfd = open(".", O_RDONLY);
361 if (wdfd < 0)
362 err(1, "can't get current directory");
363
364 if (argc == 0 && !filelist) {
365 argc = 1;
366 *--argv = ".";
367 }
368
369 #ifdef USE_QFA
370 if (tapeposflag) {
371 msg("reading QFA positions from %s\n", gTapeposfile);
372 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
373 errx(1, "can't open file for reading -- %s",
374 gTapeposfile);
375 /* start reading header info */
376 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
377 errx(1, "not requested format of -- %s", gTapeposfile);
378 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
379 if (strcmp(gTps, QFA_MAGIC) != 0)
380 errx(1, "not requested format of -- %s", gTapeposfile);
381 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
382 errx(1, "not requested format of -- %s", gTapeposfile);
383 gTps[strlen(gTps) - 1] = 0;
384 if (strcmp(gTps, QFA_VERSION) != 0)
385 errx(1, "not requested format of -- %s", gTapeposfile);
386 /* read dumpdate */
387 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
388 errx(1, "not requested format of -- %s", gTapeposfile);
389 gTps[strlen(gTps) - 1] = 0;
390 qfadumpdate = atol(gTps);
391 /* read empty line */
392 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
393 errx(1, "not requested format of -- %s", gTapeposfile);
394 gTps[strlen(gTps) - 1] = 0;
395 /* read table header line */
396 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
397 errx(1, "not requested format of -- %s", gTapeposfile);
398 gTps[strlen(gTps) - 1] = 0;
399 /* end reading header info */
400 /* tape position table starts here */
401 gSeekstart = ftell(gTapeposfp); /* remember for later use */
402 #ifdef sunos
403 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
404 errx(1, "can't get SCSI-ID for %s\n", inputdev);
405 }
406 if (scsiid < 0) {
407 errx(1, "can't get SCSI-ID for %s\n", inputdev);
408 }
409 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
410 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
411 errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
412 }
413 #endif
414 }
415 #endif /* USE_QFA */
416
417 switch (command) {
418 /*
419 * Compare contents of tape.
420 */
421 case 'C': {
422 struct STAT stbuf;
423
424 Vprintf(stdout, "Begin compare restore\n");
425 compare_ignore_not_found = 0;
426 compare_errors = 0;
427 setup();
428 printf("filesys = %s\n", filesys);
429 if (STAT(filesys, &stbuf) < 0)
430 err(1, "cannot stat directory %s", filesys);
431 if (chdir(filesys) < 0)
432 err(1, "cannot cd to %s", filesys);
433 compare_ignore_not_found = dumptime > 0;
434 initsymtable((char *)0);
435 extractdirs(1);
436 treescan(".", ROOTINO, nodeupdates);
437 compareleaves();
438 comparedirmodes();
439 checkrestore();
440 if (compare_errors) {
441 printf("Some files were modified!\n");
442 exit(2);
443 }
444 break;
445 }
446
447 /*
448 * Interactive mode.
449 */
450 case 'i':
451 setup();
452 extractdirs(1);
453 initsymtable(NULL);
454 runcmdshell();
455 break;
456 /*
457 * Incremental restoration of a file system.
458 */
459 case 'r':
460 aflag = 1; /* in -r or -R mode, -a is default */
461 setup();
462 if (dumptime > 0) {
463 /*
464 * This is an incremental dump tape.
465 */
466 Vprintf(stdout, "Begin incremental restore\n");
467 initsymtable(symtbl);
468 extractdirs(1);
469 removeoldleaves();
470 Vprintf(stdout, "Calculate node updates.\n");
471 treescan(".", ROOTINO, nodeupdates);
472 findunreflinks();
473 removeoldnodes();
474 } else {
475 /*
476 * This is a level zero dump tape.
477 */
478 Vprintf(stdout, "Begin level 0 restore\n");
479 initsymtable((char *)0);
480 extractdirs(1);
481 Vprintf(stdout, "Calculate extraction list.\n");
482 treescan(".", ROOTINO, nodeupdates);
483 }
484 createleaves(symtbl);
485 createlinks();
486 setdirmodes(FORCE);
487 checkrestore();
488 if (dflag) {
489 Vprintf(stdout, "Verify the directory structure\n");
490 treescan(".", ROOTINO, verifyfile);
491 }
492 dumpsymtable(symtbl, (long)1);
493 break;
494 /*
495 * Resume an incremental file system restoration.
496 */
497 case 'R':
498 aflag = 1; /* in -r or -R mode, -a is default */
499 initsymtable(symtbl);
500 skipmaps();
501 skipdirs();
502 createleaves(symtbl);
503 createlinks();
504 setdirmodes(FORCE);
505 checkrestore();
506 dumpsymtable(symtbl, (long)1);
507 break;
508
509 /* handle file names from either text file (-X) or the command line */
510 #define NEXTFILE(p) \
511 p = NULL; \
512 if (argc) { \
513 --argc; \
514 p = *argv++; \
515 } \
516 else if (filelist) { \
517 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
518 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
519 *(p + strlen(p) - 1) = '\0'; \
520 if ( !*p ) /* skip empty lines */ \
521 continue; \
522 } \
523 }
524
525 /*
526 * List contents of tape.
527 */
528 case 't':
529 setup();
530 extractdirs(0);
531 initsymtable((char *)0);
532 printvolinfo();
533 for (;;) {
534 NEXTFILE(p);
535 if (!p)
536 break;
537 canon(p, name, sizeof(name));
538 ino = dirlookup(name);
539 if (ino == 0)
540 continue;
541 treescan(name, ino, listfile);
542 }
543 break;
544 /*
545 * Batch extraction of tape contents.
546 */
547 case 'x':
548 #ifdef DEBUG_QFA
549 tistart = time(NULL);
550 #endif
551 setup();
552 extractdirs(1);
553 initsymtable((char *)0);
554 for (;;) {
555 NEXTFILE(p);
556 if (!p)
557 break;
558 canon(p, name, sizeof(name));
559 ino = dirlookup(name);
560 if (ino == 0)
561 continue;
562 if (mflag)
563 pathcheck(name);
564 treescan(name, ino, addfile);
565 }
566 createfiles();
567 createlinks();
568 setdirmodes(oflag ? FORCE : 0);
569 if (dflag)
570 checkrestore();
571 #ifdef sunos
572 if (fdsmtc != -1) {
573 close(fdsmtc);
574 }
575 #endif /* sunos */
576 #ifdef DEBUG_QFA
577 tiend = time(NULL);
578 titaken = tiend - tistart;
579 msg("restore took %d:%02d:%02d\n", titaken / 3600,
580 (titaken % 3600) / 60, titaken % 60);
581 #endif /* DEBUG_QFA */
582 break;
583 #ifdef USE_QFA
584 case 'P':
585 #ifdef DEBUG_QFA
586 tistart = time(NULL);
587 #endif
588 #ifdef sunos
589 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
590 errx(1, "can't get SCSI-ID for %s\n", inputdev);
591 }
592 if (scsiid < 0) {
593 errx(1, "can't get SCSI-ID for %s\n", inputdev);
594 }
595 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
596 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
597 errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
598 }
599 #endif /* sunos */
600 setup();
601 msg("writing QFA positions to %s\n", gTapeposfile);
602 (void) umask(orig_umask);
603 if ((gTapeposfd = open(gTapeposfile, O_WRONLY|O_CREAT|O_TRUNC,
604 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
605 |S_IROTH|S_IWOTH)) < 0)
606 errx(1, "can't create tapeposfile\n");
607 (void) umask(FORCED_UMASK);
608 /* print QFA-file header */
609 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
610 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
611 errx(1, "can't write tapeposfile\n");
612 sprintf(gTps, "ino\ttapeno\ttapepos\n");
613 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
614 errx(1, "can't write tapeposfile\n");
615
616 extractdirs(1);
617 initsymtable((char *)0);
618 for (;;) {
619 NEXTFILE(p);
620 if (!p)
621 break;
622 canon(p, name, sizeof(name));
623 ino = dirlookup(name);
624 if (ino == 0)
625 continue;
626 if (mflag)
627 pathcheck(name);
628 treescan(name, ino, addfile);
629 }
630 createfiles();
631 #ifdef sunos
632 if (fdsmtc != -1) {
633 close(fdsmtc);
634 }
635 #endif /* sunos */
636 #ifdef DEBUG_QFA
637 tiend = time(NULL);
638 titaken = tiend - tistart;
639 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600,
640 (titaken % 3600) / 60, titaken % 60);
641 #endif /* DEBUG_QFA */
642 break;
643 #endif /* USE_QFA */
644 }
645 exit(0);
646 /* NOTREACHED */
647 return 0; /* gcc shut up */
648 }
649
650 static void
651 usage(void)
652 {
653 char white[MAXPATHLEN];
654 const char *ext2ver, *ext2date;
655
656 memset(white, ' ', MAXPATHLEN);
657 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
658
659 #ifdef __linux__
660 ext2fs_get_library_version(&ext2ver, &ext2date);
661 (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
662 __progname, _DUMP_VERSION, ext2ver, ext2date);
663 #else
664 (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
665 #endif
666
667 #ifdef KERBEROS
668 #define kerbflag "k"
669 #else
670 #define kerbflag
671 #endif
672
673 #ifdef USE_QFA
674 #define qfaflag "[-Q file] "
675 #else
676 #define qfaflag
677 #endif
678
679 fprintf(stderr,
680 "usage:"
681 "\t%s -C [-cd" kerbflag "lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
682 "\t%s [-F script] [-L limit] [-s fileno]\n"
683 "\t%s -i [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
684 "\t%s [-F script] " qfaflag "[-s fileno]\n"
685 #ifdef USE_QFA
686 "\t%s -P file [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize]\n"
687 "\t%s [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
688 #endif
689 "\t%s -r [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
690 "\t%s [-s fileno] [-T directory]\n"
691 "\t%s -R [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
692 "\t%s [-s fileno] [-T directory]\n"
693 "\t%s -t [-cdh" kerbflag "lMuvVy] [-A file] [-b blocksize] [-f file]\n"
694 "\t%s [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
695 "\t%s -x [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
696 "\t%s [-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 'L':
745 case 'Q':
746 case 's':
747 case 'T':
748 case 'X':
749 if (*argv == NULL) {
750 warnx("option requires an argument -- %c", *ap);
751 usage();
752 }
753 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
754 err(1, "malloc arg");
755 nargv[0][0] = '-';
756 nargv[0][1] = *ap;
757 (void)strcpy(&nargv[0][2], *argv);
758 ++argv;
759 ++nargv;
760 break;
761 default:
762 if (!flags) {
763 *p++ = '-';
764 flags = 1;
765 }
766 *p++ = *ap;
767 break;
768 }
769 }
770
771 /* Terminate flags. */
772 if (flags) {
773 *p = '\0';
774 *nargv++ = flagsp;
775 }
776
777 /* Copy remaining arguments. */
778 while ((*nargv++ = *argv++));
779
780 /* Update argument count. */
781 *argcp = nargv - *argvp - 1;
782 }
783
784 /*
785 * use_stdin --
786 * reserve stdin for opt (avoid conflicts)
787 */
788 void
789 use_stdin(const char *opt)
790 {
791 if (stdin_opt)
792 errx(1, "can't handle standard input for both %s and %s",
793 stdin_opt, opt);
794 stdin_opt = opt;
795 }