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