]> git.wh0rd.org Git - dump.git/blob - restore/main.c
Fixed a lot of warnings in the code thanks to 'gcc -W'
[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.41 2002/07/19 14:57:39 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, oflag = 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:mMNo"
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                 case 'o':
262                         oflag = 1;
263                         break;
264 #ifdef USE_QFA
265                 case 'Q':
266                         gTapeposfile = optarg;
267                         tapeposflag = 1;
268                         aflag = 1;
269                         break;
270 #endif
271                 case 's':
272                         /* Dumpnum (skip to) for multifile dump tapes. */
273                         dumpnum = strtol(optarg, &p, 10);
274                         if (*p)
275                                 errx(1, "illegal dump number -- %s", optarg);
276                         if (dumpnum <= 0)
277                                 errx(1, "dump number must be greater than 0");
278                         break;
279                 case 'u':
280                         uflag = 1;
281                         break;
282                 case 'v':
283                         vflag = 1;
284                         break;
285                 case 'V':
286                         Vflag = 1;
287                         break;
288                 case 'X':
289                         if( !strcmp(optarg,"-") ) {
290                                 use_stdin("-X");
291                                 filelist = stdin;
292                         }
293                         else
294                                 if ( !(filelist = fopen(optarg,"r")) )
295                                         errx(1, "can't open file for reading -- %s", optarg);
296                         break;
297                 case 'y':
298                         yflag = 1;
299                         break;
300                 default:
301                         usage();
302                 }
303         argc -= optind;
304         argv += optind;
305
306         if (command == '\0')
307                 errx(1, "none of C, i, R, r, t or x options specified");
308
309 #ifdef USE_QFA
310         if (!mflag && tapeposflag)
311                 errx(1, "m and Q options are mutually exclusive");
312
313         if (tapeposflag && command != 'i' && command != 'x' && command != 't')
314                 errx(1, "Q option is not valid for %c command", command);
315 #endif
316         
317         if (Afile && command != 'i' && command != 'x' && command != 't')
318                 errx(1, "A option is not valid for %c command", command);
319
320         if (signal(SIGINT, onintr) == SIG_IGN)
321                 (void) signal(SIGINT, SIG_IGN);
322         if (signal(SIGTERM, onintr) == SIG_IGN)
323                 (void) signal(SIGTERM, SIG_IGN);
324         setlinebuf(stderr);
325
326         atexit(cleanup);
327
328         if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
329 #ifdef RRESTORE
330             && !strchr(inputdev, ':')
331 #endif
332           ) {
333                 /* since we chdir into the directory we are comparing
334                  * to, we must retain the full tape path */
335                 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
336                 if (!getcwd(wd, MAXPATHLEN))
337                         err(1, "can't get current directory");
338                 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
339                 fullpathinput[MAXPATHLEN - 1] = '\0';
340                 setinput(fullpathinput);
341         }
342         else
343                 setinput(inputdev);
344
345         if (argc == 0 && !filelist) {
346                 argc = 1;
347                 *--argv = ".";
348         }
349
350 #ifdef USE_QFA
351         if (tapeposflag) {
352                 msg("reading QFA positions from %s\n", gTapeposfile);
353                 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
354                         errx(1, "can't open file for reading -- %s",
355                                 gTapeposfile);
356                 /* start reading header info */
357                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
358                         errx(1, "not requested format of -- %s", gTapeposfile);
359                 gTps[strlen(gTps) - 1] = 0;     /* delete end of line */
360                 if (strcmp(gTps, QFA_MAGIC) != 0)
361                         errx(1, "not requested format of -- %s", gTapeposfile);
362                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
363                         errx(1, "not requested format of -- %s", gTapeposfile);
364                 gTps[strlen(gTps) - 1] = 0;
365                 if (strcmp(gTps, QFA_VERSION) != 0)
366                         errx(1, "not requested format of -- %s", gTapeposfile);
367                 /* read dumpdate */
368                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
369                         errx(1, "not requested format of -- %s", gTapeposfile);
370                 gTps[strlen(gTps) - 1] = 0;
371                 qfadumpdate = atol(gTps);
372                 /* read empty line */
373                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
374                         errx(1, "not requested format of -- %s", gTapeposfile);
375                 gTps[strlen(gTps) - 1] = 0;
376                 /* read table header line */
377                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
378                         errx(1, "not requested format of -- %s", gTapeposfile);
379                 gTps[strlen(gTps) - 1] = 0;
380                 /* end reading header info */
381                 /* tape position table starts here */
382                 gSeekstart = ftell(gTapeposfp); /* remember for later use */
383         }
384 #endif /* USE_QFA */
385
386         switch (command) {
387         /*
388          * Compare contents of tape.
389          */
390         case 'C': {
391                 struct STAT stbuf;
392
393                 Vprintf(stdout, "Begin compare restore\n");
394                 compare_ignore_not_found = 0;
395                 compare_errors = 0;
396                 setup();
397                 printf("filesys = %s\n", filesys);
398                 if (STAT(filesys, &stbuf) < 0)
399                         err(1, "cannot stat directory %s", filesys);
400                 if (chdir(filesys) < 0)
401                         err(1, "cannot cd to %s", filesys);
402                 compare_ignore_not_found = dumptime > 0;
403                 initsymtable((char *)0);
404                 extractdirs(0);
405                 treescan(".", ROOTINO, nodeupdates);
406                 compareleaves();
407                 checkrestore();
408                 if (compare_errors) {
409                         printf("Some files were modified!\n");
410                         exit(2);
411                 }
412                 break;
413         }
414
415         /*
416          * Interactive mode.
417          */
418         case 'i':
419                 setup();
420                 extractdirs(1);
421                 initsymtable(NULL);
422                 runcmdshell();
423                 break;
424         /*
425          * Incremental restoration of a file system.
426          */
427         case 'r':
428                 aflag = 1;      /* in -r or -R mode, -a is default */
429                 setup();
430                 if (dumptime > 0) {
431                         /*
432                          * This is an incremental dump tape.
433                          */
434                         Vprintf(stdout, "Begin incremental restore\n");
435                         initsymtable(symtbl);
436                         extractdirs(1);
437                         removeoldleaves();
438                         Vprintf(stdout, "Calculate node updates.\n");
439                         treescan(".", ROOTINO, nodeupdates);
440                         findunreflinks();
441                         removeoldnodes();
442                 } else {
443                         /*
444                          * This is a level zero dump tape.
445                          */
446                         Vprintf(stdout, "Begin level 0 restore\n");
447                         initsymtable((char *)0);
448                         extractdirs(1);
449                         Vprintf(stdout, "Calculate extraction list.\n");
450                         treescan(".", ROOTINO, nodeupdates);
451                 }
452                 createleaves(symtbl);
453                 createlinks();
454                 setdirmodes(FORCE);
455                 checkrestore();
456                 if (dflag) {
457                         Vprintf(stdout, "Verify the directory structure\n");
458                         treescan(".", ROOTINO, verifyfile);
459                 }
460                 dumpsymtable(symtbl, (long)1);
461                 break;
462         /*
463          * Resume an incremental file system restoration.
464          */
465         case 'R':
466                 aflag = 1;      /* in -r or -R mode, -a is default */
467                 initsymtable(symtbl);
468                 skipmaps();
469                 skipdirs();
470                 createleaves(symtbl);
471                 createlinks();
472                 setdirmodes(FORCE);
473                 checkrestore();
474                 dumpsymtable(symtbl, (long)1);
475                 break;
476         
477 /* handle file names from either text file (-X) or the command line */
478 #define NEXTFILE(p) \
479         p = NULL; \
480         if (argc) { \
481                 --argc; \
482                 p = *argv++; \
483         } \
484         else if (filelist) { \
485                 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
486                         if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
487                                 *(p + strlen(p) - 1) = '\0'; \
488                         if ( !*p ) /* skip empty lines */ \
489                                 continue; \
490                         } \
491         }
492         
493         /*
494          * List contents of tape.
495          */
496         case 't':
497                 setup();
498                 extractdirs(0);
499                 initsymtable((char *)0);
500                 printvolinfo();
501                 for (;;) {
502                         NEXTFILE(p);
503                         if (!p)
504                                 break;
505                         canon(p, name, sizeof(name));
506                         ino = dirlookup(name);
507                         if (ino == 0)
508                                 continue;
509                         treescan(name, ino, listfile);
510                 }
511                 break;
512         /*
513          * Batch extraction of tape contents.
514          */
515         case 'x':
516 #ifdef DEBUG_QFA
517                 tistart = time(NULL);
518 #endif
519                 setup();
520                 extractdirs(1);
521                 initsymtable((char *)0);
522                 for (;;) {
523                         NEXTFILE(p);
524                         if (!p)
525                                 break;
526                         canon(p, name, sizeof(name));
527                         ino = dirlookup(name);
528                         if (ino == 0)
529                                 continue;
530                         if (mflag)
531                                 pathcheck(name);
532                         treescan(name, ino, addfile);
533                 }
534                 createfiles();
535                 createlinks();
536                 setdirmodes(oflag ? FORCE : 0);
537                 if (dflag)
538                         checkrestore();
539 #ifdef DEBUG_QFA
540                 tiend = time(NULL);
541                 titaken = tiend - tistart;
542                 msg("restore took %d:%02d:%02d\n", titaken / 3600, 
543                         (titaken % 3600) / 60, titaken % 60);
544 #endif /* DEBUG_QFA */
545                 break;
546 #ifdef USE_QFA
547         case 'P':
548 #ifdef DEBUG_QFA
549                 tistart = time(NULL);
550 #endif
551                 setup();
552                 msg("writing QFA positions to %s\n", gTapeposfile);
553                 if ((gTapeposfd = open(gTapeposfile, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0)
554                         errx(1, "can't create tapeposfile\n");
555                 /* print QFA-file header */
556                 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION,(unsigned long)spcl.c_date);
557                 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
558                         errx(1, "can't write tapeposfile\n");
559                 sprintf(gTps, "ino\ttapeno\ttapepos\n");
560                 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
561                         errx(1, "can't write tapeposfile\n");
562
563                 extractdirs(1);
564                 initsymtable((char *)0);
565                 for (;;) {
566                         NEXTFILE(p);
567                         if (!p)
568                                 break;
569                         canon(p, name, sizeof(name));
570                         ino = dirlookup(name);
571                         if (ino == 0)
572                                 continue;
573                         if (mflag)
574                                 pathcheck(name);
575                         treescan(name, ino, addfile);
576                 }
577                 createfiles();
578 #ifdef DEBUG_QFA
579                 tiend = time(NULL);
580                 titaken = tiend - tistart;
581                 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600,
582                         (titaken % 3600) / 60, titaken % 60);
583 #endif /* DEBUG_QFA */
584                 break;
585 #endif /* USE_QFA */
586         }
587         exit(0);
588         /* NOTREACHED */
589         return 0;       /* gcc shut up */
590 }
591
592 static void
593 usage(void)
594 {
595         char white[MAXPATHLEN];
596         const char *ext2ver, *ext2date;
597
598         memset(white, ' ', MAXPATHLEN);
599         white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
600
601 #ifdef __linux__
602         ext2fs_get_library_version(&ext2ver, &ext2date);
603         (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n", 
604                       __progname, _DUMP_VERSION, ext2ver, ext2date);
605 #else
606         (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
607 #endif
608
609 #ifdef KERBEROS
610 #define kerbflag "k"
611 #else
612 #define kerbflag
613 #endif
614
615 #ifdef USE_QFA
616 #define qfaflag "[-Q file] "
617 #else
618 #define qfaflag
619 #endif
620
621         fprintf(stderr,
622                 "usage:"
623                 "\t%s -C [-cd" kerbflag "lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
624                 "\t%s    [-F script] [-L limit] [-s fileno]\n"
625                 "\t%s -i [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
626                 "\t%s    [-F script] " qfaflag "[-s fileno]\n"
627 #ifdef USE_QFA
628                 "\t%s -P file [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize]\n"
629                 "\t%s    [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
630 #endif
631                 "\t%s -r [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
632                 "\t%s    [-s fileno] [-T directory]\n"
633                 "\t%s -R [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
634                 "\t%s    [-s fileno] [-T directory]\n"
635                 "\t%s -t [-cdh" kerbflag "lMuvVy] [-A file] [-b blocksize] [-f file]\n"
636                 "\t%s    [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
637                 "\t%s -x [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
638                 "\t%s    [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
639                 __progname, white, 
640                 __progname, white, 
641 #ifdef USE_QFA
642                 __progname, white, 
643 #endif
644                 __progname, white,
645                 __progname, white, 
646                 __progname, white, 
647                 __progname, white);
648         exit(1);
649 }
650
651 /*
652  * obsolete --
653  *      Change set of key letters and ordered arguments into something
654  *      getopt(3) will like.
655  */
656 static void
657 obsolete(int *argcp, char **argvp[])
658 {
659         int argc, flags;
660         char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
661
662         /* Setup. */
663         argv = *argvp;
664         argc = *argcp;
665
666         /* Return if no arguments or first argument has leading dash. */
667         ap = argv[1];
668         if (argc == 1 || *ap == '-')
669                 return;
670
671         /* Allocate space for new arguments. */
672         if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
673             (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
674                 err(1, "malloc args");
675
676         *nargv++ = *argv;
677         argv += 2, argc -= 2;
678
679         for (flags = 0; *ap; ++ap) {
680                 switch (*ap) {
681                 case 'A':
682                 case 'b':
683                 case 'D':
684                 case 'f':
685                 case 'F':
686                 case 'L':
687                 case 'Q':
688                 case 's':
689                 case 'T':
690                 case 'X':
691                         if (*argv == NULL) {
692                                 warnx("option requires an argument -- %c", *ap);
693                                 usage();
694                         }
695                         if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
696                                 err(1, "malloc arg");
697                         nargv[0][0] = '-';
698                         nargv[0][1] = *ap;
699                         (void)strcpy(&nargv[0][2], *argv);
700                         ++argv;
701                         ++nargv;
702                         break;
703                 default:
704                         if (!flags) {
705                                 *p++ = '-';
706                                 flags = 1;
707                         }
708                         *p++ = *ap;
709                         break;
710                 }
711         }
712
713         /* Terminate flags. */
714         if (flags) {
715                 *p = '\0';
716                 *nargv++ = flagsp;
717         }
718
719         /* Copy remaining arguments. */
720         while ((*nargv++ = *argv++));
721
722         /* Update argument count. */
723         *argcp = nargv - *argvp - 1;
724 }
725
726 /*
727  * use_stdin --
728  *      reserve stdin for opt (avoid conflicts)
729  */
730 void
731 use_stdin(const char *opt)
732 {
733         if (stdin_opt)
734                 errx(1, "can't handle standard input for both %s and %s",
735                         stdin_opt, opt);
736         stdin_opt = opt;
737 }