main.c revision 1.58 1 /* $NetBSD: main.c,v 1.58 2005/04/19 07:26:38 hannken 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.58 2005/04/19 07:26:38 hannken 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 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(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;
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 (strchr(tape, ':')) {
362 host = tape;
363 tape = strchr(host, ':');
364 *tape++ = '\0';
365 #ifdef RDUMP
366 if (rmthost(host) == 0)
367 exit(X_STARTUP);
368 #else
369 (void)fprintf(stderr, "remote dump not enabled\n");
370 exit(X_STARTUP);
371 #endif
372 }
373
374 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
375 signal(SIGHUP, sig);
376 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
377 signal(SIGTRAP, sig);
378 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
379 signal(SIGFPE, sig);
380 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
381 signal(SIGBUS, sig);
382 #if 0
383 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
384 signal(SIGSEGV, sig);
385 #endif
386 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
387 signal(SIGTERM, sig);
388 if (signal(SIGINT, interrupt) == SIG_IGN)
389 signal(SIGINT, SIG_IGN);
390
391 /*
392 * disk can be either the full special file name, or
393 * the file system name.
394 */
395 mountpoint = NULL;
396 mntinfo = mntinfosearch(disk);
397 if ((dt = fstabsearch(disk)) != NULL) {
398 disk = rawname(dt->fs_spec);
399 mountpoint = dt->fs_file;
400 msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB);
401 } else if (mntinfo != NULL) {
402 disk = rawname(mntinfo->f_mntfromname);
403 mountpoint = mntinfo->f_mntonname;
404 msg("Found %s on %s in mount table\n", disk, mountpoint);
405 }
406 if (mountpoint != NULL) {
407 if (dirc != 0)
408 (void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
409 "a subset of %s", mountpoint);
410 else
411 (void)strlcpy(spcl.c_filesys, mountpoint,
412 sizeof(spcl.c_filesys));
413 } else if (Fflag) {
414 (void)strlcpy(spcl.c_filesys, "a file system image",
415 sizeof(spcl.c_filesys));
416 } else {
417 (void)strlcpy(spcl.c_filesys, "an unlisted file system",
418 sizeof(spcl.c_filesys));
419 }
420 (void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev));
421 (void)strlcpy(spcl.c_label, labelstr, sizeof(spcl.c_label));
422 (void)gethostname(spcl.c_host, sizeof(spcl.c_host));
423 spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
424
425 if ((snap_backup != NULL || snap_internal) && mntinfo == NULL) {
426 msg("WARNING: Cannot use -x or -X on unmounted file system.\n");
427 snap_backup = NULL;
428 snap_internal = 0;
429 }
430 if (snap_backup != NULL || snap_internal) {
431 diskfd = snap_open(mntinfo->f_mntonname, snap_backup, &tnow);
432 if (diskfd < 0) {
433 msg("Cannot open snapshot of %s\n",
434 mntinfo->f_mntonname);
435 exit(X_STARTUP);
436 }
437 spcl.c_date = tnow;
438 } else {
439 if ((diskfd = open(disk, O_RDONLY)) < 0) {
440 msg("Cannot open %s\n", disk);
441 exit(X_STARTUP);
442 }
443 }
444 sync();
445
446 needswap = fs_read_sblock(sblock_buf);
447
448 spcl.c_level = iswap32(level - '0');
449 spcl.c_type = iswap32(TS_TAPE);
450 spcl.c_date = iswap32(spcl.c_date);
451 spcl.c_ddate = iswap32(spcl.c_ddate);
452 if (!Tflag)
453 getdumptime(); /* /etc/dumpdates snarfed */
454
455 date = iswap32(spcl.c_date);
456 msg("Date of this level %c dump: %s", level,
457 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
458 date = iswap32(spcl.c_ddate);
459 msg("Date of last level %c dump: %s", lastlevel,
460 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
461 msg("Dumping ");
462 if (snap_backup != NULL)
463 msgtail("a snapshot of ");
464 if (dirc != 0)
465 msgtail("a subset of ");
466 msgtail("%s (%s) ", disk, spcl.c_filesys);
467 if (host)
468 msgtail("to %s on host %s\n", tape, host);
469 else
470 msgtail("to %s\n", tape);
471 msg("Label: %s\n", labelstr);
472
473 ufsib = fs_parametrize();
474
475 dev_bshift = ffs(dev_bsize) - 1;
476 if (dev_bsize != (1 << dev_bshift))
477 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
478 tp_bshift = ffs(TP_BSIZE) - 1;
479 if (TP_BSIZE != (1 << tp_bshift))
480 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
481 maxino = fs_maxino();
482 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
483 usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
484 dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
485 dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
486 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
487
488 nonodump = iswap32(spcl.c_level) < honorlevel;
489
490 initcache(readcache, readblksize);
491
492 (void)signal(SIGINFO, statussig);
493
494 msg("mapping (Pass I) [regular files]\n");
495 anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
496 (dirc ? argv : NULL));
497
498 msg("mapping (Pass II) [directories]\n");
499 while (anydirskipped) {
500 anydirskipped = mapdirs(maxino, &tapesize);
501 }
502
503 if (pipeout || unlimited) {
504 tapesize += 10; /* 10 trailer blocks */
505 msg("estimated %llu tape blocks.\n",
506 (unsigned long long)tapesize);
507 } else {
508 double fetapes;
509
510 if (blocksperfile)
511 fetapes = (double) tapesize / blocksperfile;
512 else if (cartridge) {
513 /* Estimate number of tapes, assuming streaming stops at
514 the end of each block written, and not in mid-block.
515 Assume no erroneous blocks; this can be compensated
516 for with an artificially low tape size. */
517 fetapes =
518 ( (double) tapesize /* blocks */
519 * TP_BSIZE /* bytes/block */
520 * (1.0/density) /* 0.1" / byte */
521 +
522 (double) tapesize /* blocks */
523 * (1.0/ntrec) /* streaming-stops per block */
524 * 15.48 /* 0.1" / streaming-stop */
525 ) * (1.0 / tsize ); /* tape / 0.1" */
526 } else {
527 /* Estimate number of tapes, for old fashioned 9-track
528 tape */
529 int tenthsperirg = (density == 625) ? 3 : 7;
530 fetapes =
531 ( tapesize /* blocks */
532 * TP_BSIZE /* bytes / block */
533 * (1.0/density) /* 0.1" / byte */
534 +
535 tapesize /* blocks */
536 * (1.0/ntrec) /* IRG's / block */
537 * tenthsperirg /* 0.1" / IRG */
538 ) * (1.0 / tsize ); /* tape / 0.1" */
539 }
540 etapes = fetapes; /* truncating assignment */
541 etapes++;
542 /* count the dumped inodes map on each additional tape */
543 tapesize += (etapes - 1) *
544 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
545 tapesize += etapes + 10; /* headers + 10 trailer blks */
546 msg("estimated %llu tape blocks on %3.2f tape(s).\n",
547 (unsigned long long)tapesize, fetapes);
548 }
549 /*
550 * If the user only wants an estimate of the number of
551 * tapes, exit now.
552 */
553 if (just_estimate)
554 exit(X_FINOK);
555
556 /*
557 * Allocate tape buffer.
558 */
559 if (!alloctape())
560 quit("can't allocate tape buffers - try a smaller blocking factor.\n");
561
562 startnewtape(1);
563 (void)time((time_t *)&(tstart_writing));
564 xferrate = 0;
565 dumpmap(usedinomap, TS_CLRI, maxino - 1);
566
567 msg("dumping (Pass III) [directories]\n");
568 dirty = 0; /* XXX just to get gcc to shut up */
569 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
570 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
571 dirty = *map++;
572 else
573 dirty >>= 1;
574 if ((dirty & 1) == 0)
575 continue;
576 /*
577 * Skip directory inodes deleted and maybe reallocated
578 */
579 dp = getino(ino);
580 if ((DIP(dp, mode) & IFMT) != IFDIR)
581 continue;
582 (void)dumpino(dp, ino);
583 }
584
585 msg("dumping (Pass IV) [regular files]\n");
586 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
587 int mode;
588
589 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
590 dirty = *map++;
591 else
592 dirty >>= 1;
593 if ((dirty & 1) == 0)
594 continue;
595 /*
596 * Skip inodes deleted and reallocated as directories.
597 */
598 dp = getino(ino);
599 mode = DIP(dp, mode) & IFMT;
600 if (mode == IFDIR)
601 continue;
602 (void)dumpino(dp, ino);
603 }
604
605 spcl.c_type = iswap32(TS_END);
606 for (i = 0; i < ntrec; i++)
607 writeheader(maxino - 1);
608 if (pipeout)
609 msg("%d tape blocks\n",iswap32(spcl.c_tapea));
610 else
611 msg("%d tape blocks on %d volume%s\n",
612 iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
613 (iswap32(spcl.c_volume) == 1) ? "" : "s");
614 tnow = do_stats();
615 date = iswap32(spcl.c_date);
616 msg("Date of this level %c dump: %s", level,
617 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
618 msg("Date this dump completed: %s", ctime(&tnow));
619 msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
620 putdumptime();
621 trewind(0);
622 broadcast("DUMP IS DONE!\a\a\n");
623 msg("DUMP IS DONE\n");
624 Exit(X_FINOK);
625 /* NOTREACHED */
626 exit(X_FINOK); /* XXX: to satisfy gcc */
627 }
628
629 static void
630 usage(void)
631 {
632 const char *prog = getprogname();
633
634 (void)fprintf(stderr,
635 "usage: %s [-0123456789aceFnStuX] [-B records] [-b blocksize]\n"
636 " [-d density] [-f file] [-h level] [-k read-blocksize]\n"
637 " [-L label] [-l timeout] [-r read-cache] [-s feet]\n"
638 " [-T date] [-x snap-backup] files-to-dump\n"
639 " %s [-W | -w]\n", prog, prog);
640 exit(X_STARTUP);
641 }
642
643 /*
644 * Pick up a numeric argument. It must be nonnegative and in the given
645 * range (except that a vmax of 0 means unlimited).
646 */
647 static long
648 numarg(char *meaning, long vmin, long vmax)
649 {
650 char *p;
651 long val;
652
653 val = strtol(optarg, &p, 10);
654 if (*p)
655 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
656 if (val < vmin || (vmax && val > vmax))
657 errx(X_STARTUP, "%s must be between %ld and %ld",
658 meaning, vmin, vmax);
659 return (val);
660 }
661
662 void
663 sig(int signo)
664 {
665
666 switch(signo) {
667 case SIGALRM:
668 case SIGBUS:
669 case SIGFPE:
670 case SIGHUP:
671 case SIGTERM:
672 case SIGTRAP:
673 if (pipeout)
674 quit("Signal on pipe: cannot recover\n");
675 msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]);
676 (void)fflush(stderr);
677 (void)fflush(stdout);
678 close_rewind();
679 exit(X_REWRITE);
680 /* NOTREACHED */
681 case SIGSEGV:
682 msg("SIGSEGV: ABORTING!\n");
683 (void)signal(SIGSEGV, SIG_DFL);
684 (void)kill(0, SIGSEGV);
685 /* NOTREACHED */
686 }
687 }
688
689 char *
690 rawname(char *cp)
691 {
692 static char rawbuf[MAXPATHLEN];
693 char *dp = strrchr(cp, '/');
694
695 if (dp == NULL)
696 return (NULL);
697 *dp = '\0';
698 (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
699 *dp = '/';
700 return (rawbuf);
701 }
702
703 /*
704 * obsolete --
705 * Change set of key letters and ordered arguments into something
706 * getopt(3) will like.
707 */
708 static void
709 obsolete(int *argcp, char **argvp[])
710 {
711 int argc, flags;
712 char *ap, **argv, *flagsp, **nargv, *p;
713
714 /* Setup. */
715 argv = *argvp;
716 argc = *argcp;
717
718 /* Return if no arguments or first argument has leading dash. */
719 ap = argv[1];
720 if (argc == 1 || *ap == '-')
721 return;
722
723 /* Allocate space for new arguments. */
724 *argvp = nargv = xmalloc((argc + 1) * sizeof(char *));
725 p = flagsp = xmalloc(strlen(ap) + 2);
726
727 *nargv++ = *argv;
728 argv += 2;
729
730 for (flags = 0; *ap; ++ap) {
731 switch (*ap) {
732 case 'B':
733 case 'b':
734 case 'd':
735 case 'f':
736 case 'h':
737 case 's':
738 case 'T':
739 case 'x':
740 if (*argv == NULL) {
741 warnx("option requires an argument -- %c", *ap);
742 usage();
743 }
744 nargv[0] = xmalloc(strlen(*argv) + 2 + 1);
745 nargv[0][0] = '-';
746 nargv[0][1] = *ap;
747 (void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
748 ++argv;
749 ++nargv;
750 break;
751 default:
752 if (!flags) {
753 *p++ = '-';
754 flags = 1;
755 }
756 *p++ = *ap;
757 break;
758 }
759 }
760
761 /* Terminate flags. */
762 if (flags) {
763 *p = '\0';
764 *nargv++ = flagsp;
765 }
766
767 /* Copy remaining arguments. */
768 while ((*nargv++ = *argv++) != NULL)
769 ;
770
771 /* Update argument count. */
772 *argcp = nargv - *argvp - 1;
773 }
774
775
776 void *
777 xcalloc(size_t number, size_t size)
778 {
779 void *p;
780
781 p = calloc(number, size);
782 if (p == NULL)
783 quit("%s\n", strerror(errno));
784 return (p);
785 }
786
787 void *
788 xmalloc(size_t size)
789 {
790 void *p;
791
792 p = malloc(size);
793 if (p == NULL)
794 quit("%s\n", strerror(errno));
795 return (p);
796 }
797
798 char *
799 xstrdup(const char *str)
800 {
801 char *p;
802
803 p = strdup(str);
804 if (p == NULL)
805 quit("%s\n", strerror(errno));
806 return (p);
807 }
808