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