386BSD 0.1 development
[unix-history] / usr / src / usr.sbin / diskpart / diskpart.c
index 1902a71..19f2ba6 100644 (file)
@@ -1,18 +1,45 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1983, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+"@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
  All rights reserved.\n";
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)diskpart.c 5.7 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)diskpart.c 5.11 (Berkeley) 6/1/90";
+#endif /* not lint */
 
 /*
  * Program to calculate standard disk partition sizes.
 
 /*
  * Program to calculate standard disk partition sizes.
@@ -90,11 +117,13 @@ main(argc, argv)
        struct disklabel *dp;
        register int curcyl, spc, def, part, layout, j;
        int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
        struct disklabel *dp;
        register int curcyl, spc, def, part, layout, j;
        int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
-       char *lp;
+       int totsize = 0;
+       char *lp, *tyname;
 
        argc--, argv++;
        if (argc < 1) {
 
        argc--, argv++;
        if (argc < 1) {
-               fprintf(stderr, "usage: disktab [ -p ] [ -d ] disk-type\n");
+               fprintf(stderr,
+                   "usage: disktab [ -p ] [ -d ] [ -s size ] disk-type\n");
                exit(1);
        }
        if (argc > 0 && strcmp(*argv, "-p") == 0) {
                exit(1);
        }
        if (argc > 0 && strcmp(*argv, "-p") == 0) {
@@ -105,6 +134,10 @@ main(argc, argv)
                dflag++;
                argc--, argv++;
        }
                dflag++;
                argc--, argv++;
        }
+       if (argc > 1 && strcmp(*argv, "-s") == 0) {
+               totsize = atoi(argv[1]);
+               argc += 2, argv += 2;
+       }
        dp = getdiskbyname(*argv);
        if (dp == NULL) {
                if (isatty(0))
        dp = getdiskbyname(*argv);
        if (dp == NULL) {
                if (isatty(0))
@@ -115,11 +148,11 @@ main(argc, argv)
                }
        } else {
                if (dp->d_flags & D_REMOVABLE)
                }
        } else {
                if (dp->d_flags & D_REMOVABLE)
-                       strncpy(dp->d_name, "removable", sizeof(dp->d_name));
+                       tyname = "removable";
                else if (dp->d_flags & D_RAMDISK)
                else if (dp->d_flags & D_RAMDISK)
-                       strncpy(dp->d_name, "simulated", sizeof(dp->d_name));
+                       tyname = "simulated";
                else
                else
-                       strncpy(dp->d_name, "winchester", sizeof(dp->d_name));
+                       tyname = "winchester";
        }
        spc = dp->d_secpercyl;
        /*
        }
        spc = dp->d_secpercyl;
        /*
@@ -127,8 +160,10 @@ main(argc, argv)
         * copies of the table and enough full tracks preceding
         * the last track to hold the pool of free blocks to which
         * bad sectors are mapped.
         * copies of the table and enough full tracks preceding
         * the last track to hold the pool of free blocks to which
         * bad sectors are mapped.
+        * If disk size was specified explicitly, use specified size.
         */
         */
-       if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT) {
+       if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT &&
+           totsize == 0) {
                badsecttable = dp->d_nsectors +
                    roundup(badsecttable, dp->d_nsectors);
                threshhold = howmany(spc, badsecttable);
                badsecttable = dp->d_nsectors +
                    roundup(badsecttable, dp->d_nsectors);
                threshhold = howmany(spc, badsecttable);
@@ -136,6 +171,15 @@ main(argc, argv)
                badsecttable = 0;
                threshhold = 0;
        }
                badsecttable = 0;
                threshhold = 0;
        }
+       /*
+        * If disk size was specified, recompute number of cylinders
+        * that may be used, and set badsecttable to any remaining
+        * fraction of the last cylinder.
+        */
+       if (totsize != 0) {
+               dp->d_ncylinders = howmany(totsize, spc);
+               badsecttable = spc * dp->d_ncylinders - totsize;
+       }
 
        /* 
         * Figure out if disk is large enough for
 
        /* 
         * Figure out if disk is large enough for
@@ -159,7 +203,8 @@ main(argc, argv)
        /*
         * Calculate number of cylinders allocated to each disk
         * partition.  We may waste a bit of space here, but it's
        /*
         * Calculate number of cylinders allocated to each disk
         * partition.  We may waste a bit of space here, but it's
-        * in the interest of compatibility (for mixed disk systems).
+        * in the interest of (very backward) compatibility
+        * (for mixed disk systems).
         */
        for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
                numcyls[part] = 0;
         */
        for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
                numcyls[part] = 0;
@@ -176,9 +221,11 @@ main(argc, argv)
        defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
        defpart[def][PART('c')] = numcyls[PART('c')] * spc;
 #ifndef for_now
        defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
        defpart[def][PART('c')] = numcyls[PART('c')] * spc;
 #ifndef for_now
-       if (!pflag)
-               defpart[def][PART('c')] -= badsecttable;
+       if (totsize || !pflag)
+#else
+       if (totsize)
 #endif
 #endif
+               defpart[def][PART('c')] -= badsecttable;
 
        /*
         * Calculate starting cylinder number for each partition.
 
        /*
         * Calculate starting cylinder number for each partition.
@@ -234,7 +281,7 @@ main(argc, argv)
                        defparam[PART('g')].p_fsize = temp;
                }
                printf("%s:\\\n", dp->d_typename);
                        defparam[PART('g')].p_fsize = temp;
                }
                printf("%s:\\\n", dp->d_typename);
-               printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", dp->d_name,
+               printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname,
                        dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
                if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
                        printf("sc#%d:", dp->d_secpercyl);
                        dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
                if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
                        printf("sc#%d:", dp->d_secpercyl);
@@ -279,15 +326,17 @@ main(argc, argv)
        printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
                dp->d_typename, dp->d_nsectors, dp->d_ntracks,
                dp->d_ncylinders);
        printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
                dp->d_typename, dp->d_nsectors, dp->d_ntracks,
                dp->d_ncylinders);
-       printf("\n    Partition\t   Size\t   Range\n");
+       printf("\n    Partition\t   Size\t Offset\t   Range\n");
        for (part = PART('a'); part < NPARTITIONS; part++) {
                printf("\t%c\t", 'a' + part);
                if (numcyls[part] == 0) {
                        printf(" unused\n");
                        continue;
                }
        for (part = PART('a'); part < NPARTITIONS; part++) {
                printf("\t%c\t", 'a' + part);
                if (numcyls[part] == 0) {
                        printf(" unused\n");
                        continue;
                }
-               printf("%7d\t%4d - %d\n", defpart[def][part], startcyl[part],
-                       startcyl[part] + numcyls[part] - 1);
+               printf("%7d\t%7d\t%4d - %d%s\n",
+                       defpart[def][part], startcyl[part] * spc,
+                       startcyl[part], startcyl[part] + numcyls[part] - 1,
+                       defpart[def][part] % spc ? "*" : "");
        }
 }
 
        }
 }
 
@@ -348,11 +397,11 @@ gettype:
                fprintf(stderr, "%s: bad disk type\n", buf);
                goto gettype;
        }
                fprintf(stderr, "%s: bad disk type\n", buf);
                goto gettype;
        }
-       strncpy(dp->d_name, buf, sizeof(dp->d_name));
+       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)? ",
        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_name);
+               dp->d_typename);
        (void) gets(buf);
        if (*buf != 'n')
                dp->d_flags |= D_BADSECT;
        (void) gets(buf);
        if (*buf != 'n')
                dp->d_flags |= D_BADSECT;