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