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