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