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