]> git.wh0rd.org - dump.git/blob - dump/main.c
Version 0.4b5.
[dump.git] / dump / main.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
6 *
7 */
8
9 /*-
10 * Copyright (c) 1980, 1991, 1993, 1994
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42 #ifndef lint
43 static const char copyright[] =
44 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
45 The Regents of the University of California. All rights reserved.\n";
46 #endif /* not lint */
47
48 #ifndef lint
49 #if 0
50 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
51 #endif
52 static const char rcsid[] =
53 "$Id: main.c,v 1.2 1999/10/11 12:53:22 stelian Exp $";
54 #endif /* not lint */
55
56 #include <sys/param.h>
57 #include <sys/time.h>
58 #ifdef __linux__
59 #include <linux/ext2_fs.h>
60 #include <sys/stat.h>
61 #include <bsdcompat.h>
62 #else
63 #ifdef sunos
64 #include <sys/vnode.h>
65
66 #include <ufs/inode.h>
67 #include <ufs/fs.h>
68 #else
69 #include <ufs/ufs/dinode.h>
70 #include <ufs/ffs/fs.h>
71 #endif
72 #endif
73
74 #include <protocols/dumprestore.h>
75
76 #include <ctype.h>
77 #include <err.h>
78 #include <fcntl.h>
79 #include <fstab.h>
80 #include <signal.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <unistd.h>
85
86 #ifdef __linux__
87 #include <ext2fs/ext2fs.h>
88 #endif
89
90 #include "dump.h"
91 #include "pathnames.h"
92
93 #ifndef SBOFF
94 #define SBOFF (SBLOCK * DEV_BSIZE)
95 #endif
96
97 int notify = 0; /* notify operator flag */
98 int blockswritten = 0; /* number of blocks written on current tape */
99 int tapeno = 0; /* current tape number */
100 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */
101 int ntrec = NTREC; /* # tape blocks in each tape record */
102 int cartridge = 0; /* Assume non-cartridge tape */
103 int dokerberos = 0; /* Use Kerberos authentication */
104 long dev_bsize = 1; /* recalculated below */
105 long blocksperfile; /* output blocks per file */
106 char *host = NULL; /* remote host (if any) */
107
108 #ifdef __linux__
109 char *__progname;
110 #endif
111
112 static long numarg __P((char *, long, long));
113 static void obsolete __P((int *, char **[]));
114 static void usage __P((void));
115
116 int
117 main(argc, argv)
118 int argc;
119 char *argv[];
120 {
121 register ino_t ino;
122 register int dirty;
123 register struct dinode *dp;
124 register struct fstab *dt;
125 register char *map;
126 register int ch;
127 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
128 ino_t maxino;
129 #ifdef __linux__
130 errcode_t retval;
131 char directory[NAME_MAX];
132 char pathname[NAME_MAX];
133 #endif
134 char labelstr[LBLSIZE];
135
136 spcl.c_date = 0;
137 #ifdef __linux__
138 (void)time4(&spcl.c_date);
139 #else
140 (void)time((time_t *)&spcl.c_date);
141 #endif
142
143 #ifdef __linux__
144 __progname = argv[0];
145 directory[0] = 0;
146 initialize_ext2_error_table();
147 #endif
148
149 tsize = 0; /* Default later, based on 'c' option for cart tapes */
150 if ((tape = getenv("TAPE")) == NULL)
151 tape = _PATH_DEFTAPE;
152 dumpdates = _PATH_DUMPDATES;
153 temp = _PATH_DTMP;
154 strcpy(labelstr, "none"); /* XXX safe strcpy. */
155 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
156 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
157 level = '0';
158
159 if (argc < 2)
160 usage();
161
162 obsolete(&argc, &argv);
163 #ifdef KERBEROS
164 #define optstring "0123456789aB:b:cd:f:h:kL:ns:T:uWw"
165 #else
166 #define optstring "0123456789aB:b:cd:f:h:L:ns:T:uWw"
167 #endif
168 while ((ch = getopt(argc, argv, optstring)) != -1)
169 #undef optstring
170 switch (ch) {
171 /* dump level */
172 case '0': case '1': case '2': case '3': case '4':
173 case '5': case '6': case '7': case '8': case '9':
174 level = ch;
175 break;
176
177 case 'a': /* `auto-size', Write to EOM. */
178 unlimited = 1;
179 break;
180
181 case 'B': /* blocks per output file */
182 blocksperfile = numarg("number of blocks per file",
183 1L, 0L);
184 break;
185
186 case 'b': /* blocks per tape write */
187 ntrec = numarg("number of blocks per write",
188 1L, 1000L);
189 break;
190
191 case 'c': /* Tape is cart. not 9-track */
192 cartridge = 1;
193 break;
194
195 case 'd': /* density, in bits per inch */
196 density = numarg("density", 10L, 327670L) / 10;
197 if (density >= 625 && !bflag)
198 ntrec = HIGHDENSITYTREC;
199 break;
200
201 case 'f': /* output file */
202 tape = optarg;
203 break;
204
205 case 'h':
206 honorlevel = numarg("honor level", 0L, 10L);
207 break;
208
209 #ifdef KERBEROS
210 case 'k':
211 dokerberos = 1;
212 break;
213 #endif
214
215 case 'L':
216 /*
217 * Note that although there are LBLSIZE characters,
218 * the last must be '\0', so the limit on strlen()
219 * is really LBLSIZE-1.
220 */
221 strncpy(labelstr, optarg, LBLSIZE);
222 labelstr[LBLSIZE-1] = '\0';
223 if (strlen(optarg) > LBLSIZE-1) {
224 msg(
225 "WARNING Label `%s' is larger than limit of %d characters.\n",
226 optarg, LBLSIZE-1);
227 msg("WARNING: Using truncated label `%s'.\n",
228 labelstr);
229 }
230 break;
231
232 case 'n': /* notify operators */
233 notify = 1;
234 break;
235
236 case 's': /* tape size, feet */
237 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
238 break;
239
240 case 'T': /* time of last dump */
241 spcl.c_ddate = unctime(optarg);
242 if (spcl.c_ddate < 0) {
243 (void)fprintf(stderr, "bad time \"%s\"\n",
244 optarg);
245 exit(X_STARTUP);
246 }
247 Tflag = 1;
248 lastlevel = '?';
249 argc--;
250 argv++;
251 break;
252
253 case 'u': /* update /etc/dumpdates */
254 uflag = 1;
255 break;
256
257 case 'W': /* what to do */
258 case 'w':
259 lastdump(ch);
260 exit(X_FINOK); /* do nothing else */
261
262 default:
263 usage();
264 }
265 argc -= optind;
266 argv += optind;
267
268 if (argc < 1) {
269 (void)fprintf(stderr, "Must specify disk or filesystem\n");
270 exit(X_STARTUP);
271 }
272 disk = *argv++;
273 argc--;
274 if (argc >= 1) {
275 (void)fprintf(stderr, "Unknown arguments to dump:");
276 while (argc--)
277 (void)fprintf(stderr, " %s", *argv++);
278 (void)fprintf(stderr, "\n");
279 exit(X_STARTUP);
280 }
281 if (Tflag && uflag) {
282 (void)fprintf(stderr,
283 "You cannot use the T and u flags together.\n");
284 exit(X_STARTUP);
285 }
286 if (strcmp(tape, "-") == 0) {
287 pipeout++;
288 tape = "standard output";
289 }
290
291 if (blocksperfile)
292 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
293 else if (!unlimited) {
294 /*
295 * Determine how to default tape size and density
296 *
297 * density tape size
298 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
299 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
300 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
301 * (450*4 - slop)
302 * hilit19 hits again: "
303 */
304 if (density == 0)
305 density = cartridge ? 100 : 160;
306 if (tsize == 0)
307 tsize = cartridge ? 1700L*120L : 2300L*120L;
308 }
309
310 if (strchr(tape, ':')) {
311 host = tape;
312 tape = strchr(host, ':');
313 *tape++ = '\0';
314 #ifdef RDUMP
315 if (index(tape, '\n')) {
316 (void)fprintf(stderr, "invalid characters in tape\n");
317 exit(X_STARTUP);
318 }
319 if (rmthost(host) == 0)
320 exit(X_STARTUP);
321 #else
322 (void)fprintf(stderr, "remote dump not enabled\n");
323 exit(X_STARTUP);
324 #endif
325 }
326 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
327
328 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
329 signal(SIGHUP, sig);
330 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
331 signal(SIGTRAP, sig);
332 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
333 signal(SIGFPE, sig);
334 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
335 signal(SIGBUS, sig);
336 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
337 signal(SIGSEGV, sig);
338 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
339 signal(SIGTERM, sig);
340 if (signal(SIGINT, interrupt) == SIG_IGN)
341 signal(SIGINT, SIG_IGN);
342
343 set_operators(); /* /etc/group snarfed */
344 getfstab(); /* /etc/fstab snarfed */
345 /*
346 * disk can be either the full special file name,
347 * the suffix of the special file name,
348 * the special name missing the leading '/',
349 * the file system name with or without the leading '/'.
350 */
351 dt = fstabsearch(disk);
352 if (dt != NULL) {
353 disk = rawname(dt->fs_spec);
354 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
355 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
356 #ifdef __linux__
357 } else {
358 /*
359 * The argument was not found in the fstab
360 * assume that this is a subtree and search for it
361 */
362 #ifdef HAVE_REALPATH
363 if (realpath(disk, pathname) == NULL)
364 #endif
365 strcpy(pathname, disk);
366 dt = fstabsearchdir(pathname, directory);
367 if (dt != NULL) {
368 char name[MAXPATHLEN];
369 (void)sprintf(name, "%s (dir %s)",
370 dt->fs_spec, directory);
371 (void)strncpy(spcl.c_dev, name, NAMELEN);
372 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
373 disk = rawname(dt->fs_spec);
374 } else {
375 (void)strncpy(spcl.c_dev, disk, NAMELEN);
376 (void)strncpy(spcl.c_filesys, "an unlisted file system",
377 NAMELEN);
378 }
379 }
380 #else
381 } else {
382 (void)strncpy(spcl.c_dev, disk, NAMELEN);
383 (void)strncpy(spcl.c_filesys, "an unlisted file system",
384 NAMELEN);
385 }
386 #endif
387 spcl.c_dev[NAMELEN-1]='\0';
388 spcl.c_filesys[NAMELEN-1]='\0';
389 (void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1);
390 (void)gethostname(spcl.c_host, NAMELEN);
391 spcl.c_level = level - '0';
392 spcl.c_type = TS_TAPE;
393 if (!Tflag)
394 getdumptime(); /* /etc/dumpdates snarfed */
395
396 msg("Date of this level %c dump: %s", level,
397 #ifdef __linux
398 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
399 #else
400 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
401 #endif
402 msg("Date of last level %c dump: %s", lastlevel,
403 #ifdef __linux__
404 spcl.c_ddate == 0 ? "the epoch\n" : ctime4(&spcl.c_ddate));
405 #else
406 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
407 #endif
408 msg("Dumping %s ", disk);
409 if (dt != NULL)
410 msgtail("(%s) ", dt->fs_file);
411 if (host)
412 msgtail("to %s on host %s\n", tape, host);
413 else
414 msgtail("to %s\n", tape);
415 msg("Label: %s\n", labelstr);
416
417 #ifdef __linux__
418 retval = ext2fs_open(disk, 0, 0, 0, unix_io_manager, &fs);
419 if (retval) {
420 com_err(disk, retval, "while opening filesystem");
421 if (retval == EXT2_ET_REV_TOO_HIGH)
422 printf ("Get a newer version of dump!\n");
423 exit(X_STARTUP);
424 }
425 if (fs->super->s_rev_level > DUMP_CURRENT_REV) {
426 com_err(disk, retval, "while opening filesystem");
427 printf ("Get a newer version of dump!\n");
428 exit(X_STARTUP);
429 }
430 if ((diskfd = open(disk, O_RDONLY)) < 0) {
431 msg("Cannot open %s\n", disk);
432 exit(X_STARTUP);
433 }
434 sync();
435 dev_bsize = DEV_BSIZE;
436 dev_bshift = ffs(dev_bsize) - 1;
437 if (dev_bsize != (1 << dev_bshift))
438 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
439 tp_bshift = ffs(TP_BSIZE) - 1;
440 if (TP_BSIZE != (1 << tp_bshift))
441 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
442 maxino = fs->super->s_inodes_count;
443 #if 0
444 spcl.c_flags |= DR_NEWINODEFMT;
445 #endif
446 #else /* __linux __*/
447 if ((diskfd = open(disk, O_RDONLY)) < 0) {
448 msg("Cannot open %s\n", disk);
449 exit(X_STARTUP);
450 }
451 sync();
452 sblock = (struct fs *)sblock_buf;
453 bread(SBOFF, (char *) sblock, SBSIZE);
454 if (sblock->fs_magic != FS_MAGIC)
455 quit("bad sblock magic number\n");
456 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
457 dev_bshift = ffs(dev_bsize) - 1;
458 if (dev_bsize != (1 << dev_bshift))
459 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
460 tp_bshift = ffs(TP_BSIZE) - 1;
461 if (TP_BSIZE != (1 << tp_bshift))
462 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
463 #ifdef FS_44INODEFMT
464 if (sblock->fs_inodefmt >= FS_44INODEFMT)
465 spcl.c_flags |= DR_NEWINODEFMT;
466 #endif
467 maxino = sblock->fs_ipg * sblock->fs_ncg;
468 #endif /* __linux__ */
469 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
470 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
471 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
472 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
473 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
474
475 nonodump = spcl.c_level < honorlevel;
476
477 msg("mapping (Pass I) [regular files]\n");
478 #ifdef __linux__
479 if (directory[0] == 0)
480 anydirskipped = mapfiles(maxino, &tapesize);
481 else
482 anydirskipped = mapfilesfromdir(maxino, &tapesize, directory);
483 #else
484 anydirskipped = mapfiles(maxino, &tapesize);
485 #endif
486
487 msg("mapping (Pass II) [directories]\n");
488 while (anydirskipped) {
489 anydirskipped = mapdirs(maxino, &tapesize);
490 }
491
492 if (pipeout || unlimited) {
493 tapesize += 10; /* 10 trailer blocks */
494 msg("estimated %ld tape blocks.\n", tapesize);
495 } else {
496 double fetapes;
497
498 if (blocksperfile)
499 fetapes = (double) tapesize / blocksperfile;
500 else if (cartridge) {
501 /* Estimate number of tapes, assuming streaming stops at
502 the end of each block written, and not in mid-block.
503 Assume no erroneous blocks; this can be compensated
504 for with an artificially low tape size. */
505 fetapes =
506 ( (double) tapesize /* blocks */
507 * TP_BSIZE /* bytes/block */
508 * (1.0/density) /* 0.1" / byte " */
509 +
510 (double) tapesize /* blocks */
511 * (1.0/ntrec) /* streaming-stops per block */
512 * 15.48 /* 0.1" / streaming-stop " */
513 ) * (1.0 / tsize ); /* tape / 0.1" " */
514 } else {
515 /* Estimate number of tapes, for old fashioned 9-track
516 tape */
517 int tenthsperirg = (density == 625) ? 3 : 7;
518 fetapes =
519 ( (double) tapesize /* blocks */
520 * TP_BSIZE /* bytes / block */
521 * (1.0/density) /* 0.1" / byte " */
522 +
523 (double) tapesize /* blocks */
524 * (1.0/ntrec) /* IRG's / block */
525 * tenthsperirg /* 0.1" / IRG " */
526 ) * (1.0 / tsize ); /* tape / 0.1" " */
527 }
528 etapes = fetapes; /* truncating assignment */
529 etapes++;
530 /* count the dumped inodes map on each additional tape */
531 tapesize += (etapes - 1) *
532 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
533 tapesize += etapes + 10; /* headers + 10 trailer blks */
534 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
535 tapesize, fetapes);
536 }
537
538 /*
539 * Allocate tape buffer.
540 */
541 if (!alloctape())
542 quit(
543 "can't allocate tape buffers - try a smaller blocking factor.\n");
544
545 startnewtape(1);
546 (void)time((time_t *)&(tstart_writing));
547 dumpmap(usedinomap, TS_CLRI, maxino - 1);
548
549 msg("dumping (Pass III) [directories]\n");
550 dirty = 0; /* XXX just to get gcc to shut up */
551 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
552 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
553 dirty = *map++;
554 else
555 dirty >>= 1;
556 if ((dirty & 1) == 0)
557 continue;
558 /*
559 * Skip directory inodes deleted and maybe reallocated
560 */
561 dp = getino(ino);
562 if ((dp->di_mode & IFMT) != IFDIR)
563 continue;
564 #ifdef __linux__
565 (void)dumpdirino(dp, ino);
566 #else
567 (void)dumpino(dp, ino);
568 #endif
569 }
570
571 msg("dumping (Pass IV) [regular files]\n");
572 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
573 int mode;
574
575 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
576 dirty = *map++;
577 else
578 dirty >>= 1;
579 if ((dirty & 1) == 0)
580 continue;
581 /*
582 * Skip inodes deleted and reallocated as directories.
583 */
584 dp = getino(ino);
585 mode = dp->di_mode & IFMT;
586 if (mode == IFDIR)
587 continue;
588 (void)dumpino(dp, ino);
589 }
590
591 (void)time((time_t *)&(tend_writing));
592 spcl.c_type = TS_END;
593 for (i = 0; i < ntrec; i++)
594 writeheader(maxino - 1);
595 if (pipeout)
596 msg("DUMP: %ld tape blocks\n", spcl.c_tapea);
597 else
598 msg("DUMP: %ld tape blocks on %d volumes(s)\n",
599 spcl.c_tapea, spcl.c_volume);
600
601 /* report dump performance, avoid division through zero */
602 if (tend_writing - tstart_writing == 0)
603 msg("finished in less than a second\n");
604 else
605 msg("finished in %d seconds, throughput %d KBytes/sec\n",
606 tend_writing - tstart_writing,
607 spcl.c_tapea / (tend_writing - tstart_writing));
608
609 putdumptime();
610 trewind();
611 broadcast("DUMP IS DONE!\7\7\n");
612 msg("DUMP IS DONE\n");
613 Exit(X_FINOK);
614 /* NOTREACHED */
615 exit(1); /* gcc - shut up */
616 }
617
618 static void
619 usage()
620 {
621 fprintf(stderr,
622 "usage: dump [-0123456789ac"
623 #ifdef KERBEROS
624 "k"
625 #endif
626 "nu] [-B records] [-b blocksize] [-d density] [-f file]\n"
627 " [-h level] [-s feet] [-T date] filesystem\n"
628 " dump [-W | -w]\n");
629 exit(X_STARTUP);
630 }
631
632 /*
633 * Pick up a numeric argument. It must be nonnegative and in the given
634 * range (except that a vmax of 0 means unlimited).
635 */
636 static long
637 numarg(meaning, vmin, vmax)
638 char *meaning;
639 long vmin, vmax;
640 {
641 char *p;
642 long val;
643
644 val = strtol(optarg, &p, 10);
645 if (*p)
646 errx(1, "illegal %s -- %s", meaning, optarg);
647 if (val < vmin || (vmax && val > vmax))
648 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
649 return (val);
650 }
651
652 void
653 sig(signo)
654 int signo;
655 {
656 switch(signo) {
657 case SIGALRM:
658 case SIGBUS:
659 case SIGFPE:
660 case SIGHUP:
661 case SIGTERM:
662 case SIGTRAP:
663 if (pipeout)
664 quit("Signal on pipe: cannot recover\n");
665 msg("Rewriting attempted as response to unknown signal.\n");
666 (void)fflush(stderr);
667 (void)fflush(stdout);
668 close_rewind();
669 exit(X_REWRITE);
670 /* NOTREACHED */
671 case SIGSEGV:
672 msg("SIGSEGV: ABORTING!\n");
673 (void)signal(SIGSEGV, SIG_DFL);
674 (void)kill(0, SIGSEGV);
675 /* NOTREACHED */
676 }
677 }
678
679 char *
680 rawname(cp)
681 char *cp;
682 {
683 #ifdef __linux__
684 return cp;
685 #else /* __linux__ */
686 static char rawbuf[MAXPATHLEN];
687 char *dp = strrchr(cp, '/');
688
689 if (dp == NULL)
690 return (NULL);
691 *dp = '\0';
692 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1);
693 rawbuf[MAXPATHLEN-1] = '\0';
694 *dp = '/';
695 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
696 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
697 return (rawbuf);
698 #endif /* __linux__ */
699 }
700
701 /*
702 * obsolete --
703 * Change set of key letters and ordered arguments into something
704 * getopt(3) will like.
705 */
706 static void
707 obsolete(argcp, argvp)
708 int *argcp;
709 char **argvp[];
710 {
711 int argc, flags;
712 char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL;
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 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
725 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
726 err(1, NULL);
727
728 *nargv++ = *argv;
729 argv += 2;
730
731 for (flags = 0; *ap; ++ap) {
732 switch (*ap) {
733 case 'B':
734 case 'b':
735 case 'd':
736 case 'f':
737 case 'h':
738 case 's':
739 case 'T':
740 if (*argv == NULL) {
741 warnx("option requires an argument -- %c", *ap);
742 usage();
743 }
744 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
745 err(1, NULL);
746 nargv[0][0] = '-';
747 nargv[0][1] = *ap;
748 (void)strcpy(&nargv[0][2], *argv);
749 ++argv;
750 ++nargv;
751 break;
752 default:
753 if (!flags) {
754 *p++ = '-';
755 flags = 1;
756 }
757 *p++ = *ap;
758 break;
759 }
760 }
761
762 /* Terminate flags. */
763 if (flags) {
764 *p = '\0';
765 *nargv++ = flagsp;
766 }
767
768 /* Copy remaining arguments. */
769 while ((*nargv++ = *argv++));
770
771 /* Update argument count. */
772 *argcp = nargv - *argvp - 1;
773 }