]> git.wh0rd.org Git - dump.git/blob - restore/main.c
Documented the -d option in restore.
[dump.git] / restore / main.c
1 /*
2  *      Ported to Linux's Second Extended File System as part of the
3  *      dump and restore backup suit
4  *      Remy Card <card@Linux.EU.Org>, 1994-1997
5  *      Stelian Pop <stelian@popies.net>, 1999-2000
6  *      Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
7  */
8
9 /*
10  * Copyright (c) 1983, 1993
11  *      The Regents of the University of California.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41
42 #ifndef lint
43 static const char rcsid[] =
44         "$Id: main.c,v 1.39 2002/06/05 13:29:15 stelian Exp $";
45 #endif /* not lint */
46
47 #include <config.h>
48 #include <compatlfs.h>
49 #include <fcntl.h>
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <errno.h>
53
54 #ifdef  __linux__
55 #include <sys/time.h>
56 #include <time.h>
57 #ifdef HAVE_EXT2FS_EXT2_FS_H
58 #include <ext2fs/ext2_fs.h>
59 #else
60 #include <linux/ext2_fs.h>
61 #endif
62 #include <bsdcompat.h>
63 #include <signal.h>
64 #include <string.h>
65 #else   /* __linux__ */
66 #include <ufs/ufs/dinode.h>
67 #endif  /* __linux__ */
68 #include <protocols/dumprestore.h>
69
70 #include <compaterr.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <unistd.h>
74
75 #ifdef  __linux__
76 #include <ext2fs/ext2fs.h>
77 #include <getopt.h>
78 #endif
79
80 #include "pathnames.h"
81 #include "restore.h"
82 #include "extern.h"
83
84 int     aflag = 0, bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
85 int     hflag = 1, mflag = 1, Mflag = 0, Nflag = 0, Vflag = 0, zflag = 0;
86 int     uflag = 0, lflag = 0, Lflag = 0;
87 char    *Afile = NULL;
88 int     dokerberos = 0;
89 char    command = '\0';
90 long    dumpnum = 1;
91 long    volno = 0;
92 long    ntrec;
93 char    *dumpmap;
94 char    *usedinomap;
95 dump_ino_t maxino;
96 time_t  dumptime;
97 time_t  dumpdate;
98 FILE    *terminal;
99 char    *tmpdir;
100 int     compare_ignore_not_found;
101 int     compare_errors;
102 char    filesys[NAMELEN];
103 static const char *stdin_opt = NULL;
104 char    *bot_script = NULL;
105 dump_ino_t volinfo[TP_NINOS];
106
107 #ifdef USE_QFA
108 FILE    *gTapeposfp;
109 char    *gTapeposfile;
110 char    gTps[255];
111 long    gSeekstart;
112 int     tapeposflag;
113 int     gTapeposfd;
114 int     createtapeposflag;
115 unsigned long qfadumpdate;
116 long long curtapepos;
117 #endif /* USE_QFA */
118
119 #if defined(__linux__) || defined(sunos)
120 char    *__progname;
121 #endif
122
123 static void obsolete __P((int *, char **[]));
124 static void usage __P((void));
125 static void use_stdin __P((const char *));
126
127 int
128 main(int argc, char *argv[])
129 {
130         int ch;
131         dump_ino_t ino;
132         char *inputdev = _PATH_DEFTAPE;
133         char *symtbl = "./restoresymtable";
134         char *p, name[MAXPATHLEN];
135         FILE *filelist = NULL;
136         char fname[MAXPATHLEN];
137 #ifdef DEBUG_QFA
138         time_t tistart, tiend, titaken;
139 #endif
140 #ifdef USE_QFA
141         tapeposflag = 0;
142         createtapeposflag = 0;
143 #endif /* USE_QFA */
144
145         /* Temp files should *not* be readable.  We set permissions later. */
146         (void) umask(077);
147         filesys[0] = '\0';
148 #if defined(__linux__) || defined(sunos)
149         __progname = argv[0];
150 #endif
151
152         if (argc < 2)
153                 usage();
154
155         if ((inputdev = getenv("TAPE")) == NULL)
156                 inputdev = _PATH_DEFTAPE;
157         if ((tmpdir = getenv("TMPDIR")) == NULL)
158                 tmpdir = _PATH_TMP;
159         if ((tmpdir = strdup(tmpdir)) == NULL)
160                 err(1, "malloc tmpdir");
161         for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
162                 ;                                                               
163         obsolete(&argc, &argv);
164         while ((ch = getopt(argc, argv, 
165                 "aA:b:CcdD:f:F:hi"
166 #ifdef KERBEROS
167                 "k"
168 #endif
169                 "lL:mMN"
170 #ifdef USE_QFA
171                 "P:Q:"
172 #endif
173                 "Rrs:tT:uvVxX:y")) != -1)
174                 switch(ch) {
175                 case 'a':
176                         aflag = 1;
177                         break;
178                 case 'A':
179                         Afile = optarg;
180                         aflag = 1;
181                         break;
182                 case 'b':
183                         /* Change default tape blocksize. */
184                         bflag = 1;
185                         ntrec = strtol(optarg, &p, 10);
186                         if (*p)
187                                 errx(1, "illegal blocksize -- %s", optarg);
188                         if (ntrec <= 0)
189                                 errx(1, "block size must be greater than 0");
190                         break;
191                 case 'c':
192                         cvtflag = 1;
193                         break;
194                 case 'D':
195                         strncpy(filesys, optarg, NAMELEN);
196                         filesys[NAMELEN - 1] = '\0';
197                         break;
198                 case 'T':
199                         tmpdir = optarg;
200                         break;
201                 case 'd':
202                         dflag = 1;
203                         break;
204                 case 'f':
205                         if( !strcmp(optarg,"-") )
206                                 use_stdin("-f");
207                         inputdev = optarg;
208                         break;
209                 case 'F':
210                         bot_script = optarg;
211                         break;
212                 case 'h':
213                         hflag = 0;
214                         break;
215 #ifdef KERBEROS
216                 case 'k':
217                         dokerberos = 1;
218                         break;
219 #endif
220                 case 'C':
221                 case 'i':
222 #ifdef USE_QFA
223                 case 'P':
224 #endif
225                 case 'R':
226                 case 'r':
227                 case 't':
228                 case 'x':
229                         if (command != '\0')
230                                 errx(1,
231                                     "%c and %c options are mutually exclusive",
232                                     ch, command);
233                         command = ch;
234 #ifdef USE_QFA
235                         if (ch == 'P') {
236                                 gTapeposfile = optarg;
237                                 createtapeposflag = 1;
238                         }
239 #endif
240
241                         break;
242                 case 'l':
243                         lflag = 1;
244                         break;
245                 case 'L':
246                         Lflag = strtol(optarg, &p, 10);
247                         if (*p)
248                                 errx(1, "illegal limit -- %s", optarg);
249                         if (Lflag < 0)
250                                 errx(1, "limit must be greater than 0");
251                         break;
252                 case 'm':
253                         mflag = 0;
254                         break;
255                 case 'M':
256                         Mflag = 1;
257                         break;
258                 case 'N':
259                         Nflag = 1;
260                         break;
261 #ifdef USE_QFA
262                 case 'Q':
263                         gTapeposfile = optarg;
264                         tapeposflag = 1;
265                         aflag = 1;
266                         break;
267 #endif
268                 case 's':
269                         /* Dumpnum (skip to) for multifile dump tapes. */
270                         dumpnum = strtol(optarg, &p, 10);
271                         if (*p)
272                                 errx(1, "illegal dump number -- %s", optarg);
273                         if (dumpnum <= 0)
274                                 errx(1, "dump number must be greater than 0");
275                         break;
276                 case 'u':
277                         uflag = 1;
278                         break;
279                 case 'v':
280                         vflag = 1;
281                         break;
282                 case 'V':
283                         Vflag = 1;
284                         break;
285                 case 'X':
286                         if( !strcmp(optarg,"-") ) {
287                                 use_stdin("-X");
288                                 filelist = stdin;
289                         }
290                         else
291                                 if ( !(filelist = fopen(optarg,"r")) )
292                                         errx(1, "can't open file for reading -- %s", optarg);
293                         break;
294                 case 'y':
295                         yflag = 1;
296                         break;
297                 default:
298                         usage();
299                 }
300         argc -= optind;
301         argv += optind;
302
303         if (command == '\0')
304                 errx(1, "none of C, i, R, r, t or x options specified");
305
306 #ifdef USE_QFA
307         if (!mflag && tapeposflag)
308                 errx(1, "m and Q options are mutually exclusive");
309
310         if (tapeposflag && command != 'i' && command != 'x' && command != 't')
311                 errx(1, "Q option is not valid for %c command", command);
312 #endif
313         
314         if (Afile && command != 'i' && command != 'x' && command != 't')
315                 errx(1, "A option is not valid for %c command", command);
316
317         if (signal(SIGINT, onintr) == SIG_IGN)
318                 (void) signal(SIGINT, SIG_IGN);
319         if (signal(SIGTERM, onintr) == SIG_IGN)
320                 (void) signal(SIGTERM, SIG_IGN);
321         setlinebuf(stderr);
322
323         atexit(cleanup);
324
325         if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
326 #ifdef RRESTORE
327             && !strchr(inputdev, ':')
328 #endif
329           ) {
330                 /* since we chdir into the directory we are comparing
331                  * to, we must retain the full tape path */
332                 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
333                 if (!getcwd(wd, MAXPATHLEN))
334                         err(1, "can't get current directory");
335                 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
336                 fullpathinput[MAXPATHLEN - 1] = '\0';
337                 setinput(fullpathinput);
338         }
339         else
340                 setinput(inputdev);
341
342         if (argc == 0 && !filelist) {
343                 argc = 1;
344                 *--argv = ".";
345         }
346
347 #ifdef USE_QFA
348         if (tapeposflag) {
349                 msg("reading QFA positions from %s\n", gTapeposfile);
350                 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
351                         errx(1, "can't open file for reading -- %s",
352                                 gTapeposfile);
353                 /* start reading header info */
354                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
355                         errx(1, "not requested format of -- %s", gTapeposfile);
356                 gTps[strlen(gTps) - 1] = 0;     /* delete end of line */
357                 if (strcmp(gTps, QFA_MAGIC) != 0)
358                         errx(1, "not requested format of -- %s", gTapeposfile);
359                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
360                         errx(1, "not requested format of -- %s", gTapeposfile);
361                 gTps[strlen(gTps) - 1] = 0;
362                 if (strcmp(gTps, QFA_VERSION) != 0)
363                         errx(1, "not requested format of -- %s", gTapeposfile);
364                 /* read dumpdate */
365                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
366                         errx(1, "not requested format of -- %s", gTapeposfile);
367                 gTps[strlen(gTps) - 1] = 0;
368                 qfadumpdate = atol(gTps);
369                 /* read empty line */
370                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
371                         errx(1, "not requested format of -- %s", gTapeposfile);
372                 gTps[strlen(gTps) - 1] = 0;
373                 /* read table header line */
374                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
375                         errx(1, "not requested format of -- %s", gTapeposfile);
376                 gTps[strlen(gTps) - 1] = 0;
377                 /* end reading header info */
378                 /* tape position table starts here */
379                 gSeekstart = ftell(gTapeposfp); /* remember for later use */
380         }
381 #endif /* USE_QFA */
382
383         switch (command) {
384         /*
385          * Compare contents of tape.
386          */
387         case 'C': {
388                 struct STAT stbuf;
389
390                 Vprintf(stdout, "Begin compare restore\n");
391                 compare_ignore_not_found = 0;
392                 compare_errors = 0;
393                 setup();
394                 printf("filesys = %s\n", filesys);
395                 if (STAT(filesys, &stbuf) < 0)
396                         err(1, "cannot stat directory %s", filesys);
397                 if (chdir(filesys) < 0)
398                         err(1, "cannot cd to %s", filesys);
399                 compare_ignore_not_found = dumptime > 0;
400                 initsymtable((char *)0);
401                 extractdirs(0);
402                 treescan(".", ROOTINO, nodeupdates);
403                 compareleaves();
404                 checkrestore();
405                 if (compare_errors) {
406                         printf("Some files were modified!\n");
407                         exit(2);
408                 }
409                 break;
410         }
411
412         /*
413          * Interactive mode.
414          */
415         case 'i':
416                 setup();
417                 extractdirs(1);
418                 initsymtable(NULL);
419                 runcmdshell();
420                 break;
421         /*
422          * Incremental restoration of a file system.
423          */
424         case 'r':
425                 aflag = 1;      /* in -r or -R mode, -a is default */
426                 setup();
427                 if (dumptime > 0) {
428                         /*
429                          * This is an incremental dump tape.
430                          */
431                         Vprintf(stdout, "Begin incremental restore\n");
432                         initsymtable(symtbl);
433                         extractdirs(1);
434                         removeoldleaves();
435                         Vprintf(stdout, "Calculate node updates.\n");
436                         treescan(".", ROOTINO, nodeupdates);
437                         findunreflinks();
438                         removeoldnodes();
439                 } else {
440                         /*
441                          * This is a level zero dump tape.
442                          */
443                         Vprintf(stdout, "Begin level 0 restore\n");
444                         initsymtable((char *)0);
445                         extractdirs(1);
446                         Vprintf(stdout, "Calculate extraction list.\n");
447                         treescan(".", ROOTINO, nodeupdates);
448                 }
449                 createleaves(symtbl);
450                 createlinks();
451                 setdirmodes(FORCE);
452                 checkrestore();
453                 if (dflag) {
454                         Vprintf(stdout, "Verify the directory structure\n");
455                         treescan(".", ROOTINO, verifyfile);
456                 }
457                 dumpsymtable(symtbl, (long)1);
458                 break;
459         /*
460          * Resume an incremental file system restoration.
461          */
462         case 'R':
463                 aflag = 1;      /* in -r or -R mode, -a is default */
464                 initsymtable(symtbl);
465                 skipmaps();
466                 skipdirs();
467                 createleaves(symtbl);
468                 createlinks();
469                 setdirmodes(FORCE);
470                 checkrestore();
471                 dumpsymtable(symtbl, (long)1);
472                 break;
473         
474 /* handle file names from either text file (-X) or the command line */
475 #define NEXTFILE(p) \
476         p = NULL; \
477         if (argc) { \
478                 --argc; \
479                 p = *argv++; \
480         } \
481         else if (filelist) { \
482                 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
483                         if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
484                                 *(p + strlen(p) - 1) = '\0'; \
485                         if ( !*p ) /* skip empty lines */ \
486                                 continue; \
487                         } \
488         }
489         
490         /*
491          * List contents of tape.
492          */
493         case 't':
494                 setup();
495                 extractdirs(0);
496                 initsymtable((char *)0);
497                 printvolinfo();
498                 for (;;) {
499                         NEXTFILE(p);
500                         if (!p)
501                                 break;
502                         canon(p, name, sizeof(name));
503                         ino = dirlookup(name);
504                         if (ino == 0)
505                                 continue;
506                         treescan(name, ino, listfile);
507                 }
508                 break;
509         /*
510          * Batch extraction of tape contents.
511          */
512         case 'x':
513 #ifdef DEBUG_QFA
514                 tistart = time(NULL);
515 #endif
516                 setup();
517                 extractdirs(1);
518                 initsymtable((char *)0);
519                 for (;;) {
520                         NEXTFILE(p);
521                         if (!p)
522                                 break;
523                         canon(p, name, sizeof(name));
524                         ino = dirlookup(name);
525                         if (ino == 0)
526                                 continue;
527                         if (mflag)
528                                 pathcheck(name);
529                         treescan(name, ino, addfile);
530                 }
531                 createfiles();
532                 createlinks();
533                 setdirmodes(0);
534                 if (dflag)
535                         checkrestore();
536 #ifdef DEBUG_QFA
537                 tiend = time(NULL);
538                 titaken = tiend - tistart;
539                 msg("restore took %d:%02d:%02d\n", titaken / 3600, 
540                         (titaken % 3600) / 60, titaken % 60);
541 #endif /* DEBUG_QFA */
542                 break;
543 #ifdef USE_QFA
544         case 'P':
545 #ifdef DEBUG_QFA
546                 tistart = time(NULL);
547 #endif
548                 setup();
549                 msg("writing QFA positions to %s\n", gTapeposfile);
550                 if ((gTapeposfd = open(gTapeposfile, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0)
551                         errx(1, "can't create tapeposfile\n");
552                 /* print QFA-file header */
553                 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION,(unsigned long)spcl.c_date);
554                 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps))
555                         errx(1, "can't write tapeposfile\n");
556                 sprintf(gTps, "ino\ttapeno\ttapepos\n");
557                 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps))
558                         errx(1, "can't write tapeposfile\n");
559
560                 extractdirs(1);
561                 initsymtable((char *)0);
562                 for (;;) {
563                         NEXTFILE(p);
564                         if (!p)
565                                 break;
566                         canon(p, name, sizeof(name));
567                         ino = dirlookup(name);
568                         if (ino == 0)
569                                 continue;
570                         if (mflag)
571                                 pathcheck(name);
572                         treescan(name, ino, addfile);
573                 }
574                 createfiles();
575 #ifdef DEBUG_QFA
576                 tiend = time(NULL);
577                 titaken = tiend - tistart;
578                 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600,
579                         (titaken % 3600) / 60, titaken % 60);
580 #endif /* DEBUG_QFA */
581                 break;
582 #endif /* USE_QFA */
583         }
584         exit(0);
585         /* NOTREACHED */
586         return 0;       /* gcc shut up */
587 }
588
589 static void
590 usage(void)
591 {
592         char white[MAXPATHLEN];
593         const char *ext2ver, *ext2date;
594
595         memset(white, ' ', MAXPATHLEN);
596         white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
597
598 #ifdef __linux__
599         ext2fs_get_library_version(&ext2ver, &ext2date);
600         (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n", 
601                       __progname, _DUMP_VERSION, ext2ver, ext2date);
602 #else
603         (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
604 #endif
605
606 #ifdef KERBEROS
607 #define kerbflag "k"
608 #else
609 #define kerbflag
610 #endif
611
612 #ifdef USE_QFA
613 #define qfaflag "[-Q file] "
614 #else
615 #define qfaflag
616 #endif
617
618         fprintf(stderr,
619                 "usage:"
620                 "\t%s -C [-cd" kerbflag "lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
621                 "\t%s    [-F script] [-L limit] [-s fileno]\n"
622                 "\t%s -i [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize] [-f file]\n"
623                 "\t%s    [-F script] " qfaflag "[-s fileno]\n"
624 #ifdef USE_QFA
625                 "\t%s -P file [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize]\n"
626                 "\t%s    [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
627 #endif
628                 "\t%s -r [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
629                 "\t%s    [-s fileno] [-T directory]\n"
630                 "\t%s -R [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
631                 "\t%s    [-s fileno] [-T directory]\n"
632                 "\t%s -t [-cdh" kerbflag "lMuvVy] [-A file] [-b blocksize] [-f file]\n"
633                 "\t%s    [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
634                 "\t%s -x [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize] [-f file]\n"
635                 "\t%s    [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
636                 __progname, white, 
637                 __progname, white, 
638 #ifdef USE_QFA
639                 __progname, white, 
640 #endif
641                 __progname, white,
642                 __progname, white, 
643                 __progname, white, 
644                 __progname, white);
645         exit(1);
646 }
647
648 /*
649  * obsolete --
650  *      Change set of key letters and ordered arguments into something
651  *      getopt(3) will like.
652  */
653 static void
654 obsolete(int *argcp, char **argvp[])
655 {
656         int argc, flags;
657         char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
658
659         /* Setup. */
660         argv = *argvp;
661         argc = *argcp;
662
663         /* Return if no arguments or first argument has leading dash. */
664         ap = argv[1];
665         if (argc == 1 || *ap == '-')
666                 return;
667
668         /* Allocate space for new arguments. */
669         if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
670             (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
671                 err(1, "malloc args");
672
673         *nargv++ = *argv;
674         argv += 2, argc -= 2;
675
676         for (flags = 0; *ap; ++ap) {
677                 switch (*ap) {
678                 case 'A':
679                 case 'b':
680                 case 'D':
681                 case 'f':
682                 case 'F':
683                 case 'L':
684                 case 'Q':
685                 case 's':
686                 case 'T':
687                 case 'X':
688                         if (*argv == NULL) {
689                                 warnx("option requires an argument -- %c", *ap);
690                                 usage();
691                         }
692                         if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
693                                 err(1, "malloc arg");
694                         nargv[0][0] = '-';
695                         nargv[0][1] = *ap;
696                         (void)strcpy(&nargv[0][2], *argv);
697                         ++argv;
698                         ++nargv;
699                         break;
700                 default:
701                         if (!flags) {
702                                 *p++ = '-';
703                                 flags = 1;
704                         }
705                         *p++ = *ap;
706                         break;
707                 }
708         }
709
710         /* Terminate flags. */
711         if (flags) {
712                 *p = '\0';
713                 *nargv++ = flagsp;
714         }
715
716         /* Copy remaining arguments. */
717         while ((*nargv++ = *argv++));
718
719         /* Update argument count. */
720         *argcp = nargv - *argvp - 1;
721 }
722
723 /*
724  * use_stdin --
725  *      reserve stdin for opt (avoid conflicts)
726  */
727 void
728 use_stdin(const char *opt)
729 {
730         if (stdin_opt)
731                 errx(1, "can't handle standard input for both %s and %s",
732                         stdin_opt, opt);
733         stdin_opt = opt;
734 }