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