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