main.c revision 1.25.6.5 1 /* $NetBSD: main.c,v 1.25.6.5 2002/01/16 09:41:39 he Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
45 #else
46 __RCSID("$NetBSD: main.c,v 1.25.6.5 2002/01/16 09:41:39 he Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <sys/stat.h>
53 #include <sys/mount.h>
54
55 #ifdef sunos
56 #include <sys/vnode.h>
57
58 #include <ufs/inode.h>
59 #include <ufs/fs.h>
60 #else
61 #include <ufs/ufs/dinode.h>
62 #include <ufs/ffs/fs.h>
63 #include <ufs/ffs/ffs_extern.h>
64 #endif
65
66 #include <protocols/dumprestore.h>
67
68 #include <ctype.h>
69 #include <err.h>
70 #include <errno.h>
71 #include <fcntl.h>
72 #include <fstab.h>
73 #include <signal.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <time.h>
78 #include <unistd.h>
79
80 #include "dump.h"
81 #include "pathnames.h"
82
83 gid_t egid; /* Retain tty privs for notification */
84 int notify = 0; /* notify operator flag */
85 int blockswritten = 0; /* number of blocks written on current tape */
86 int tapeno = 0; /* current tape number */
87 int density = 0; /* density in bytes/0.1" */
88 int ntrec = NTREC; /* # tape blocks in each tape record */
89 int cartridge = 0; /* Assume non-cartridge tape */
90 long dev_bsize = 1; /* recalculated below */
91 long blocksperfile = 0; /* output blocks per file */
92 char *host = NULL; /* remote host (if any) */
93 int readcache = -1; /* read cache size (in readblksize blks) */
94 int readblksize = 32 * 1024; /* read block size */
95
96 int main __P((int, char *[]));
97 static long numarg __P((char *, long, long));
98 static void obsolete __P((int *, char **[]));
99 static void usage __P((void));
100
101 int
102 main(argc, argv)
103 int argc;
104 char *argv[];
105 {
106 ino_t ino;
107 int dirty;
108 struct dinode *dp;
109 struct fstab *dt;
110 char *map;
111 int ch;
112 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
113 ino_t maxino;
114 time_t tnow, date;
115 int dirlist;
116 char *toplevel;
117 int just_estimate = 0;
118 char labelstr[LBLSIZE];
119
120 spcl.c_date = 0;
121 (void)time((time_t *)&spcl.c_date);
122
123 /* Save setgid bit for use later */
124 egid = getegid();
125 setegid(getgid());
126
127 tsize = 0; /* Default later, based on 'c' option for cart tapes */
128 if ((tape = getenv("TAPE")) == NULL)
129 tape = _PATH_DEFTAPE;
130 dumpdates = _PATH_DUMPDATES;
131 temp = _PATH_DTMP;
132 strcpy(labelstr, "none"); /* XXX safe strcpy. */
133 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
134 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
135 level = '0';
136
137 if (argc < 2)
138 usage();
139
140 obsolete(&argc, &argv);
141 while ((ch = getopt(argc, argv,
142 "0123456789B:b:cd:ef:h:k:l:L:nr:s:ST:uWw")) != -1)
143 switch (ch) {
144 /* dump level */
145 case '0': case '1': case '2': case '3': case '4':
146 case '5': case '6': case '7': case '8': case '9':
147 level = ch;
148 break;
149
150 case 'B': /* blocks per output file */
151 blocksperfile = numarg("blocks per file", 1L, 0L);
152 break;
153
154 case 'b': /* blocks per tape write */
155 ntrec = numarg("blocks per write", 1L, 1000L);
156 bflag = 1;
157 break;
158
159 case 'c': /* Tape is cart. not 9-track */
160 cartridge = 1;
161 break;
162
163 case 'd': /* density, in bits per inch */
164 density = numarg("density", 10L, 327670L) / 10;
165 if (density >= 625 && !bflag)
166 ntrec = HIGHDENSITYTREC;
167 break;
168
169 case 'e': /* eject full tapes */
170 eflag = 1;
171 break;
172
173 case 'f': /* output file */
174 tape = optarg;
175 break;
176
177 case 'h':
178 honorlevel = numarg("honor level", 0L, 10L);
179 break;
180
181 case 'k':
182 readblksize = numarg("read block size", 0, 64) * 1024;
183 break;
184
185 case 'l': /* autoload after eject full tapes */
186 eflag = 1;
187 lflag = numarg("timeout (in seconds)", 1, 0);
188 break;
189
190 case 'L':
191 /*
192 * Note that although there are LBLSIZE characters,
193 * the last must be '\0', so the limit on strlen()
194 * is really LBLSIZE-1.
195 */
196 strncpy(labelstr, optarg, LBLSIZE);
197 labelstr[LBLSIZE-1] = '\0';
198 if (strlen(optarg) > LBLSIZE-1) {
199 msg(
200 "WARNING Label `%s' is larger than limit of %d characters.\n",
201 optarg, LBLSIZE-1);
202 msg("WARNING: Using truncated label `%s'.\n",
203 labelstr);
204 }
205 break;
206 case 'n': /* notify operators */
207 notify = 1;
208 break;
209
210 case 'r': /* read cache size */
211 readcache = numarg("read cache size", 0, 512);
212 break;
213
214 case 's': /* tape size, feet */
215 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
216 break;
217
218 case 'S': /* exit after estimating # of tapes */
219 just_estimate = 1;
220 break;
221
222 case 'T': /* time of last dump */
223 spcl.c_ddate = unctime(optarg);
224 if (spcl.c_ddate < 0) {
225 (void)fprintf(stderr, "bad time \"%s\"\n",
226 optarg);
227 exit(X_ABORT);
228 }
229 Tflag = 1;
230 lastlevel = '?';
231 break;
232
233 case 'u': /* update /etc/dumpdates */
234 uflag = 1;
235 break;
236
237 case 'W': /* what to do */
238 case 'w':
239 lastdump(ch);
240 exit(0); /* do nothing else */
241
242 default:
243 usage();
244 }
245 argc -= optind;
246 argv += optind;
247
248 if (argc < 1) {
249 (void)fprintf(stderr, "Must specify disk or filesystem\n");
250 exit(X_ABORT);
251 }
252
253 /*
254 * determine if disk is a subdirectory, and setup appropriately
255 */
256 dirlist = 0;
257 toplevel = NULL;
258 for (i = 0; i < argc; i++) {
259 struct stat sb;
260 struct statfs fsbuf;
261
262 if (lstat(argv[i], &sb) == -1) {
263 msg("Cannot lstat %s: %s\n", argv[i], strerror(errno));
264 exit(X_ABORT);
265 }
266 if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
267 break;
268 if (statfs(argv[i], &fsbuf) == -1) {
269 msg("Cannot statfs %s: %s\n", argv[i], strerror(errno));
270 exit(X_ABORT);
271 }
272 if (strcmp(argv[i], fsbuf.f_mntonname) == 0) {
273 if (dirlist != 0) {
274 msg("Can't dump a mountpoint and a filelist\n");
275 exit(X_ABORT);
276 }
277 break; /* exit if sole mountpoint */
278 }
279 if (!disk) {
280 if ((toplevel = strdup(fsbuf.f_mntonname)) == NULL) {
281 msg("Cannot malloc diskname\n");
282 exit(X_ABORT);
283 }
284 disk = toplevel;
285 if (uflag) {
286 msg("Ignoring u flag for subdir dump\n");
287 uflag = 0;
288 }
289 if (level > '0') {
290 msg("Subdir dump is done at level 0\n");
291 level = '0';
292 }
293 msg("Dumping sub files/directories from %s\n", disk);
294 } else {
295 if (strcmp(disk, fsbuf.f_mntonname) != 0) {
296 msg("%s is not on %s\n", argv[i], disk);
297 exit(X_ABORT);
298 }
299 }
300 msg("Dumping file/directory %s\n", argv[i]);
301 dirlist++;
302 }
303 if (dirlist == 0) {
304 disk = *argv++;
305 if (argc != 1) {
306 (void)fprintf(stderr, "Excess arguments to dump:");
307 while (--argc)
308 (void)fprintf(stderr, " %s", *argv++);
309 (void)fprintf(stderr, "\n");
310 exit(X_ABORT);
311 }
312 }
313 if (Tflag && uflag) {
314 (void)fprintf(stderr,
315 "You cannot use the T and u flags together.\n");
316 exit(X_ABORT);
317 }
318 if (strcmp(tape, "-") == 0) {
319 pipeout++;
320 tape = "standard output";
321 }
322
323 if (blocksperfile)
324 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
325 else {
326 /*
327 * Determine how to default tape size and density
328 *
329 * density tape size
330 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
331 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
332 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
333 * (450*4 - slop)
334 */
335 if (density == 0)
336 density = cartridge ? 100 : 160;
337 if (tsize == 0)
338 tsize = cartridge ? 1700L*120L : 2300L*120L;
339 }
340
341 if (strchr(tape, ':')) {
342 host = tape;
343 tape = strchr(host, ':');
344 *tape++ = '\0';
345 #ifdef RDUMP
346 if (rmthost(host) == 0)
347 exit(X_ABORT);
348 #else
349 (void)fprintf(stderr, "remote dump not enabled\n");
350 exit(X_ABORT);
351 #endif
352 }
353
354 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
355 signal(SIGHUP, sig);
356 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
357 signal(SIGTRAP, sig);
358 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
359 signal(SIGFPE, sig);
360 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
361 signal(SIGBUS, sig);
362 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
363 signal(SIGSEGV, sig);
364 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
365 signal(SIGTERM, sig);
366 if (signal(SIGINT, interrupt) == SIG_IGN)
367 signal(SIGINT, SIG_IGN);
368
369 set_operators(); /* /etc/group snarfed */
370 getfstab(); /* /etc/fstab snarfed */
371
372 /*
373 * disk can be either the full special file name,
374 * the suffix of the special file name,
375 * the special name missing the leading '/',
376 * the file system name with or without the leading '/'.
377 */
378 dt = fstabsearch(disk);
379 if (dt != NULL) {
380 disk = rawname(dt->fs_spec);
381 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
382 if (dirlist != 0)
383 (void)snprintf(spcl.c_filesys, NAMELEN,
384 "a subset of %s", dt->fs_file);
385 else
386 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
387 } else {
388 (void)strncpy(spcl.c_dev, disk, NAMELEN);
389 (void)strncpy(spcl.c_filesys, "an unlisted file system",
390 NAMELEN);
391 }
392 (void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1);
393 (void)gethostname(spcl.c_host, NAMELEN);
394 spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
395
396 if ((diskfd = open(disk, O_RDONLY)) < 0) {
397 msg("Cannot open %s\n", disk);
398 exit(X_ABORT);
399 }
400 sync();
401
402 needswap = fs_read_sblock(sblock_buf);
403
404 spcl.c_level = iswap32(level - '0');
405 spcl.c_type = iswap32(TS_TAPE);
406 spcl.c_date = iswap32(spcl.c_date);
407 spcl.c_ddate = iswap32(spcl.c_ddate);
408 if (!Tflag)
409 getdumptime(); /* /etc/dumpdates snarfed */
410
411 date = iswap32(spcl.c_date);
412 msg("Date of this level %c dump: %s", level,
413 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
414 date = iswap32(spcl.c_ddate);
415 msg("Date of last level %c dump: %s", lastlevel,
416 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
417 msg("Dumping ");
418 if (dt != NULL && dirlist != 0)
419 msgtail("a subset of ");
420 msgtail("%s (%s) ", disk, spcl.c_filesys);
421 if (host)
422 msgtail("to %s on host %s\n", tape, host);
423 else
424 msgtail("to %s\n", tape);
425 msg("Label: %s\n", labelstr);
426
427 ufsib = fs_parametrize();
428
429 dev_bshift = ffs(dev_bsize) - 1;
430 if (dev_bsize != (1 << dev_bshift))
431 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
432 tp_bshift = ffs(TP_BSIZE) - 1;
433 if (TP_BSIZE != (1 << tp_bshift))
434 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
435 maxino = fs_maxino();
436 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
437 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
438 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
439 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
440 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
441
442 nonodump = iswap32(spcl.c_level) < honorlevel;
443
444 initcache(readcache, readblksize);
445
446 (void)signal(SIGINFO, statussig);
447
448 msg("mapping (Pass I) [regular files]\n");
449 anydirskipped = mapfiles(maxino, &tapesize, toplevel,
450 (dirlist ? argv : NULL));
451
452 msg("mapping (Pass II) [directories]\n");
453 while (anydirskipped) {
454 anydirskipped = mapdirs(maxino, &tapesize);
455 }
456
457 if (pipeout) {
458 tapesize += 10; /* 10 trailer blocks */
459 msg("estimated %ld tape blocks.\n", tapesize);
460 } else {
461 double fetapes;
462
463 if (blocksperfile)
464 fetapes = (double) tapesize / blocksperfile;
465 else if (cartridge) {
466 /* Estimate number of tapes, assuming streaming stops at
467 the end of each block written, and not in mid-block.
468 Assume no erroneous blocks; this can be compensated
469 for with an artificially low tape size. */
470 fetapes =
471 ( tapesize /* blocks */
472 * TP_BSIZE /* bytes/block */
473 * (1.0/density) /* 0.1" / byte */
474 +
475 tapesize /* blocks */
476 * (1.0/ntrec) /* streaming-stops per block */
477 * 15.48 /* 0.1" / streaming-stop */
478 ) * (1.0 / tsize ); /* tape / 0.1" */
479 } else {
480 /* Estimate number of tapes, for old fashioned 9-track
481 tape */
482 int tenthsperirg = (density == 625) ? 3 : 7;
483 fetapes =
484 ( tapesize /* blocks */
485 * TP_BSIZE /* bytes / block */
486 * (1.0/density) /* 0.1" / byte */
487 +
488 tapesize /* blocks */
489 * (1.0/ntrec) /* IRG's / block */
490 * tenthsperirg /* 0.1" / IRG */
491 ) * (1.0 / tsize ); /* tape / 0.1" */
492 }
493 etapes = fetapes; /* truncating assignment */
494 etapes++;
495 /* count the dumped inodes map on each additional tape */
496 tapesize += (etapes - 1) *
497 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
498 tapesize += etapes + 10; /* headers + 10 trailer blks */
499 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
500 tapesize, fetapes);
501 }
502 /*
503 * If the user only wants an estimate of the number of
504 * tapes, exit now.
505 */
506 if (just_estimate)
507 exit(0);
508
509 /*
510 * Allocate tape buffer.
511 */
512 if (!alloctape())
513 quit("can't allocate tape buffers - try a smaller blocking factor.\n");
514
515 startnewtape(1);
516 (void)time((time_t *)&(tstart_writing));
517 xferrate = 0;
518 dumpmap(usedinomap, TS_CLRI, maxino - 1);
519
520 msg("dumping (Pass III) [directories]\n");
521 dirty = 0; /* XXX just to get gcc to shut up */
522 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
523 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
524 dirty = *map++;
525 else
526 dirty >>= 1;
527 if ((dirty & 1) == 0)
528 continue;
529 /*
530 * Skip directory inodes deleted and maybe reallocated
531 */
532 dp = getino(ino);
533 if ((dp->di_mode & IFMT) != IFDIR)
534 continue;
535 (void)dumpino(dp, ino);
536 }
537
538 msg("dumping (Pass IV) [regular files]\n");
539 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
540 int mode;
541
542 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
543 dirty = *map++;
544 else
545 dirty >>= 1;
546 if ((dirty & 1) == 0)
547 continue;
548 /*
549 * Skip inodes deleted and reallocated as directories.
550 */
551 dp = getino(ino);
552 mode = dp->di_mode & IFMT;
553 if (mode == IFDIR)
554 continue;
555 (void)dumpino(dp, ino);
556 }
557
558 spcl.c_type = iswap32(TS_END);
559 for (i = 0; i < ntrec; i++)
560 writeheader(maxino - 1);
561 if (pipeout)
562 msg("%d tape blocks\n",iswap32(spcl.c_tapea));
563 else
564 msg("%d tape blocks on %d volume%s\n",
565 iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
566 (iswap32(spcl.c_volume) == 1) ? "" : "s");
567 tnow = do_stats();
568 date = iswap32(spcl.c_date);
569 msg("Date of this level %c dump: %s", level,
570 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
571 msg("Date this dump completed: %s", ctime(&tnow));
572 msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
573 putdumptime();
574 trewind(0);
575 broadcast("DUMP IS DONE!\7\7\n");
576 msg("DUMP IS DONE\n");
577 Exit(X_FINOK);
578 /* NOTREACHED */
579 exit(X_FINOK); /* XXX: to satisfy gcc */
580 }
581
582 static void
583 usage()
584 {
585
586 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
587 "usage: dump [-0123456789cnu] [-B records] [-b blocksize] [-d density]",
588 " [-f file] [-h level] [-k read block size] [-L label]",
589 " [-l timeout] [-r read cache size] [-s feet] [-T date] filesystem",
590 " dump [-W | -w]");
591 exit(1);
592 }
593
594 /*
595 * Pick up a numeric argument. It must be nonnegative and in the given
596 * range (except that a vmax of 0 means unlimited).
597 */
598 static long
599 numarg(meaning, vmin, vmax)
600 char *meaning;
601 long vmin, vmax;
602 {
603 char *p;
604 long val;
605
606 val = strtol(optarg, &p, 10);
607 if (*p)
608 errx(1, "illegal %s -- %s", meaning, optarg);
609 if (val < vmin || (vmax && val > vmax))
610 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
611 return (val);
612 }
613
614 void
615 sig(signo)
616 int signo;
617 {
618 switch(signo) {
619 case SIGALRM:
620 case SIGBUS:
621 case SIGFPE:
622 case SIGHUP:
623 case SIGTERM:
624 case SIGTRAP:
625 if (pipeout)
626 quit("Signal on pipe: cannot recover\n");
627 msg("Rewriting attempted as response to unknown signal.\n");
628 (void)fflush(stderr);
629 (void)fflush(stdout);
630 close_rewind();
631 exit(X_REWRITE);
632 /* NOTREACHED */
633 case SIGSEGV:
634 msg("SIGSEGV: ABORTING!\n");
635 (void)signal(SIGSEGV, SIG_DFL);
636 (void)kill(0, SIGSEGV);
637 /* NOTREACHED */
638 }
639 }
640
641 char *
642 rawname(cp)
643 char *cp;
644 {
645 static char rawbuf[MAXPATHLEN];
646 char *dp = strrchr(cp, '/');
647
648 if (dp == NULL)
649 return (NULL);
650 *dp = '\0';
651 (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
652 *dp = '/';
653 return (rawbuf);
654 }
655
656 /*
657 * obsolete --
658 * Change set of key letters and ordered arguments into something
659 * getopt(3) will like.
660 */
661 static void
662 obsolete(argcp, argvp)
663 int *argcp;
664 char **argvp[];
665 {
666 int argc, flags;
667 char *ap, **argv, *flagsp, **nargv, *p;
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");
682
683 *nargv++ = *argv;
684 argv += 2;
685
686 for (flags = 0; *ap; ++ap) {
687 switch (*ap) {
688 case 'B':
689 case 'b':
690 case 'd':
691 case 'f':
692 case 'h':
693 case 's':
694 case 'T':
695 if (*argv == NULL) {
696 warnx("option requires an argument -- %c", *ap);
697 usage();
698 }
699 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
700 err(1, "malloc");
701 nargv[0][0] = '-';
702 nargv[0][1] = *ap;
703 (void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
704 ++argv;
705 ++nargv;
706 break;
707 default:
708 if (!flags) {
709 *p++ = '-';
710 flags = 1;
711 }
712 *p++ = *ap;
713 break;
714 }
715 }
716
717 /* Terminate flags. */
718 if (flags) {
719 *p = '\0';
720 *nargv++ = flagsp;
721 }
722
723 /* Copy remaining arguments. */
724 while ((*nargv++ = *argv++) != NULL)
725 ;
726
727 /* Update argument count. */
728 *argcp = nargv - *argvp - 1;
729 }
730