main.c revision 1.48 1 /* $NetBSD: main.c,v 1.48 2001/12/25 12:06:26 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.48 2001/12/25 12:06:26 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 gid_t egid; /* Retain tty privs for notification */
77 int timestamp; /* print message timestamps */
78 int notify; /* notify operator flag */
79 int blockswritten; /* number of blocks written on current tape */
80 int tapeno; /* current tape number */
81 int density; /* density in bytes/0.1" */
82 int ntrec = NTREC; /* # tape blocks in each tape record */
83 int cartridge; /* Assume non-cartridge tape */
84 long dev_bsize = 1; /* recalculated below */
85 long blocksperfile; /* output blocks per file */
86 char *host; /* remote host (if any) */
87 int readcache = -1; /* read cache size (in readblksize blks) */
88 int readblksize = 32 * 1024; /* read block size */
89 char default_time_string[] = "%T %Z"; /* default timestamp string */
90 char *time_string = default_time_string; /* timestamp string */
91
92 int main(int, char *[]);
93 static long numarg(char *, long, long);
94 static void obsolete(int *, char **[]);
95 static void usage(void);
96
97 int
98 main(int argc, char *argv[])
99 {
100 ino_t ino;
101 int dirty;
102 struct dinode *dp;
103 struct fstab *dt;
104 struct statfs *mntinfo, fsbuf;
105 char *map;
106 int ch;
107 int i, anydirskipped, bflag = 0, Tflag = 0, Fflag = 0, honorlevel = 1;
108 ino_t maxino;
109 time_t tnow, date;
110 int dirc;
111 char *mountpoint;
112 int just_estimate = 0;
113 char labelstr[LBLSIZE];
114 char *new_time_format;
115
116 spcl.c_date = 0;
117 (void)time(&tnow);
118 spcl.c_date = tnow;
119 tzset(); /* set up timezone for strftime */
120 if ((new_time_format = getenv("TIMEFORMAT")) != NULL)
121 time_string = new_time_format;
122
123 /* Save setgid bit for use later */
124 egid = getegid();
125 setegid(getgid());
126
127 tsize = 0; /* Default later, based on 'c' option for cart tapes */
128 if ((tape = getenv("TAPE")) == NULL)
129 tape = _PATH_DEFTAPE;
130 dumpdates = _PATH_DUMPDATES;
131 temp = _PATH_DTMP;
132 strcpy(labelstr, "none"); /* XXX safe strcpy. */
133 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
134 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
135 level = '0';
136 timestamp = 0;
137
138 if (argc < 2)
139 usage();
140
141 obsolete(&argc, &argv);
142 while ((ch = getopt(argc, argv,
143 "0123456789B:b:cd:eFf:h:k:lL:nr:s:StT:uWw")) != -1)
144 switch (ch) {
145 /* dump level */
146 case '0': case '1': case '2': case '3': case '4':
147 case '5': case '6': case '7': case '8': case '9':
148 level = ch;
149 break;
150
151 case 'B': /* blocks per output file */
152 blocksperfile = numarg("blocks per file", 1L, 0L);
153 break;
154
155 case 'b': /* blocks per tape write */
156 ntrec = numarg("blocks per write", 1L, 1000L);
157 bflag = 1;
158 break;
159
160 case 'c': /* Tape is cart. not 9-track */
161 cartridge = 1;
162 break;
163
164 case 'd': /* density, in bits per inch */
165 density = numarg("density", 10L, 327670L) / 10;
166 if (density >= 625 && !bflag)
167 ntrec = HIGHDENSITYTREC;
168 break;
169
170 case 'e': /* eject full tapes */
171 eflag = 1;
172 break;
173
174 case 'F': /* files-to-dump is an fs image */
175 Fflag = 1;
176 break;
177
178 case 'f': /* output file */
179 tape = optarg;
180 break;
181
182 case 'h':
183 honorlevel = numarg("honor level", 0L, 10L);
184 break;
185
186 case 'k':
187 readblksize = numarg("read block size", 0, 64) * 1024;
188 break;
189
190 case 'l': /* autoload after eject full tapes */
191 eflag = 1;
192 lflag = 1;
193 break;
194
195 case 'L':
196 /*
197 * Note that although there are LBLSIZE characters,
198 * the last must be '\0', so the limit on strlen()
199 * is really LBLSIZE-1.
200 */
201 strncpy(labelstr, optarg, LBLSIZE);
202 labelstr[LBLSIZE-1] = '\0';
203 if (strlen(optarg) > LBLSIZE-1) {
204 msg(
205 "WARNING Label `%s' is larger than limit of %d characters.\n",
206 optarg, LBLSIZE-1);
207 msg("WARNING: Using truncated label `%s'.\n",
208 labelstr);
209 }
210 break;
211 case 'n': /* notify operators */
212 notify = 1;
213 break;
214
215 case 'r': /* read cache size */
216 readcache = numarg("read cache size", 0, 512);
217 break;
218
219 case 's': /* tape size, feet */
220 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
221 break;
222
223 case 'S': /* exit after estimating # of tapes */
224 just_estimate = 1;
225 break;
226
227 case 't':
228 timestamp = 1;
229 break;
230
231 case 'T': /* time of last dump */
232 spcl.c_ddate = unctime(optarg);
233 if (spcl.c_ddate < 0) {
234 (void)fprintf(stderr, "bad time \"%s\"\n",
235 optarg);
236 exit(X_STARTUP);
237 }
238 Tflag = 1;
239 lastlevel = '?';
240 break;
241
242 case 'u': /* update /etc/dumpdates */
243 uflag = 1;
244 break;
245
246 case 'W': /* what to do */
247 case 'w':
248 lastdump(ch);
249 exit(X_FINOK); /* do nothing else */
250
251 default:
252 usage();
253 }
254 argc -= optind;
255 argv += optind;
256
257 if (argc < 1) {
258 (void)fprintf(stderr,
259 "Must specify disk or image, or file list\n");
260 exit(X_STARTUP);
261 }
262
263
264 /*
265 * determine if disk is a subdirectory, and setup appropriately
266 */
267 getfstab(); /* /etc/fstab snarfed */
268 disk = NULL;
269 mountpoint = NULL;
270 dirc = 0;
271 for (i = 0; i < argc; i++) {
272 struct stat sb;
273
274 if (lstat(argv[i], &sb) == -1)
275 quit("Cannot stat %s: %s\n", argv[i], strerror(errno));
276 if (Fflag || S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
277 disk = argv[i];
278 multicheck:
279 if (dirc != 0)
280 quit(
281 "Can't dump a disk or image at the same time as a file list\n");
282 break;
283 }
284 if ((dt = fstabsearch(argv[i])) != NULL) {
285 disk = dt->fs_spec;
286 mountpoint = xstrdup(dt->fs_file);
287 goto multicheck;
288 }
289 if (statfs(argv[i], &fsbuf) == -1)
290 quit("Cannot statfs %s: %s\n", argv[i],
291 strerror(errno));
292 disk = fsbuf.f_mntfromname;
293 if (strcmp(argv[i], fsbuf.f_mntonname) == 0)
294 goto multicheck;
295 if (mountpoint == NULL) {
296 mountpoint = xstrdup(fsbuf.f_mntonname);
297 if (uflag) {
298 msg("Ignoring u flag for subdir dump\n");
299 uflag = 0;
300 }
301 if (level > '0') {
302 msg("Subdir dump is done at level 0\n");
303 level = '0';
304 }
305 msg("Dumping sub files/directories from %s\n",
306 mountpoint);
307 } else {
308 if (strcmp(mountpoint, fsbuf.f_mntonname) != 0)
309 quit("%s is not on %s\n", argv[i], mountpoint);
310 }
311 msg("Dumping file/directory %s\n", argv[i]);
312 dirc++;
313 }
314 if (mountpoint)
315 free(mountpoint);
316
317 if (dirc == 0) {
318 argv++;
319 if (argc != 1) {
320 (void)fprintf(stderr, "Excess arguments to dump:");
321 while (--argc)
322 (void)fprintf(stderr, " %s", *argv++);
323 (void)fprintf(stderr, "\n");
324 exit(X_STARTUP);
325 }
326 }
327 if (Tflag && uflag) {
328 (void)fprintf(stderr,
329 "You cannot use the T and u flags together.\n");
330 exit(X_STARTUP);
331 }
332 if (strcmp(tape, "-") == 0) {
333 pipeout++;
334 tape = "standard output";
335 }
336
337 if (blocksperfile)
338 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
339 else {
340 /*
341 * Determine how to default tape size and density
342 *
343 * density tape size
344 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
345 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
346 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
347 * (450*4 - slop)
348 */
349 if (density == 0)
350 density = cartridge ? 100 : 160;
351 if (tsize == 0)
352 tsize = cartridge ? 1700L*120L : 2300L*120L;
353 }
354
355 if (strchr(tape, ':')) {
356 host = tape;
357 tape = strchr(host, ':');
358 *tape++ = '\0';
359 #ifdef RDUMP
360 if (rmthost(host) == 0)
361 exit(X_STARTUP);
362 #else
363 (void)fprintf(stderr, "remote dump not enabled\n");
364 exit(X_STARTUP);
365 #endif
366 }
367
368 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
369 signal(SIGHUP, sig);
370 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
371 signal(SIGTRAP, sig);
372 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
373 signal(SIGFPE, sig);
374 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
375 signal(SIGBUS, sig);
376 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
377 signal(SIGSEGV, sig);
378 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
379 signal(SIGTERM, sig);
380 if (signal(SIGINT, interrupt) == SIG_IGN)
381 signal(SIGINT, SIG_IGN);
382
383 set_operators(); /* /etc/group snarfed */
384
385 /*
386 * disk can be either the full special file name, or
387 * the file system name.
388 */
389 mountpoint = NULL;
390 if ((dt = fstabsearch(disk)) != NULL) {
391 disk = rawname(dt->fs_spec);
392 mountpoint = dt->fs_file;
393 msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB);
394 } else if ((mntinfo = mntinfosearch(disk)) != NULL) {
395 disk = rawname(mntinfo->f_mntfromname);
396 mountpoint = mntinfo->f_mntonname;
397 msg("Found %s on %s in mount table\n", disk, mountpoint);
398 }
399 if (mountpoint != NULL) {
400 if (dirc != 0)
401 (void)snprintf(spcl.c_filesys, NAMELEN,
402 "a subset of %s", mountpoint);
403 else
404 (void)strncpy(spcl.c_filesys, mountpoint, NAMELEN);
405 } else if (Fflag) {
406 (void)strncpy(spcl.c_filesys, "a file system image", NAMELEN);
407 } else {
408 (void)strncpy(spcl.c_filesys, "an unlisted file system",
409 NAMELEN);
410 }
411 (void)strncpy(spcl.c_dev, disk, NAMELEN);
412 (void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1);
413 (void)gethostname(spcl.c_host, NAMELEN);
414 spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
415
416 if ((diskfd = open(disk, O_RDONLY)) < 0) {
417 msg("Cannot open %s\n", disk);
418 exit(X_STARTUP);
419 }
420 sync();
421
422 needswap = fs_read_sblock(sblock_buf);
423
424 spcl.c_level = iswap32(level - '0');
425 spcl.c_type = iswap32(TS_TAPE);
426 spcl.c_date = iswap32(spcl.c_date);
427 spcl.c_ddate = iswap32(spcl.c_ddate);
428 if (!Tflag)
429 getdumptime(); /* /etc/dumpdates snarfed */
430
431 date = iswap32(spcl.c_date);
432 msg("Date of this level %c dump: %s", level,
433 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
434 date = iswap32(spcl.c_ddate);
435 msg("Date of last level %c dump: %s", lastlevel,
436 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
437 msg("Dumping ");
438 if (dirc != 0)
439 msgtail("a subset of ");
440 msgtail("%s (%s) ", disk, spcl.c_filesys);
441 if (host)
442 msgtail("to %s on host %s\n", tape, host);
443 else
444 msgtail("to %s\n", tape);
445 msg("Label: %s\n", labelstr);
446
447 ufsib = fs_parametrize();
448
449 dev_bshift = ffs(dev_bsize) - 1;
450 if (dev_bsize != (1 << dev_bshift))
451 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
452 tp_bshift = ffs(TP_BSIZE) - 1;
453 if (TP_BSIZE != (1 << tp_bshift))
454 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
455 maxino = fs_maxino();
456 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
457 usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
458 dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
459 dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
460 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
461
462 nonodump = iswap32(spcl.c_level) < honorlevel;
463
464 initcache(readcache, readblksize);
465
466 (void)signal(SIGINFO, statussig);
467
468 msg("mapping (Pass I) [regular files]\n");
469 anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
470 (dirc ? argv : NULL));
471
472 msg("mapping (Pass II) [directories]\n");
473 while (anydirskipped) {
474 anydirskipped = mapdirs(maxino, &tapesize);
475 }
476
477 if (pipeout) {
478 tapesize += 10; /* 10 trailer blocks */
479 msg("estimated %ld tape blocks.\n", tapesize);
480 } else {
481 double fetapes;
482
483 if (blocksperfile)
484 fetapes = (double) tapesize / blocksperfile;
485 else if (cartridge) {
486 /* Estimate number of tapes, assuming streaming stops at
487 the end of each block written, and not in mid-block.
488 Assume no erroneous blocks; this can be compensated
489 for with an artificially low tape size. */
490 fetapes =
491 ( (double) tapesize /* blocks */
492 * TP_BSIZE /* bytes/block */
493 * (1.0/density) /* 0.1" / byte */
494 +
495 (double) tapesize /* blocks */
496 * (1.0/ntrec) /* streaming-stops per block */
497 * 15.48 /* 0.1" / streaming-stop */
498 ) * (1.0 / tsize ); /* tape / 0.1" */
499 } else {
500 /* Estimate number of tapes, for old fashioned 9-track
501 tape */
502 int tenthsperirg = (density == 625) ? 3 : 7;
503 fetapes =
504 ( tapesize /* blocks */
505 * TP_BSIZE /* bytes / block */
506 * (1.0/density) /* 0.1" / byte */
507 +
508 tapesize /* blocks */
509 * (1.0/ntrec) /* IRG's / block */
510 * tenthsperirg /* 0.1" / IRG */
511 ) * (1.0 / tsize ); /* tape / 0.1" */
512 }
513 etapes = fetapes; /* truncating assignment */
514 etapes++;
515 /* count the dumped inodes map on each additional tape */
516 tapesize += (etapes - 1) *
517 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
518 tapesize += etapes + 10; /* headers + 10 trailer blks */
519 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
520 tapesize, fetapes);
521 }
522 /*
523 * If the user only wants an estimate of the number of
524 * tapes, exit now.
525 */
526 if (just_estimate)
527 exit(X_FINOK);
528
529 /*
530 * Allocate tape buffer.
531 */
532 if (!alloctape())
533 quit("can't allocate tape buffers - try a smaller blocking factor.\n");
534
535 startnewtape(1);
536 (void)time((time_t *)&(tstart_writing));
537 xferrate = 0;
538 dumpmap(usedinomap, TS_CLRI, maxino - 1);
539
540 msg("dumping (Pass III) [directories]\n");
541 dirty = 0; /* XXX just to get gcc to shut up */
542 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
543 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
544 dirty = *map++;
545 else
546 dirty >>= 1;
547 if ((dirty & 1) == 0)
548 continue;
549 /*
550 * Skip directory inodes deleted and maybe reallocated
551 */
552 dp = getino(ino);
553 if ((dp->di_mode & IFMT) != IFDIR)
554 continue;
555 (void)dumpino(dp, ino);
556 }
557
558 msg("dumping (Pass IV) [regular files]\n");
559 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
560 int mode;
561
562 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
563 dirty = *map++;
564 else
565 dirty >>= 1;
566 if ((dirty & 1) == 0)
567 continue;
568 /*
569 * Skip inodes deleted and reallocated as directories.
570 */
571 dp = getino(ino);
572 mode = dp->di_mode & IFMT;
573 if (mode == IFDIR)
574 continue;
575 (void)dumpino(dp, ino);
576 }
577
578 spcl.c_type = iswap32(TS_END);
579 for (i = 0; i < ntrec; i++)
580 writeheader(maxino - 1);
581 if (pipeout)
582 msg("%d tape blocks\n",iswap32(spcl.c_tapea));
583 else
584 msg("%d tape blocks on %d volume%s\n",
585 iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
586 (iswap32(spcl.c_volume) == 1) ? "" : "s");
587 tnow = do_stats();
588 date = iswap32(spcl.c_date);
589 msg("Date of this level %c dump: %s", level,
590 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
591 msg("Date this dump completed: %s", ctime(&tnow));
592 msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
593 putdumptime();
594 trewind(0);
595 broadcast("DUMP IS DONE!\a\a\n");
596 msg("DUMP IS DONE\n");
597 Exit(X_FINOK);
598 /* NOTREACHED */
599 exit(X_FINOK); /* XXX: to satisfy gcc */
600 }
601
602 static void
603 usage(void)
604 {
605
606 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
607 "usage: dump [-0123456789ceFlnStu] [-B records] [-b blocksize] [-d density]",
608 " [-f file] [-h level] [-k read block size] [-L label]",
609 " [-r read cache size] [-s feet] [-T date] file system",
610 " dump [-W | -w]");
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