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