main.c revision 1.25.6.8 1 /* $NetBSD: main.c,v 1.25.6.8 2002/03/28 22:40:03 he 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.25.6.8 2002/03/28 22:40:03 he 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 notify = 0; /* notify operator flag */
78 int blockswritten = 0; /* number of blocks written on current tape */
79 int tapeno = 0; /* current tape number */
80 int density = 0; /* density in bytes/0.1" */
81 int ntrec = NTREC; /* # tape blocks in each tape record */
82 int cartridge = 0; /* Assume non-cartridge tape */
83 long dev_bsize = 1; /* recalculated below */
84 long blocksperfile = 0; /* output blocks per file */
85 char *host = NULL; /* remote host (if any) */
86 int readcache = -1; /* read cache size (in readblksize blks) */
87 int readblksize = 32 * 1024; /* read block size */
88
89 int main __P((int, char *[]));
90 static long numarg __P((char *, long, long));
91 static void obsolete __P((int *, char **[]));
92 static void usage __P((void));
93
94 int
95 main(argc, argv)
96 int argc;
97 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, 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
114 spcl.c_date = 0;
115 (void)time(&tnow);
116 spcl.c_date = tnow;
117
118 /* Save setgid bit for use later */
119 egid = getegid();
120 setegid(getgid());
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
132 if (argc < 2)
133 usage();
134
135 obsolete(&argc, &argv);
136 while ((ch = getopt(argc, argv,
137 "0123456789aB:b:cd:ef:h:k:l:L:nr:s:ST:uWw")) != -1)
138 switch (ch) {
139 /* dump level */
140 case '0': case '1': case '2': case '3': case '4':
141 case '5': case '6': case '7': case '8': case '9':
142 level = ch;
143 break;
144
145 case 'a': /* `auto-size', Write to EOM. */
146 unlimited = 1;
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 'e': /* eject full tapes */
169 eflag = 1;
170 break;
171
172 case 'f': /* output file */
173 tape = optarg;
174 break;
175
176 case 'h':
177 honorlevel = numarg("honor level", 0L, 10L);
178 break;
179
180 case 'k':
181 readblksize = numarg("read block size", 0, 64) * 1024;
182 break;
183
184 case 'l': /* autoload after eject full tapes */
185 eflag = 1;
186 lflag = numarg("timeout (in seconds)", 1, 0);
187 break;
188
189 case 'L':
190 /*
191 * Note that although there are LBLSIZE characters,
192 * the last must be '\0', so the limit on strlen()
193 * is really LBLSIZE-1.
194 */
195 strncpy(labelstr, optarg, LBLSIZE);
196 labelstr[LBLSIZE-1] = '\0';
197 if (strlen(optarg) > LBLSIZE-1) {
198 msg(
199 "WARNING Label `%s' is larger than limit of %d characters.\n",
200 optarg, LBLSIZE-1);
201 msg("WARNING: Using truncated label `%s'.\n",
202 labelstr);
203 }
204 break;
205 case 'n': /* notify operators */
206 notify = 1;
207 break;
208
209 case 'r': /* read cache size */
210 readcache = numarg("read cache size", 0, 512);
211 break;
212
213 case 's': /* tape size, feet */
214 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
215 break;
216
217 case 'S': /* exit after estimating # of tapes */
218 just_estimate = 1;
219 break;
220
221 case 'T': /* time of last dump */
222 spcl.c_ddate = unctime(optarg);
223 if (spcl.c_ddate < 0) {
224 (void)fprintf(stderr, "bad time \"%s\"\n",
225 optarg);
226 exit(X_STARTUP);
227 }
228 Tflag = 1;
229 lastlevel = '?';
230 break;
231
232 case 'u': /* update /etc/dumpdates */
233 uflag = 1;
234 break;
235
236 case 'W': /* what to do */
237 case 'w':
238 lastdump(ch);
239 exit(X_FINOK); /* do nothing else */
240
241 default:
242 usage();
243 }
244 argc -= optind;
245 argv += optind;
246
247 if (argc < 1) {
248 (void)fprintf(stderr, "Must specify disk or filesystem\n");
249 exit(X_ABORT);
250 }
251
252
253 /*
254 * determine if disk is a subdirectory, and setup appropriately
255 */
256 getfstab(); /* /etc/fstab snarfed */
257 disk = NULL;
258 mountpoint = NULL;
259 dirc = 0;
260 for (i = 0; i < argc; i++) {
261 struct stat sb;
262
263 if (lstat(argv[i], &sb) == -1)
264 quit("Cannot stat %s: %s\n", argv[i], strerror(errno));
265 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
266 disk = argv[i];
267 multicheck:
268 if (dirc != 0)
269 quit(
270 "Can't dump a mountpoint and a filelist\n");
271 break;
272 }
273 if ((dt = fstabsearch(argv[i])) != NULL) {
274 disk = dt->fs_spec;
275 if ((mountpoint = strdup(dt->fs_file)) == NULL) {
276 msg("Cannot malloc mountpoint\n");
277 exit(X_ABORT);
278 }
279 goto multicheck;
280 }
281 if (statfs(argv[i], &fsbuf) == -1)
282 quit("Cannot statfs %s: %s\n", argv[i],
283 strerror(errno));
284 disk = fsbuf.f_mntfromname;
285 if (strcmp(argv[i], fsbuf.f_mntonname) == 0)
286 goto multicheck;
287 if (mountpoint == NULL) {
288 if ((mountpoint = strdup(fsbuf.f_mntonname)) == NULL) {
289 msg("Cannot malloc mountpoint\n");
290 exit(X_ABORT);
291 }
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 (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
372 signal(SIGSEGV, sig);
373 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
374 signal(SIGTERM, sig);
375 if (signal(SIGINT, interrupt) == SIG_IGN)
376 signal(SIGINT, SIG_IGN);
377
378 set_operators(); /* /etc/group snarfed */
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, NAMELEN,
397 "a subset of %s", mountpoint);
398 else
399 (void)strncpy(spcl.c_filesys, mountpoint, NAMELEN);
400 } else {
401 (void)strncpy(spcl.c_filesys, "an unlisted file system",
402 NAMELEN);
403 }
404 (void)strncpy(spcl.c_dev, disk, NAMELEN);
405 (void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1);
406 (void)gethostname(spcl.c_host, NAMELEN);
407 spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
408
409 if ((diskfd = open(disk, O_RDONLY)) < 0) {
410 msg("Cannot open %s\n", disk);
411 exit(X_STARTUP);
412 }
413 sync();
414
415 needswap = fs_read_sblock(sblock_buf);
416
417 spcl.c_level = iswap32(level - '0');
418 spcl.c_type = iswap32(TS_TAPE);
419 spcl.c_date = iswap32(spcl.c_date);
420 spcl.c_ddate = iswap32(spcl.c_ddate);
421 if (!Tflag)
422 getdumptime(); /* /etc/dumpdates snarfed */
423
424 date = iswap32(spcl.c_date);
425 msg("Date of this level %c dump: %s", level,
426 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
427 date = iswap32(spcl.c_ddate);
428 msg("Date of last level %c dump: %s", lastlevel,
429 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
430 msg("Dumping ");
431 if (dirc != 0)
432 msgtail("a subset of ");
433 msgtail("%s (%s) ", disk, spcl.c_filesys);
434 if (host)
435 msgtail("to %s on host %s\n", tape, host);
436 else
437 msgtail("to %s\n", tape);
438 msg("Label: %s\n", labelstr);
439
440 ufsib = fs_parametrize();
441
442 dev_bshift = ffs(dev_bsize) - 1;
443 if (dev_bsize != (1 << dev_bshift))
444 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
445 tp_bshift = ffs(TP_BSIZE) - 1;
446 if (TP_BSIZE != (1 << tp_bshift))
447 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
448 maxino = fs_maxino();
449 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
450 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
451 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
452 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
453 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
454
455 nonodump = iswap32(spcl.c_level) < honorlevel;
456
457 initcache(readcache, readblksize);
458
459 (void)signal(SIGINFO, statussig);
460
461 msg("mapping (Pass I) [regular files]\n");
462 anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
463 (dirc ? argv : NULL));
464
465 msg("mapping (Pass II) [directories]\n");
466 while (anydirskipped) {
467 anydirskipped = mapdirs(maxino, &tapesize);
468 }
469
470 if (pipeout || unlimited) {
471 tapesize += 10; /* 10 trailer blocks */
472 msg("estimated %ld tape blocks.\n", tapesize);
473 } else {
474 double fetapes;
475
476 if (blocksperfile)
477 fetapes = (double) tapesize / blocksperfile;
478 else if (cartridge) {
479 /* Estimate number of tapes, assuming streaming stops at
480 the end of each block written, and not in mid-block.
481 Assume no erroneous blocks; this can be compensated
482 for with an artificially low tape size. */
483 fetapes =
484 ( (double) tapesize /* blocks */
485 * TP_BSIZE /* bytes/block */
486 * (1.0/density) /* 0.1" / byte */
487 +
488 (double) tapesize /* blocks */
489 * (1.0/ntrec) /* streaming-stops per block */
490 * 15.48 /* 0.1" / streaming-stop */
491 ) * (1.0 / tsize ); /* tape / 0.1" */
492 } else {
493 /* Estimate number of tapes, for old fashioned 9-track
494 tape */
495 int tenthsperirg = (density == 625) ? 3 : 7;
496 fetapes =
497 ( tapesize /* blocks */
498 * TP_BSIZE /* bytes / block */
499 * (1.0/density) /* 0.1" / byte */
500 +
501 tapesize /* blocks */
502 * (1.0/ntrec) /* IRG's / block */
503 * tenthsperirg /* 0.1" / IRG */
504 ) * (1.0 / tsize ); /* tape / 0.1" */
505 }
506 etapes = fetapes; /* truncating assignment */
507 etapes++;
508 /* count the dumped inodes map on each additional tape */
509 tapesize += (etapes - 1) *
510 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
511 tapesize += etapes + 10; /* headers + 10 trailer blks */
512 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
513 tapesize, fetapes);
514 }
515 /*
516 * If the user only wants an estimate of the number of
517 * tapes, exit now.
518 */
519 if (just_estimate)
520 exit(X_FINOK);
521
522 /*
523 * Allocate tape buffer.
524 */
525 if (!alloctape())
526 quit("can't allocate tape buffers - try a smaller blocking factor.\n");
527
528 startnewtape(1);
529 (void)time((time_t *)&(tstart_writing));
530 xferrate = 0;
531 dumpmap(usedinomap, TS_CLRI, maxino - 1);
532
533 msg("dumping (Pass III) [directories]\n");
534 dirty = 0; /* XXX just to get gcc to shut up */
535 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
536 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
537 dirty = *map++;
538 else
539 dirty >>= 1;
540 if ((dirty & 1) == 0)
541 continue;
542 /*
543 * Skip directory inodes deleted and maybe reallocated
544 */
545 dp = getino(ino);
546 if ((dp->di_mode & IFMT) != IFDIR)
547 continue;
548 (void)dumpino(dp, ino);
549 }
550
551 msg("dumping (Pass IV) [regular files]\n");
552 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
553 int mode;
554
555 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
556 dirty = *map++;
557 else
558 dirty >>= 1;
559 if ((dirty & 1) == 0)
560 continue;
561 /*
562 * Skip inodes deleted and reallocated as directories.
563 */
564 dp = getino(ino);
565 mode = dp->di_mode & IFMT;
566 if (mode == IFDIR)
567 continue;
568 (void)dumpino(dp, ino);
569 }
570
571 spcl.c_type = iswap32(TS_END);
572 for (i = 0; i < ntrec; i++)
573 writeheader(maxino - 1);
574 if (pipeout)
575 msg("%d tape blocks\n",iswap32(spcl.c_tapea));
576 else
577 msg("%d tape blocks on %d volume%s\n",
578 iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
579 (iswap32(spcl.c_volume) == 1) ? "" : "s");
580 tnow = do_stats();
581 date = iswap32(spcl.c_date);
582 msg("Date of this level %c dump: %s", level,
583 spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
584 msg("Date this dump completed: %s", ctime(&tnow));
585 msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
586 putdumptime();
587 trewind(0);
588 broadcast("DUMP IS DONE!\a\a\n");
589 msg("DUMP IS DONE\n");
590 Exit(X_FINOK);
591 /* NOTREACHED */
592 exit(X_FINOK); /* XXX: to satisfy gcc */
593 }
594
595 static void
596 usage()
597 {
598 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
599 "usage: dump [-0123456789acnu] [-B records] [-b blocksize] [-d density]",
600 " [-f file] [-h level] [-k read block size] [-L label]",
601 " [-l timeout] [-r read cache size] [-s feet] [-T date] filesystem",
602 " dump [-W | -w]");
603 exit(X_STARTUP);
604 }
605
606 /*
607 * Pick up a numeric argument. It must be nonnegative and in the given
608 * range (except that a vmax of 0 means unlimited).
609 */
610 static long
611 numarg(meaning, vmin, vmax)
612 char *meaning;
613 long vmin, vmax;
614 {
615 char *p;
616 long val;
617
618 val = strtol(optarg, &p, 10);
619 if (*p)
620 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
621 if (val < vmin || (vmax && val > vmax))
622 errx(X_STARTUP, "%s must be between %ld and %ld",
623 meaning, vmin, vmax);
624 return (val);
625 }
626
627 void
628 sig(signo)
629 int signo;
630 {
631 switch(signo) {
632 case SIGALRM:
633 case SIGBUS:
634 case SIGFPE:
635 case SIGHUP:
636 case SIGTERM:
637 case SIGTRAP:
638 if (pipeout)
639 quit("Signal on pipe: cannot recover\n");
640 msg("Rewriting attempted as response to unknown signal.\n");
641 (void)fflush(stderr);
642 (void)fflush(stdout);
643 close_rewind();
644 exit(X_REWRITE);
645 /* NOTREACHED */
646 case SIGSEGV:
647 msg("SIGSEGV: ABORTING!\n");
648 (void)signal(SIGSEGV, SIG_DFL);
649 (void)kill(0, SIGSEGV);
650 /* NOTREACHED */
651 }
652 }
653
654 char *
655 rawname(cp)
656 char *cp;
657 {
658 static char rawbuf[MAXPATHLEN];
659 char *dp = strrchr(cp, '/');
660
661 if (dp == NULL)
662 return (NULL);
663 *dp = '\0';
664 (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
665 *dp = '/';
666 return (rawbuf);
667 }
668
669 /*
670 * obsolete --
671 * Change set of key letters and ordered arguments into something
672 * getopt(3) will like.
673 */
674 static void
675 obsolete(argcp, argvp)
676 int *argcp;
677 char **argvp[];
678 {
679 int argc, flags;
680 char *ap, **argv, *flagsp, **nargv, *p;
681
682 /* Setup. */
683 argv = *argvp;
684 argc = *argcp;
685
686 /* Return if no arguments or first argument has leading dash. */
687 ap = argv[1];
688 if (argc == 1 || *ap == '-')
689 return;
690
691 /* Allocate space for new arguments. */
692 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
693 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
694 err(1, "malloc");
695
696 *nargv++ = *argv;
697 argv += 2;
698
699 for (flags = 0; *ap; ++ap) {
700 switch (*ap) {
701 case 'B':
702 case 'b':
703 case 'd':
704 case 'f':
705 case 'h':
706 case 's':
707 case 'T':
708 if (*argv == NULL) {
709 warnx("option requires an argument -- %c", *ap);
710 usage();
711 }
712 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
713 err(1, "malloc");
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