main.c revision 1.37 1 /* $NetBSD: main.c,v 1.37 2001/08/08 16:49:54 david 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.37 2001/08/08 16:49:54 david 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 gid_t egid; /* Retain tty privs for notification */
84 int notify; /* notify operator flag */
85 int blockswritten; /* number of blocks written on current tape */
86 int tapeno; /* current tape number */
87 int density; /* density in bytes/0.1" */
88 int ntrec = NTREC; /* # tape blocks in each tape record */
89 int cartridge; /* Assume non-cartridge tape */
90 long dev_bsize = 1; /* recalculated below */
91 long blocksperfile; /* output blocks per file */
92 char *host; /* remote host (if any) */
93 int readcache = -1; /* read cache size (in readblksize blks) */
94 int readblksize = 32 * 1024; /* read block size */
95
96 int main(int, char *[]);
97 static long numarg(char *, long, long);
98 static void obsolete(int *, char **[]);
99 static void usage(void);
100
101 int
102 main(int argc, char *argv[])
103 {
104 ino_t ino;
105 int dirty;
106 struct dinode *dp;
107 struct fstab *dt;
108 struct statfs *mntinfo, fsbuf;
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 dirc;
115 char *mountpoint;
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 /* Save setgid bit for use later */
123 egid = getegid();
124 setegid(getgid());
125
126 tsize = 0; /* Default later, based on 'c' option for cart tapes */
127 if ((tape = getenv("TAPE")) == NULL)
128 tape = _PATH_DEFTAPE;
129 dumpdates = _PATH_DUMPDATES;
130 temp = _PATH_DTMP;
131 strcpy(labelstr, "none"); /* XXX safe strcpy. */
132 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
133 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
134 level = '0';
135
136 if (argc < 2)
137 usage();
138
139 obsolete(&argc, &argv);
140 while ((ch = getopt(argc, argv,
141 "0123456789B:b:cd:ef:h:k:L:nr:s:ST:uWw")) != -1)
142 switch (ch) {
143 /* dump level */
144 case '0': case '1': case '2': case '3': case '4':
145 case '5': case '6': case '7': case '8': case '9':
146 level = ch;
147 break;
148
149 case 'B': /* blocks per output file */
150 blocksperfile = numarg("blocks per file", 1L, 0L);
151 break;
152
153 case 'b': /* blocks per tape write */
154 ntrec = numarg("blocks per write", 1L, 1000L);
155 bflag = 1;
156 break;
157
158 case 'c': /* Tape is cart. not 9-track */
159 cartridge = 1;
160 break;
161
162 case 'd': /* density, in bits per inch */
163 density = numarg("density", 10L, 327670L) / 10;
164 if (density >= 625 && !bflag)
165 ntrec = HIGHDENSITYTREC;
166 break;
167
168 case 'e': /* eject full tapes */
169 eflag = 1;
170 break;
171
172 case 'f': /* output file */
173 tape = optarg;
174 break;
175
176 case 'h':
177 honorlevel = numarg("honor level", 0L, 10L);
178 break;
179
180 case 'k':
181 readblksize = numarg("read block size", 0, 64) * 1024;
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_ABORT);
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(0); /* 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 /*
249 * determine if disk is a subdirectory, and setup appropriately
250 */
251 getfstab(); /* /etc/fstab snarfed */
252 disk = NULL;
253 mountpoint = NULL;
254 dirc = 0;
255 for (i = 0; i < argc; i++) {
256 struct stat sb;
257
258 if (lstat(argv[i], &sb) == -1)
259 quit("Cannot stat %s: %s\n", argv[i], strerror(errno));
260 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
261 disk = argv[i];
262 multicheck:
263 if (dirc != 0)
264 quit(
265 "Can't dump a mountpoint and a filelist\n");
266 break;
267 }
268 if ((dt = fstabsearch(argv[i])) != NULL) {
269 disk = dt->fs_spec;
270 mountpoint = xstrdup(dt->fs_file);
271 goto multicheck;
272 }
273 if (statfs(argv[i], &fsbuf) == -1)
274 quit("Cannot statfs %s: %s\n", argv[i],
275 strerror(errno));
276 disk = fsbuf.f_mntfromname;
277 if (strcmp(argv[i], fsbuf.f_mntonname) == 0)
278 goto multicheck;
279 if (mountpoint == NULL) {
280 mountpoint = xstrdup(fsbuf.f_mntonname);
281 if (uflag) {
282 msg("Ignoring u flag for subdir dump\n");
283 uflag = 0;
284 }
285 if (level > '0') {
286 msg("Subdir dump is done at level 0\n");
287 level = '0';
288 }
289 msg("Dumping sub files/directories from %s\n",
290 mountpoint);
291 } else {
292 if (strcmp(mountpoint, fsbuf.f_mntonname) != 0)
293 quit("%s is not on %s\n", argv[i], mountpoint);
294 }
295 msg("Dumping file/directory %s\n", argv[i]);
296 dirc++;
297 }
298 if (mountpoint)
299 free(mountpoint);
300
301 if (dirc == 0) {
302 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_ABORT);
309 }
310 }
311 if (Tflag && uflag) {
312 (void)fprintf(stderr,
313 "You cannot use the T and u flags together.\n");
314 exit(X_ABORT);
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 {
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_ABORT);
346 #else
347 (void)fprintf(stderr, "remote dump not enabled\n");
348 exit(X_ABORT);
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
369 /*
370 * disk can be either the full special file name, or
371 * the file system name.
372 */
373 mountpoint = NULL;
374 if ((dt = fstabsearch(disk)) != NULL) {
375 disk = rawname(dt->fs_spec);
376 mountpoint = dt->fs_file;
377 msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB);
378 } else if ((mntinfo = mntinfosearch(disk)) != NULL) {
379 disk = rawname(mntinfo->f_mntfromname);
380 mountpoint = mntinfo->f_mntonname;
381 msg("Found %s on %s in mount table\n", disk, mountpoint);
382 }
383 if (mountpoint != NULL) {
384 if (dirc != 0)
385 (void)snprintf(spcl.c_filesys, NAMELEN,
386 "a subset of %s", mountpoint);
387 else
388 (void)strncpy(spcl.c_filesys, mountpoint, NAMELEN);
389 } else {
390 (void)strncpy(spcl.c_filesys, "an unlisted file system",
391 NAMELEN);
392 }
393 (void)strncpy(spcl.c_dev, disk, NAMELEN);
394 (void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1);
395 (void)gethostname(spcl.c_host, NAMELEN);
396 spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
397
398 if ((diskfd = open(disk, O_RDONLY)) < 0) {
399 msg("Cannot open %s\n", disk);
400 exit(X_ABORT);
401 }
402 sync();
403
404 needswap = fs_read_sblock(sblock_buf);
405
406 spcl.c_level = iswap32(level - '0');
407 spcl.c_type = iswap32(TS_TAPE);
408 spcl.c_date = iswap32(spcl.c_date);
409 spcl.c_ddate = iswap32(spcl.c_ddate);
410 if (!Tflag)
411 getdumptime(); /* /etc/dumpdates snarfed */
412
413 date = iswap32(spcl.c_date);
414 msg("Date of this level %c dump: %s", level,
415 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
416 date = iswap32(spcl.c_ddate);
417 msg("Date of last level %c dump: %s", lastlevel,
418 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
419 msg("Dumping ");
420 if (dirc != 0)
421 msgtail("a subset of ");
422 msgtail("%s (%s) ", disk, spcl.c_filesys);
423 if (host)
424 msgtail("to %s on host %s\n", tape, host);
425 else
426 msgtail("to %s\n", tape);
427 msg("Label: %s\n", labelstr);
428
429 ufsib = fs_parametrize();
430
431 dev_bshift = ffs(dev_bsize) - 1;
432 if (dev_bsize != (1 << dev_bshift))
433 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
434 tp_bshift = ffs(TP_BSIZE) - 1;
435 if (TP_BSIZE != (1 << tp_bshift))
436 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
437 maxino = fs_maxino();
438 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
439 usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
440 dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
441 dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
442 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
443
444 nonodump = iswap32(spcl.c_level) < honorlevel;
445
446 initcache(readcache, readblksize);
447
448 (void)signal(SIGINFO, statussig);
449
450 msg("mapping (Pass I) [regular files]\n");
451 anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
452 (dirc ? argv : NULL));
453
454 msg("mapping (Pass II) [directories]\n");
455 while (anydirskipped) {
456 anydirskipped = mapdirs(maxino, &tapesize);
457 }
458
459 if (pipeout) {
460 tapesize += 10; /* 10 trailer blocks */
461 msg("estimated %ld tape blocks.\n", tapesize);
462 } else {
463 double fetapes;
464
465 if (blocksperfile)
466 fetapes = (double) tapesize / blocksperfile;
467 else if (cartridge) {
468 /* Estimate number of tapes, assuming streaming stops at
469 the end of each block written, and not in mid-block.
470 Assume no erroneous blocks; this can be compensated
471 for with an artificially low tape size. */
472 fetapes =
473 ( tapesize /* blocks */
474 * TP_BSIZE /* bytes/block */
475 * (1.0/density) /* 0.1" / byte */
476 +
477 tapesize /* blocks */
478 * (1.0/ntrec) /* streaming-stops per block */
479 * 15.48 /* 0.1" / streaming-stop */
480 ) * (1.0 / tsize ); /* tape / 0.1" */
481 } else {
482 /* Estimate number of tapes, for old fashioned 9-track
483 tape */
484 int tenthsperirg = (density == 625) ? 3 : 7;
485 fetapes =
486 ( tapesize /* blocks */
487 * TP_BSIZE /* bytes / block */
488 * (1.0/density) /* 0.1" / byte */
489 +
490 tapesize /* blocks */
491 * (1.0/ntrec) /* IRG's / block */
492 * tenthsperirg /* 0.1" / IRG */
493 ) * (1.0 / tsize ); /* tape / 0.1" */
494 }
495 etapes = fetapes; /* truncating assignment */
496 etapes++;
497 /* count the dumped inodes map on each additional tape */
498 tapesize += (etapes - 1) *
499 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
500 tapesize += etapes + 10; /* headers + 10 trailer blks */
501 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
502 tapesize, fetapes);
503 }
504 /*
505 * If the user only wants an estimate of the number of
506 * tapes, exit now.
507 */
508 if (just_estimate)
509 exit(0);
510
511 /*
512 * Allocate tape buffer.
513 */
514 if (!alloctape())
515 quit("can't allocate tape buffers - try a smaller blocking factor.\n");
516
517 startnewtape(1);
518 (void)time((time_t *)&(tstart_writing));
519 xferrate = 0;
520 dumpmap(usedinomap, TS_CLRI, maxino - 1);
521
522 msg("dumping (Pass III) [directories]\n");
523 dirty = 0; /* XXX just to get gcc to shut up */
524 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
525 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
526 dirty = *map++;
527 else
528 dirty >>= 1;
529 if ((dirty & 1) == 0)
530 continue;
531 /*
532 * Skip directory inodes deleted and maybe reallocated
533 */
534 dp = getino(ino);
535 if ((dp->di_mode & IFMT) != IFDIR)
536 continue;
537 (void)dumpino(dp, ino);
538 }
539
540 msg("dumping (Pass IV) [regular files]\n");
541 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
542 int mode;
543
544 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
545 dirty = *map++;
546 else
547 dirty >>= 1;
548 if ((dirty & 1) == 0)
549 continue;
550 /*
551 * Skip inodes deleted and reallocated as directories.
552 */
553 dp = getino(ino);
554 mode = dp->di_mode & IFMT;
555 if (mode == IFDIR)
556 continue;
557 (void)dumpino(dp, ino);
558 }
559
560 spcl.c_type = iswap32(TS_END);
561 for (i = 0; i < ntrec; i++)
562 writeheader(maxino - 1);
563 if (pipeout)
564 msg("%d tape blocks\n",iswap32(spcl.c_tapea));
565 else
566 msg("%d tape blocks on %d volume%s\n",
567 iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
568 (iswap32(spcl.c_volume) == 1) ? "" : "s");
569 tnow = do_stats();
570 date = iswap32(spcl.c_date);
571 msg("Date of this level %c dump: %s", level,
572 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
573 msg("Date this dump completed: %s", ctime(&tnow));
574 msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
575 putdumptime();
576 trewind(0);
577 broadcast("DUMP IS DONE!\7\7\n");
578 msg("DUMP IS DONE\n");
579 Exit(X_FINOK);
580 /* NOTREACHED */
581 exit(X_FINOK); /* XXX: to satisfy gcc */
582 }
583
584 static void
585 usage(void)
586 {
587
588 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
589 "usage: dump [-0123456789cenu] [-B records] [-b blocksize] [-d density]",
590 " [-f file] [-h level] [-k read block size] [-L label]",
591 " [-r read cache size] [-s feet] [-T date] filesystem",
592 " dump [-W | -w]");
593 exit(1);
594 }
595
596 /*
597 * Pick up a numeric argument. It must be nonnegative and in the given
598 * range (except that a vmax of 0 means unlimited).
599 */
600 static long
601 numarg(char *meaning, long vmin, long vmax)
602 {
603 char *p;
604 long val;
605
606 val = strtol(optarg, &p, 10);
607 if (*p)
608 errx(1, "illegal %s -- %s", meaning, optarg);
609 if (val < vmin || (vmax && val > vmax))
610 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
611 return (val);
612 }
613
614 void
615 sig(int signo)
616 {
617
618 switch(signo) {
619 case SIGALRM:
620 case SIGBUS:
621 case SIGFPE:
622 case SIGHUP:
623 case SIGTERM:
624 case SIGTRAP:
625 if (pipeout)
626 quit("Signal on pipe: cannot recover\n");
627 msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]);
628 (void)fflush(stderr);
629 (void)fflush(stdout);
630 close_rewind();
631 exit(X_REWRITE);
632 /* NOTREACHED */
633 case SIGSEGV:
634 msg("SIGSEGV: ABORTING!\n");
635 (void)signal(SIGSEGV, SIG_DFL);
636 (void)kill(0, SIGSEGV);
637 /* NOTREACHED */
638 }
639 }
640
641 char *
642 rawname(char *cp)
643 {
644 static char rawbuf[MAXPATHLEN];
645 char *dp = strrchr(cp, '/');
646
647 if (dp == NULL)
648 return (NULL);
649 *dp = '\0';
650 (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
651 *dp = '/';
652 return (rawbuf);
653 }
654
655 /*
656 * obsolete --
657 * Change set of key letters and ordered arguments into something
658 * getopt(3) will like.
659 */
660 static void
661 obsolete(int *argcp, char **argvp[])
662 {
663 int argc, flags;
664 char *ap, **argv, *flagsp, **nargv, *p;
665
666 /* Setup. */
667 argv = *argvp;
668 argc = *argcp;
669
670 /* Return if no arguments or first argument has leading dash. */
671 ap = argv[1];
672 if (argc == 1 || *ap == '-')
673 return;
674
675 /* Allocate space for new arguments. */
676 *argvp = nargv = xmalloc((argc + 1) * sizeof(char *));
677 p = flagsp = xmalloc(strlen(ap) + 2);
678
679 *nargv++ = *argv;
680 argv += 2;
681
682 for (flags = 0; *ap; ++ap) {
683 switch (*ap) {
684 case 'B':
685 case 'b':
686 case 'd':
687 case 'f':
688 case 'h':
689 case 's':
690 case 'T':
691 if (*argv == NULL) {
692 warnx("option requires an argument -- %c", *ap);
693 usage();
694 }
695 nargv[0] = xmalloc(strlen(*argv) + 2 + 1);
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
726
727 void *
728 xcalloc(size_t number, size_t size)
729 {
730 void *p;
731
732 p = calloc(number, size);
733 if (p == NULL)
734 quit("%s\n", strerror(errno));
735 return (p);
736 }
737
738 void *
739 xmalloc(size_t size)
740 {
741 void *p;
742
743 p = malloc(size);
744 if (p == NULL)
745 quit("%s\n", strerror(errno));
746 return (p);
747 }
748
749 char *
750 xstrdup(const char *str)
751 {
752 char *p;
753
754 p = strdup(str);
755 if (p == NULL)
756 quit("%s\n", strerror(errno));
757 return (p);
758 }
759