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