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