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