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