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