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