Imported Debian patch 0.4b40-1
[debian/dump] / 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.49 2005/01/14 13:04:56 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                 Nflag = 1;
428                 setup();
429                 printf("filesys = %s\n", filesys);
430                 if (STAT(filesys, &stbuf) < 0)
431                         err(1, "cannot stat directory %s", filesys);
432                 if (chdir(filesys) < 0)
433                         err(1, "cannot cd to %s", filesys);
434                 compare_ignore_not_found = dumptime > 0;
435                 initsymtable((char *)0);
436                 extractdirs(1);
437                 treescan(".", ROOTINO, nodeupdates);
438                 compareleaves();
439                 comparedirmodes();
440                 checkrestore();
441                 if (compare_errors) {
442                         printf("Some files were modified!\n");
443                         exit(2);
444                 }
445                 break;
446         }
447
448         /*
449          * Interactive mode.
450          */
451         case 'i':
452                 setup();
453                 extractdirs(1);
454                 initsymtable(NULL);
455                 runcmdshell();
456                 break;
457         /*
458          * Incremental restoration of a file system.
459          */
460         case 'r':
461                 aflag = 1;      /* in -r or -R mode, -a is default */
462                 setup();
463                 if (dumptime > 0) {
464                         /*
465                          * This is an incremental dump tape.
466                          */
467                         Vprintf(stdout, "Begin incremental restore\n");
468                         initsymtable(symtbl);
469                         extractdirs(1);
470                         removeoldleaves();
471                         Vprintf(stdout, "Calculate node updates.\n");
472                         treescan(".", ROOTINO, nodeupdates);
473                         findunreflinks();
474                         removeoldnodes();
475                 } else {
476                         /*
477                          * This is a level zero dump tape.
478                          */
479                         Vprintf(stdout, "Begin level 0 restore\n");
480                         initsymtable((char *)0);
481                         extractdirs(1);
482                         Vprintf(stdout, "Calculate extraction list.\n");
483                         treescan(".", ROOTINO, nodeupdates);
484                 }
485                 createleaves(symtbl);
486                 createlinks();
487                 setdirmodes(FORCE);
488                 checkrestore();
489                 if (dflag) {
490                         Vprintf(stdout, "Verify the directory structure\n");
491                         treescan(".", ROOTINO, verifyfile);
492                 }
493                 dumpsymtable(symtbl, (long)1);
494                 break;
495         /*
496          * Resume an incremental file system restoration.
497          */
498         case 'R':
499                 aflag = 1;      /* in -r or -R mode, -a is default */
500                 initsymtable(symtbl);
501                 skipmaps();
502                 skipdirs();
503                 createleaves(symtbl);
504                 createlinks();
505                 setdirmodes(FORCE);
506                 checkrestore();
507                 dumpsymtable(symtbl, (long)1);
508                 break;
509         
510 /* handle file names from either text file (-X) or the command line */
511 #define NEXTFILE(p) \
512         p = NULL; \
513         if (argc) { \
514                 --argc; \
515                 p = *argv++; \
516         } \
517         else if (filelist) { \
518                 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
519                         if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
520                                 *(p + strlen(p) - 1) = '\0'; \
521                         if ( !*p ) /* skip empty lines */ \
522                                 continue; \
523                         } \
524         }
525         
526         /*
527          * List contents of tape.
528          */
529         case 't':
530                 setup();
531                 extractdirs(0);
532                 initsymtable((char *)0);
533                 printvolinfo();
534                 for (;;) {
535                         NEXTFILE(p);
536                         if (!p)
537                                 break;
538                         canon(p, name, sizeof(name));
539                         ino = dirlookup(name);
540                         if (ino == 0)
541                                 continue;
542                         treescan(name, ino, listfile);
543                 }
544                 break;
545         /*
546          * Batch extraction of tape contents.
547          */
548         case 'x':
549 #ifdef DEBUG_QFA
550                 tistart = time(NULL);
551 #endif
552                 setup();
553                 extractdirs(1);
554                 initsymtable((char *)0);
555                 for (;;) {
556                         NEXTFILE(p);
557                         if (!p)
558                                 break;
559                         canon(p, name, sizeof(name));
560                         ino = dirlookup(name);
561                         if (ino == 0)
562                                 continue;
563                         if (mflag)
564                                 pathcheck(name);
565                         treescan(name, ino, addfile);
566                 }
567                 createfiles();
568                 createlinks();
569                 setdirmodes(oflag ? FORCE : 0);
570                 if (dflag)
571                         checkrestore();
572 #ifdef sunos
573                 if (fdsmtc != -1) {
574                         close(fdsmtc);
575                 }
576 #endif /* sunos */
577 #ifdef DEBUG_QFA
578                 tiend = time(NULL);
579                 titaken = tiend - tistart;
580                 msg("restore took %d:%02d:%02d\n", titaken / 3600, 
581                         (titaken % 3600) / 60, titaken % 60);
582 #endif /* DEBUG_QFA */
583                 break;
584 #ifdef USE_QFA
585         case 'P':
586 #ifdef DEBUG_QFA
587                 tistart = time(NULL);
588 #endif
589 #ifdef sunos
590                 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
591                         errx(1, "can't get SCSI-ID for %s\n", inputdev);
592                 }
593                 if (scsiid < 0) {
594                         errx(1, "can't get SCSI-ID for %s\n", inputdev);
595                 }
596                 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
597                 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
598                         errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
599                 }
600 #endif /* sunos */
601                 setup();
602                 msg("writing QFA positions to %s\n", gTapeposfile);
603                 (void) umask(orig_umask);
604                 if ((gTapeposfd = open(gTapeposfile, O_WRONLY|O_CREAT|O_TRUNC,
605                                        S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
606                                        |S_IROTH|S_IWOTH)) < 0)
607                         errx(1, "can't create tapeposfile\n");
608                 (void) umask(FORCED_UMASK);
609                 /* print QFA-file header */
610                 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
611                 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
612                         errx(1, "can't write tapeposfile\n");
613                 sprintf(gTps, "ino\ttapeno\ttapepos\n");
614                 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
615                         errx(1, "can't write tapeposfile\n");
616
617                 extractdirs(1);
618                 initsymtable((char *)0);
619                 for (;;) {
620                         NEXTFILE(p);
621                         if (!p)
622                                 break;
623                         canon(p, name, sizeof(name));
624                         ino = dirlookup(name);
625                         if (ino == 0)
626                                 continue;
627                         if (mflag)
628                                 pathcheck(name);
629                         treescan(name, ino, addfile);
630                 }
631                 createfiles();
632 #ifdef sunos
633                 if (fdsmtc != -1) {
634                         close(fdsmtc);
635                 }
636 #endif /* sunos */
637 #ifdef DEBUG_QFA
638                 tiend = time(NULL);
639                 titaken = tiend - tistart;
640                 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600, 
641                         (titaken % 3600) / 60, titaken % 60);
642 #endif /* DEBUG_QFA */
643                 break;
644 #endif  /* USE_QFA */
645         }
646         exit(0);
647         /* NOTREACHED */
648         return 0;       /* gcc shut up */
649 }
650
651 static void
652 usage(void)
653 {
654         char white[MAXPATHLEN];
655         const char *ext2ver, *ext2date;
656
657         memset(white, ' ', MAXPATHLEN);
658         white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
659
660 #ifdef __linux__
661         ext2fs_get_library_version(&ext2ver, &ext2date);
662         (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n", 
663                       __progname, _DUMP_VERSION, ext2ver, ext2date);
664 #else
665         (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
666 #endif
667
668 #ifdef KERBEROS
669 #define kerbflag "k"
670 #else
671 #define kerbflag
672 #endif
673
674 #ifdef USE_QFA
675 #define qfaflag "[-Q file] "
676 #else
677 #define qfaflag
678 #endif
679
680         fprintf(stderr,
681                 "usage:"
682                 "\t%s -C [-cd" kerbflag "lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
683                 "\t%s    [-F script] [-L limit] [-s fileno]\n"
684                 "\t%s -i [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
685                 "\t%s    [-F script] " qfaflag "[-s fileno]\n"
686 #ifdef USE_QFA
687                 "\t%s -P file [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize]\n"
688                 "\t%s    [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
689 #endif
690                 "\t%s -r [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
691                 "\t%s    [-s fileno] [-T directory]\n"
692                 "\t%s -R [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
693                 "\t%s    [-s fileno] [-T directory]\n"
694                 "\t%s -t [-cdh" kerbflag "lMuvVy] [-A file] [-b blocksize] [-f file]\n"
695                 "\t%s    [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
696                 "\t%s -x [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
697                 "\t%s    [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
698                 __progname, white, 
699                 __progname, white, 
700 #ifdef USE_QFA
701                 __progname, white, 
702 #endif
703                 __progname, white,
704                 __progname, white, 
705                 __progname, white, 
706                 __progname, white);
707         exit(1);
708 }
709
710 /*
711  * obsolete --
712  *      Change set of key letters and ordered arguments into something
713  *      getopt(3) will like.
714  */
715 static void
716 obsolete(int *argcp, char **argvp[])
717 {
718         int argc, flags;
719         char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
720
721         /* Setup. */
722         argv = *argvp;
723         argc = *argcp;
724
725         /* Return if no arguments or first argument has leading dash. */
726         ap = argv[1];
727         if (argc == 1 || *ap == '-')
728                 return;
729
730         /* Allocate space for new arguments. */
731         if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
732             (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
733                 err(1, "malloc args");
734
735         *nargv++ = *argv;
736         argv += 2, argc -= 2;
737
738         for (flags = 0; *ap; ++ap) {
739                 switch (*ap) {
740                 case 'A':
741                 case 'b':
742                 case 'D':
743                 case 'f':
744                 case 'F':
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 }