]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fdisk_pc98/fdisk.c
Copy device support from head in to 6
[FreeBSD/FreeBSD.git] / sbin / fdisk_pc98 / fdisk.c
1 /*
2  * Mach Operating System
3  * Copyright (c) 1992 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie Mellon
24  * the rights to redistribute these changes.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/disk.h>
31 #include <sys/disklabel.h>
32 #include <sys/diskpc98.h>
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <sys/mount.h>
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <libgeom.h>
41 #include <paths.h>
42 #include <regex.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 int iotest;
50
51 #define LBUF 100
52 static char lbuf[LBUF];
53
54 /*
55  *
56  * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
57  *
58  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
59  *      Copyright (c) 1989      Robert. V. Baron
60  *      Created.
61  */
62
63 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
64 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
65
66 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
67
68 #define MAX_SEC_SIZE 2048       /* maximum section size that is supported */
69 #define MIN_SEC_SIZE 512        /* the sector size to start sensing at */
70 static int secsize = 0;         /* the sensed sector size */
71
72 static char *disk;
73
74 static int cyls, sectors, heads, cylsecs, disksecs;
75
76 struct mboot {
77         unsigned char padding[2]; /* force the longs to be long aligned */
78         unsigned char bootinst[510];
79         unsigned short int      signature;
80         struct  pc98_partition parts[8];
81         unsigned char large_sector_overflow[MAX_SEC_SIZE-MIN_SEC_SIZE];
82 };
83
84 static struct mboot mboot;
85 static int fd;
86
87 #define ACTIVE 0x80
88
89 static uint dos_cyls;
90 static uint dos_heads;
91 static uint dos_sectors;
92 static uint dos_cylsecs;
93
94 #define MAX_ARGS        10
95
96 typedef struct cmd {
97     char                cmd;
98     int                 n_args;
99     struct arg {
100         char    argtype;
101         int     arg_val;
102     }                   args[MAX_ARGS];
103 } CMD;
104
105 static int B_flag  = 0;         /* replace boot code */
106 static int a_flag  = 0;         /* set active partition */
107 static int i_flag  = 0;         /* replace partition data */
108 static int u_flag  = 0;         /* update partition data */
109 static int s_flag  = 0;         /* Print a summary and exit */
110 static int t_flag  = 0;         /* test only */
111 static char *f_flag = NULL;     /* Read config info from file */
112 static int v_flag  = 0;         /* Be verbose */
113
114 static struct part_type
115 {
116         unsigned char type;
117         const char *name;
118 } part_types[] = {
119          {0x00, "unused"}
120         ,{0x01, "Primary DOS with 12 bit FAT"}
121         ,{0x11, "MSDOS"}
122         ,{0x20, "MSDOS"}
123         ,{0x21, "MSDOS"}
124         ,{0x22, "MSDOS"}
125         ,{0x23, "MSDOS"}
126         ,{0x02, "XENIX / file system"}  
127         ,{0x03, "XENIX /usr file system"}  
128         ,{0x04, "PC-UX"}   
129         ,{0x05, "Extended DOS"}   
130         ,{0x06, "Primary 'big' DOS (> 32MB)"}   
131         ,{0x07, "OS/2 HPFS, QNX or Advanced UNIX"}  
132         ,{0x08, "AIX file system"}   
133         ,{0x09, "AIX boot partition or Coherent"}  
134         ,{0x0A, "OS/2 Boot Manager or OPUS"}  
135         ,{0x10, "OPUS"} 
136         ,{0x14, "FreeBSD/NetBSD/386BSD"}  
137         ,{0x94, "FreeBSD/NetBSD/386BSD"}
138         ,{0x40, "VENIX 286"}  
139         ,{0x50, "DM"}   
140         ,{0x51, "DM"}   
141         ,{0x52, "CP/M or Microport SysV/AT"}
142         ,{0x56, "GB"}
143         ,{0x61, "Speed"}
144         ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
145         ,{0x64, "Novell Netware 2.xx"}
146         ,{0x65, "Novell Netware 3.xx"}
147         ,{0x75, "PCIX"}
148         ,{0x40, "Minix"} 
149 };
150
151 static void print_s0(int which);
152 static void print_part(int i);
153 static void init_sector0(unsigned long start);
154 static void init_boot(void);
155 static void change_part(int i);
156 static void print_params(void);
157 static void change_active(int which);
158 static void change_code(void);
159 static void get_params_to_use(void);
160 static char *get_rootdisk(void);
161 static void dos(u_int32_t start, u_int32_t size, struct pc98_partition *partp);
162 static int open_disk(int flag);
163 static ssize_t read_disk(off_t sector, void *buf);
164 static int write_disk(off_t sector, void *buf);
165 static int get_params(void);
166 static int read_s0(void);
167 static int write_s0(void);
168 static int ok(const char *str);
169 static int decimal(const char *str, int *num, int deflt);
170 static const char *get_type(int type);
171 static void usage(void);
172 static int string(const char *str, char **ans);
173
174 int
175 main(int argc, char *argv[])
176 {
177         struct  stat sb;
178         int     c, i;
179         int     partition = -1;
180         struct  pc98_partition *partp;
181
182         while ((c = getopt(argc, argv, "Ba:f:istuv12345678")) != -1)
183                 switch (c) {
184                 case 'B':
185                         B_flag = 1;
186                         break;
187                 case 'a':
188                         a_flag = 1;
189                         break;
190                 case 'f':
191                         f_flag = optarg;
192                         break;
193                 case 'i':
194                         i_flag = 1;
195                         break;
196                 case 's':
197                         s_flag = 1;
198                         break;
199                 case 't':
200                         t_flag = 1;
201                         break;
202                 case 'u':
203                         u_flag = 1;
204                         break;
205                 case 'v':
206                         v_flag = 1;
207                         break;
208                 case '1':
209                 case '2':
210                 case '3':
211                 case '4':
212                 case '5':
213                 case '6':
214                 case '7':
215                 case '8':
216                         partition = c - '0';
217                         break;
218                 default:
219                         usage();
220                 }
221         if (f_flag || i_flag)
222                 u_flag = 1;
223         if (t_flag)
224                 v_flag = 1;
225         argc -= optind;
226         argv += optind;
227
228         if (argc == 0) {
229                 disk = get_rootdisk();
230         } else {
231                 if (stat(argv[0], &sb) == 0) {
232                         /* OK, full pathname given */
233                         disk = argv[0];
234                 } else if (errno == ENOENT && argv[0][0] != '/') {
235                         /* Try prepending "/dev" */
236                         asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
237                         if (disk == NULL)
238                                 errx(1, "out of memory");
239                 } else {
240                         /* other stat error, let it fail below */
241                         disk = argv[0];
242                 }
243         }
244         if (open_disk(u_flag) < 0)
245                 err(1, "cannot open disk %s", disk);
246
247         if (s_flag) {
248                 if (read_s0())
249                         err(1, "read_s0");
250                 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
251                     dos_sectors);
252                 printf("Part  %11s %11s SID\n", "Start", "Size");
253                 for (i = 0; i < NDOSPART; i++) {
254                         partp = ((struct pc98_partition *) &mboot.parts) + i;
255                         if (partp->dp_sid == 0)
256                                 continue;
257                         printf("%4d: %11u %11u 0x%02x\n", i + 1,
258                             partp->dp_scyl * cylsecs,
259                             (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs,
260                                 partp->dp_sid);
261                 }
262                 exit(0);
263         }
264
265         printf("******* Working on device %s *******\n",disk);
266
267         if (f_flag) {
268             if (v_flag)
269                 print_s0(-1);
270             if (!t_flag)
271                 write_s0();
272         } else {
273             if(u_flag)
274                 get_params_to_use();
275             else
276                 print_params();
277
278             if (read_s0())
279                 init_sector0(dos_sectors);
280
281             printf("Media sector size is %d\n", secsize);
282             printf("Warning: BIOS sector numbering starts with sector 1\n");
283             printf("Information from DOS bootblock is:\n");
284             if (partition == -1)
285                 for (i = 1; i <= NDOSPART; i++)
286                     change_part(i);
287             else
288                 change_part(partition);
289
290             if (u_flag || a_flag)
291                 change_active(partition);
292
293             if (B_flag)
294                 change_code();
295
296             if (u_flag || a_flag || B_flag) {
297                 if (!t_flag) {
298                     printf("\nWe haven't changed the partition table yet.  ");
299                     printf("This is your last chance.\n");
300                 }
301                 print_s0(-1);
302                 if (!t_flag) {
303                     if (ok("Should we write new partition table?"))
304                         write_s0();
305                 } else {
306                     printf("\n-t flag specified -- partition table not written.\n");
307                 }
308             }
309         }
310
311         exit(0);
312 }
313
314 static void
315 usage()
316 {
317         fprintf(stderr, "%s%s",
318                 "usage: fdisk [-Baistu] [-12345678] [disk]\n",
319                 "       fdisk -f configfile [-itv] [disk]\n");
320         exit(1);
321 }
322
323 static void
324 print_s0(int which)
325 {
326         int     i;
327
328         print_params();
329         printf("Information from DOS bootblock is:\n");
330         if (which == -1)
331                 for (i = 1; i <= NDOSPART; i++)
332                         printf("%d: ", i), print_part(i);
333         else
334                 print_part(which);
335 }
336
337 static struct pc98_partition mtpart;
338
339 static void
340 print_part(int i)
341 {
342         struct    pc98_partition *partp;
343         u_int64_t part_sz, part_mb;
344
345         partp = ((struct pc98_partition *) &mboot.parts) + i - 1;
346
347         if (!bcmp(partp, &mtpart, sizeof (struct pc98_partition))) {
348                 printf("<UNUSED>\n");
349                 return;
350         }
351         /*
352          * Be careful not to overflow.
353          */
354         part_sz = (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs;
355         part_mb = part_sz * secsize;
356         part_mb /= (1024 * 1024);
357         printf("sysmid %d (%#04x),(%s)\n", partp->dp_mid, partp->dp_mid,
358             get_type(partp->dp_mid));
359         printf("    start %lu, size %lu (%ju Meg), sid %d\n",
360                 (u_long)(partp->dp_scyl * cylsecs), (u_long)part_sz,
361                 (uintmax_t)part_mb, partp->dp_sid);
362         printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
363                 ,partp->dp_scyl
364                 ,partp->dp_shd
365                 ,partp->dp_ssect
366                 ,partp->dp_ecyl
367                 ,partp->dp_ehd
368                 ,partp->dp_esect);
369         printf ("\tsystem Name %.16s\n", partp->dp_name);
370 }
371
372
373 static void
374 init_boot(void)
375 {
376
377         mboot.signature = DOSMAGIC;
378 }
379
380
381 static void
382 init_sector0(unsigned long start)
383 {
384         struct pc98_partition *partp =
385                 (struct pc98_partition *)(&mboot.parts[0]);
386
387         init_boot();
388
389         partp->dp_mid = DOSMID_386BSD;
390         partp->dp_sid = DOSSID_386BSD;
391
392         dos(start, disksecs - start, partp);
393 }
394
395 static void
396 change_part(int i)
397 {
398         struct pc98_partition *partp =
399                 ((struct pc98_partition *) &mboot.parts) + i - 1;
400
401     printf("The data for partition %d is:\n", i);
402     print_part(i);
403
404     if (u_flag && ok("Do you want to change it?")) {
405         int tmp;
406
407         if (i_flag) {
408                 bzero((char *)partp, sizeof (struct pc98_partition));
409                 if (i == 1) {
410                         init_sector0(1);
411                         printf("\nThe static data for the slice 1 has been reinitialized to:\n");
412                         print_part(i);
413                 }
414         }
415
416         do {
417                 int x_start = partp->dp_scyl * cylsecs ;
418                 int x_size  = (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs;
419                 Decimal("sysmid", partp->dp_mid, tmp);
420                 Decimal("syssid", partp->dp_sid, tmp);
421                 String ("system name", partp->dp_name, 16);
422                 Decimal("start", x_start, tmp);
423                 Decimal("size", x_size, tmp);
424
425                 if (ok("Explicitly specify beg/end address ?"))
426                 {
427                         int     tsec,tcyl,thd;
428                         tcyl = partp->dp_scyl;
429                         thd = partp->dp_shd;
430                         tsec = partp->dp_ssect;
431                         Decimal("beginning cylinder", tcyl, tmp);
432                         Decimal("beginning head", thd, tmp);
433                         Decimal("beginning sector", tsec, tmp);
434                         partp->dp_scyl = tcyl;
435                         partp->dp_ssect = tsec;
436                         partp->dp_shd = thd;
437                         partp->dp_ipl_cyl = partp->dp_scyl;
438                         partp->dp_ipl_sct = partp->dp_ssect;
439                         partp->dp_ipl_head = partp->dp_shd;
440
441                         tcyl = partp->dp_ecyl;
442                         thd = partp->dp_ehd;
443                         tsec = partp->dp_esect;
444                         Decimal("ending cylinder", tcyl, tmp);
445                         Decimal("ending head", thd, tmp);
446                         Decimal("ending sector", tsec, tmp);
447                         partp->dp_ecyl = tcyl;
448                         partp->dp_esect = tsec;
449                         partp->dp_ehd = thd;
450                 } else
451                         dos(x_start, x_size, partp);
452
453                 print_part(i);
454         } while (!ok("Are we happy with this entry?"));
455     }
456 }
457
458 static void
459 print_params()
460 {
461         printf("parameters extracted from in-core disklabel are:\n");
462         printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
463                         ,cyls,heads,sectors,cylsecs);
464         if (dos_cyls > 65535 || dos_heads > 255 || dos_sectors > 255)
465                 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
466         printf("parameters to be used for BIOS calculations are:\n");
467         printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
468                 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
469 }
470
471 static void
472 change_active(int which)
473 {
474         struct pc98_partition *partp = &mboot.parts[0];
475         int active, i, new, tmp;
476
477         active = -1;
478         for (i = 0; i < NDOSPART; i++) {
479                 if ((partp[i].dp_sid & ACTIVE) == 0)
480                         continue;
481                 printf("Partition %d is marked active\n", i + 1);
482                 if (active == -1)
483                         active = i + 1;
484         }
485         if (a_flag && which != -1)
486                 active = which;
487         else if (active == -1)
488                 active = 1;
489
490         if (!ok("Do you want to change the active partition?"))
491                 return;
492 setactive:
493         do {
494                 new = active;
495                 Decimal("active partition", new, tmp);
496                 if (new < 1 || new > 8) {
497                         printf("Active partition number must be in range 1-8."
498                                         "  Try again.\n");
499                         goto setactive;
500                 }
501                 active = new;
502         } while (!ok("Are you happy with this choice"));
503         if (active > 0 && active <= 8)
504                 partp[active-1].dp_sid |= ACTIVE;
505 }
506
507 static void
508 change_code()
509 {
510         if (ok("Do you want to change the boot code?"))
511                 init_boot();
512 }
513
514 void
515 get_params_to_use()
516 {
517         int     tmp;
518         print_params();
519         if (ok("Do you want to change our idea of what BIOS thinks ?"))
520         {
521                 do
522                 {
523                         Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
524                         Decimal("BIOS's idea of #heads", dos_heads, tmp);
525                         Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
526                         dos_cylsecs = dos_heads * dos_sectors;
527                         print_params();
528                 }
529                 while(!ok("Are you happy with this choice"));
530         }
531 }
532
533
534 /***********************************************\
535 * Change real numbers into strange dos numbers  *
536 \***********************************************/
537 static void
538 dos(u_int32_t start, u_int32_t size, struct pc98_partition *partp)
539 {
540         u_int32_t end;
541
542         if (partp->dp_mid == 0 && partp->dp_sid == 0 &&
543             start == 0 && size == 0) {
544                 memcpy(partp, &mtpart, sizeof(*partp));
545                 return;
546         }
547
548         /* Start c/h/s. */
549         partp->dp_scyl = partp->dp_ipl_cyl = start / dos_cylsecs;
550         partp->dp_shd = partp->dp_ipl_head = start % dos_cylsecs / dos_sectors;
551         partp->dp_ssect = partp->dp_ipl_sct = start % dos_sectors;
552
553         /* End c/h/s. */
554         end = start + size - cylsecs;
555         partp->dp_ecyl = end / dos_cylsecs;
556         partp->dp_ehd = end % dos_cylsecs / dos_sectors;
557         partp->dp_esect = end % dos_sectors;
558 }
559
560 static int
561 open_disk(int flag)
562 {
563         struct stat     st;
564         int rwmode;
565
566         if (stat(disk, &st) == -1) {
567                 if (errno == ENOENT)
568                         return -2;
569                 warnx("can't get file status of %s", disk);
570                 return -1;
571         }
572         if ( !(st.st_mode & S_IFCHR) )
573                 warnx("device %s is not character special", disk);
574         rwmode = a_flag || B_flag || flag ? O_RDWR : O_RDONLY;
575         fd = open(disk, rwmode);
576         if (fd == -1 && errno == EPERM && rwmode == O_RDWR)
577                 fd = open(disk, O_RDONLY);
578         if (fd == -1 && errno == ENXIO)
579                 return -2;
580         if (fd == -1) {
581                 warnx("can't open device %s", disk);
582                 return -1;
583         }
584         if (get_params() == -1) {
585                 warnx("can't get disk parameters on %s", disk);
586                 return -1;
587         }
588         return fd;
589 }
590
591 static ssize_t
592 read_disk(off_t sector, void *buf)
593 {
594
595         lseek(fd, (sector * 512), 0);
596         return read(fd, buf,
597                     secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
598 }
599
600 static int
601 write_disk(off_t sector, void *buf)
602 {
603         int error;
604         struct gctl_req *grq;
605         const char *q;
606         char fbuf[BUFSIZ];
607         int i, fdw;
608
609         grq = gctl_get_handle();
610         gctl_ro_param(grq, "verb", -1, "write PC98");
611         gctl_ro_param(grq, "class", -1, "PC98");
612         q = strrchr(disk, '/');
613         if (q == NULL)
614                 q = disk;
615         else
616                 q++;
617         gctl_ro_param(grq, "geom", -1, q);
618         gctl_ro_param(grq, "data", secsize, buf);
619         q = gctl_issue(grq);
620         if (q == NULL) {
621                 gctl_free(grq);
622                 return(0);
623         }
624         warnx("%s", q);
625         gctl_free(grq);
626
627         error = pwrite(fd, buf, secsize, (sector * 512));
628         if (error == secsize)
629                 return (0);
630
631         for (i = 0; i < NDOSPART; i++) {
632                 sprintf(fbuf, "%ss%d", disk, i + 1);
633                 fdw = open(fbuf, O_RDWR, 0);
634                 if (fdw < 0)
635                         continue;
636                 error = ioctl(fdw, DIOCSPC98, buf);
637                 close(fdw);
638                 if (error == 0)
639                         return (0);
640         }
641         warnx("Failed to write sector zero");
642         return(EINVAL);
643 }
644
645 static int
646 get_params()
647 {
648         int error;
649         u_int u;
650         off_t o;
651
652         error = ioctl(fd, DIOCGFWSECTORS, &u);
653         if (error == 0)
654                 sectors = dos_sectors = u;
655         else
656                 sectors = dos_sectors = 17;
657
658         error = ioctl(fd, DIOCGFWHEADS, &u);
659         if (error == 0)
660                 heads = dos_heads = u;
661         else
662                 heads = dos_heads = 8;
663
664         dos_cylsecs = cylsecs = heads * sectors;
665         disksecs = cyls * heads * sectors;
666
667         error = ioctl(fd, DIOCGSECTORSIZE, &u);
668         if (error != 0 || u == 0)
669                 u = 512;
670         secsize = u;
671
672         error = ioctl(fd, DIOCGMEDIASIZE, &o);
673         if (error == 0) {
674                 disksecs = o / u;
675                 cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
676         }
677
678         return (disksecs);
679 }
680 \f
681
682 static int
683 read_s0()
684 {
685
686         if (read_disk(0, (char *) mboot.bootinst) == -1) {
687                 warnx("can't read fdisk partition table");
688                 return -1;
689         }
690         if (mboot.signature != DOSMAGIC) {
691                 warnx("invalid fdisk partition table found");
692                 /* So should we initialize things */
693                 return -1;
694         }
695
696         return 0;
697 }
698
699 static int
700 write_s0()
701 {
702
703         if (iotest) {
704                 print_s0(-1);
705                 return 0;
706         }
707
708         /*
709          * write enable label sector before write (if necessary),
710          * disable after writing.
711          * needed if the disklabel protected area also protects
712          * sector 0. (e.g. empty disk)
713          */
714         if (write_disk(0, (char *) mboot.bootinst) == -1) {
715                 warn("can't write fdisk partition table");
716                 return -1;
717         }
718
719         return(0);
720 }
721
722
723 static int
724 ok(const char *str)
725 {
726         printf("%s [n] ", str);
727         fflush(stdout);
728         if (fgets(lbuf, LBUF, stdin) == NULL)
729                 exit(1);
730         lbuf[strlen(lbuf)-1] = 0;
731
732         if (*lbuf &&
733                 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
734                  !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
735                 return 1;
736         else
737                 return 0;
738 }
739
740 static int
741 decimal(const char *str, int *num, int deflt)
742 {
743         int acc = 0, c;
744         char *cp;
745
746         while (1) {
747                 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
748                 fflush(stdout);
749                 if (fgets(lbuf, LBUF, stdin) == NULL)
750                         exit(1);
751                 lbuf[strlen(lbuf)-1] = 0;
752
753                 if (!*lbuf)
754                         return 0;
755
756                 cp = lbuf;
757                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
758                 if (!c)
759                         return 0;
760                 while ((c = *cp++)) {
761                         if (c <= '9' && c >= '0')
762                                 acc = acc * 10 + c - '0';
763                         else
764                                 break;
765                 }
766                 if (c == ' ' || c == '\t')
767                         while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
768                 if (!c) {
769                         *num = acc;
770                         return 1;
771                 } else
772                         printf("%s is an invalid decimal number.  Try again.\n",
773                                 lbuf);
774         }
775
776 }
777
778 static int
779 string(const char *str, char **ans)
780 {
781         int i, c;
782         char *cp = lbuf;
783
784         while (1) {
785                 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
786                 fgets(lbuf, LBUF, stdin);
787                 lbuf[strlen(lbuf)-1] = 0;
788
789                 if (!*lbuf)
790                         return 0;
791
792                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
793                 if (c == '"') {
794                         c = *++cp;
795                         *ans = cp;
796                         while ((c = *cp) && c != '"') cp++;
797                 } else {
798                         *ans = cp;
799                         while ((c = *cp) && c != ' ' && c != '\t') cp++;
800                 }
801
802                 for (i = strlen(*ans); i < 16; i++)
803                         (*ans)[i] = ' ';
804                 (*ans)[16] = 0;
805
806                 return 1;
807         }
808 }
809
810 static const char *
811 get_type(int type)
812 {
813         int     numentries = (sizeof(part_types)/sizeof(struct part_type));
814         int     counter = 0;
815         struct  part_type *ptr = part_types;
816
817
818         while(counter < numentries) {
819                 if(ptr->type == (type & 0x7f))
820                         return(ptr->name);
821                 ptr++;
822                 counter++;
823         }
824         return("unknown");
825 }
826
827 /*
828  * Try figuring out the root device's canonical disk name.
829  * The following choices are considered:
830  *   /dev/ad0s1a     => /dev/ad0
831  *   /dev/da0a       => /dev/da0
832  *   /dev/vinum/root => /dev/vinum/root
833  */
834 static char *
835 get_rootdisk(void)
836 {
837         struct statfs rootfs;
838         regex_t re;
839 #define NMATCHES 2
840         regmatch_t rm[NMATCHES];
841         char *s;
842         int rv;
843
844         if (statfs("/", &rootfs) == -1)
845                 err(1, "statfs(\"/\")");
846
847         if ((rv = regcomp(&re, "^(/dev/[a-z]+[0-9]+)([sp][0-9]+)?[a-h]?$",
848                     REG_EXTENDED)) != 0)
849                 errx(1, "regcomp() failed (%d)", rv);
850         if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
851                 errx(1,
852 "mounted root fs resource doesn't match expectations (regexec returned %d)",
853                     rv);
854         if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
855                 errx(1, "out of memory");
856         memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
857             rm[1].rm_eo - rm[1].rm_so);
858         s[rm[1].rm_eo - rm[1].rm_so] = 0;
859
860         return s;
861 }