* convert to using getopt instead of parsing argv[] by hand, solving

part of [bin/1500] - from Peter Svensson <petersv@df.lth.se> - as
  a side effect.
* ensure ty: is set when -d is used (from [bin/1500])
* only ask for bad144 support for SMD disks (from [bin/1500])
* add prototypes, and ensure structures are initialised correctly
* fix up prior conversion of gets() to fgets(); the former removes
  trailing \n, the latter leaves it.
* check return value of fgets() so that EOF won't infinatly loop
* fix a couple of man page typos
This commit is contained in:
lukem 1997-06-21 09:07:07 +00:00
parent 76b5bb5198
commit 1375baa210
2 changed files with 122 additions and 85 deletions

View File

@ -30,7 +30,7 @@
.\" SUCH DAMAGE.
.\"
.\" from: @(#)diskpart.8 8.1 (Berkeley) 6/6/93
.\" $NetBSD: diskpart.8,v 1.4 1997/03/08 14:28:52 mouse Exp $
.\" $NetBSD: diskpart.8,v 1.5 1997/06/21 09:07:07 lukem Exp $
.\"
.Dd June 6, 1993
.Dt DISKPART 8
@ -39,27 +39,27 @@
.Nm diskpart
.Nd calculate default disk partition sizes
.Sh SYNOPSIS
.Nm diskpart
.Op Fl p
.Nm
.Op Fl d
.Op Fl p
.Op Fl s Ar size
.Ar disk-type
.Sh DESCRIPTION
.Nm Diskpart
.Nm
is used to calculate the disk partition sizes based on the
default rules used at Berkeley.
.Pp
Available options and operands:
.Bl -tag -width Fl
.It Fl p
Tables suitable for inclusion in a device driver
are produced.
.It Fl d
An entry suitable for inclusion in the disk
description file
.Pa /etc/disktab
is generated; for example,
.Xr disktab 5 .
.It Fl p
Tables suitable for inclusion in a device driver
are produced.
.It Fl s Ar size
The size of the disk may be limited to
.Ar size

View File

