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