@ -39,7 +39,7 @@ static char copyright[] =
#ifndef lint
/* static char sccsid[] = "from: @(#)diskpart.c 8.3 (Berkeley) 11/30/94"; */
static char rcsid[] = "$NetBSD: diskpart.c,v 1.7 1997/03/08 07:24:42 mikel Exp $";
static char rcsid[] = "$NetBSD: diskpart.c,v 1.8 1997/06/21 09:07:09 lukem Exp $";
#endif /* not lint */
/*
@ -49,9 +49,12 @@ static char rcsid[] = "$NetBSD: diskpart.c,v 1.7 1997/03/08 07:24:42 mikel Exp $
#define DKTYPENAMES
#include <sys/disklabel.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define for_now /* show all of `c' partition for disklabel */
#define NPARTITIONS 8
@ -87,14 +90,14 @@ char layouts[NLAYOUTS][NPARTITIONS] = {
* (e.g. swap areas or for access to the entire device).
*/
struct partition defparam[NPARTITIONS] = {
{ 0, 0, 1024, FS_UNUSED, 8, 0 }, /* a */
{ 0, 0, 1024, FS_SWAP, 8, 0 }, /* b */
{ 0, 0, 1024, FS_UNUSED, 8, 0 }, /* c */
{ 0, 0, 512, FS_UNUSED, 8, 0 }, /* d */
{ 0, 0, 1024, FS_UNUSED, 8, 0 }, /* e */
{ 0, 0, 1024, FS_UNUSED, 8, 0 }, /* f */
{ 0, 0, 1024, FS_UNUSED, 8, 0 }, /* g */
{ 0, 0, 1024, FS_UNUSED, 8, 0 } /* h */
{ 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* a */
{ 0, 0, 1024, FS_SWAP, 8, { 0 }, }, /* b */
{ 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* c */
{ 0, 0, 512, FS_UNUSED, 8, { 0 }, }, /* d */
{ 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* e */
{ 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* f */
{ 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* g */
{ 0, 0, 1024, FS_UNUSED, 8, { 0 }, } /* h */
};
/*
@ -110,52 +113,67 @@ int badsecttable = 126; /* # sectors */
int pflag; /* print device driver partition tables */
int dflag; /* print disktab entry */
struct disklabel *promptfordisk();
struct disklabel *promptfordisk __P((const char *));
void usage __P((void));
int gettype __P((const char *, char **));
int
main(argc, argv)
int argc;
char *argv[];
{
struct disklabel *dp;
register int curcyl, spc, def, part, layout, j;
int curcyl, spc, def, part, layout, j, ch;
int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
int totsize = 0;
off_t totsize = 0;
char *lp, *tyname;
argc--, argv++;
if (argc < 1) {
fprintf(stderr,
"usage: diskpart [ -p ] [ -d ] [ -s size ] disk-type\n");
exit(1);
while ((ch = getopt(argc, argv, "pds:")) != -1) {
switch (ch) {
case 'd':
dflag++;
break;
case 'p':
pflag++;
break;
case 's':
totsize = strtoul(optarg, &lp, 10);
if (*lp != '\0')
usage();
break;
case '?':
default:
usage();
/* NOTREACHED */
}
}
if (argc > 0 && strcmp(*argv, "-p") == 0) {
pflag++;
argc--, argv++;
}
if (argc > 0 && strcmp(*argv, "-d") == 0) {
dflag++;
argc--, argv++;
}
if (argc > 1 && strcmp(*argv, "-s") == 0) {
totsize = atoi(argv[1]);
argc += 2, argv += 2;
argc -= optind;
argv += optind;
if (argc != 1) {
usage();
/* NOTREACHED */
}
dp = getdiskbyname(*argv);
if (dp == NULL) {
if (isatty(0))
dp = promptfordisk(*argv);
if (dp == NULL) {
fprintf(stderr, "%s: unknown disk type\n", *argv);
exit(2);
exit(1);
}
} else {
if (dp->d_flags & D_REMOVABLE)
tyname = "removable";
else if (dp->d_flags & D_RAMDISK)
tyname = "simulated";
else
tyname = "winchester";
}
if (dp->d_flags & D_REMOVABLE)
tyname = "removable";
else if (dp->d_flags & D_RAMDISK)
tyname = "simulated";
else
tyname = "winchester";
spc = dp->d_secpercyl;
/*
* Bad sector table contains one track for the replicated
@ -199,7 +217,7 @@ main(argc, argv)
if (def >= NDEFAULTS) {
fprintf(stderr, "%s: disk too small, calculate by hand\n",
*argv);
exit(3);
exit(1);
}
/*
@ -340,6 +358,7 @@ main(argc, argv)
startcyl[part], startcyl[part] + numcyls[part] - 1,
defpart[def][part] % spc ? "*" : "");
}
exit(0);
}
struct disklabel disk;
@ -358,24 +377,26 @@ struct field {
struct disklabel *
promptfordisk(name)
char *name;
const char *name;
{
register struct disklabel *dp = &disk;
register struct field *fp;
register i;
struct disklabel *dp = &disk;
struct field *fp;
int i;
char buf[BUFSIZ], **tp, *cp;
strncpy(dp->d_typename, name, sizeof(dp->d_typename));
fprintf(stderr,
"%s: unknown disk type, want to supply parameters (y/n)? ",
name);
(void) fgets(buf, BUFSIZ, stdin);
if (*buf != 'y')
if ((fgets(buf, BUFSIZ, stdin) == NULL) || buf[0] != 'y')
return ((struct disklabel *)0);
for (;;) {
fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]);
(void) fgets(buf, BUFSIZ, stdin);
if (buf[0] == 0) {
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
if (buf[0] == '\0') {
dp->d_type = 1;
break;
}
@ -384,7 +405,7 @@ promptfordisk(name)
break;
}
fprintf(stderr, "%s: unrecognized controller type\n", buf);
fprintf(stderr, "use one of:\n", buf);
fprintf(stderr, "use one of:\n");
for (tp = dktypenames; *tp; tp++)
if (strchr(*tp, ' ') == 0)
fprintf(stderr, "\t%s\n", *tp);
@ -392,30 +413,48 @@ promptfordisk(name)
gettype:
dp->d_flags = 0;
fprintf(stderr, "type (winchester|removable|simulated)? ");
(void) fgets(buf, BUFSIZ, stdin);
if (strcmp(buf, "removable") == 0)
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
if (buf[0] == '\0')
goto gettype;
switch (buf[0]) {
case 'r':
dp->d_flags = D_REMOVABLE;
else if (strcmp(buf, "simulated") == 0)
break;
case 's':
dp->d_flags = D_RAMDISK;
else if (strcmp(buf, "winchester")) {
break;
case 'w':
break;
default:
fprintf(stderr, "%s: bad disk type\n", buf);
/* FALLTHROUGH */
case '\0':
goto gettype;
}
strncpy(dp->d_typename, buf, sizeof(dp->d_typename));
fprintf(stderr, "(type <cr> to get default value, if only one)\n");
if (dp->d_type == DTYPE_SMD)
fprintf(stderr, "Do %ss support bad144 bad block forwarding (yes)? ",
dp->d_typename);
(void) fgets(buf, BUFSIZ, stdin);
if (*buf != 'n')
dp->d_flags |= D_BADSECT;
if (dp->d_type == DTYPE_SMD) {
fprintf(stderr,
"Do '%s' disks support bad144 bad block forwarding (yes)? ",
dp->d_typename);
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if (buf[0] != 'n')
dp->d_flags |= D_BADSECT;
}
for (fp = fields; fp->f_name != NULL; fp++) {
again:
fprintf(stderr, "%s ", fp->f_name);
if (fp->f_defaults != NULL)
fprintf(stderr, "(%s)", fp->f_defaults);
fprintf(stderr, "? ");
cp = fgets(buf, BUFSIZ, stdin);
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
cp = buf;
if (*cp == '\0') {
if (fp->f_defaults == NULL) {
fprintf(stderr, "no default value\n");
@ -431,7 +470,10 @@ again:
}
fprintf(stderr, "sectors/cylinder (%d)? ",
dp->d_nsectors * dp->d_ntracks);
(void) fgets(buf, BUFSIZ, stdin);
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
if (buf[0] == 0)
dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks;
else
@ -439,7 +481,10 @@ again:
fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n");
for (i = 0; i < NDDATA; i++) {
fprintf(stderr, "d%d? ", i);
(void) fgets(buf, BUFSIZ, stdin);
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
if (buf[0] == 0)
break;
dp->d_drivedata[i] = atol(buf);
@ -447,32 +492,24 @@ again:
return (dp);
}
int
gettype(t, names)
char *t;
const char *t;
char **names;
{
register char **nm;
char **nm;
for (nm = names; *nm; nm++)
if (ustrcmp(t, *nm) == 0)
if (strcasecmp(t, *nm) == 0)
return (nm - names);
if (isdigit(*t))
return (atoi(t));
return (-1);
}
ustrcmp(s1, s2)
register char *s1, *s2;
void
usage(void)
{
#define lower(c) (islower(c) ? (c) : tolower(c))
for (; *s1; s1++, s2++) {
if (*s1 == *s2)
continue;
if (isalpha(*s1) && isalpha(*s2) &&
lower(*s1) == lower(*s2))
continue;
return (*s2 - *s1);
}
return (0);
(void)fprintf(stderr, "Usage: diskpart [-dp] [-s size] disk-type\n");
exit(1);
}