386BSD 0.1 development
[unix-history] / usr / src / usr.sbin / diskpart / diskpart.c
index 219ffcf..19f2ba6 100644 (file)
@@ -1,28 +1,44 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
+ * Copyright (c) 1983, 1988 Regents of the University of California.
  * All rights reserved.
  *
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * 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";
 #endif /* not lint */
 
 #ifndef lint
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)diskpart.c 5.10 (Berkeley) %G%";
+static char sccsid[] = "@(#)diskpart.c 5.11 (Berkeley) 6/1/90";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -101,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) {
@@ -116,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))
@@ -126,14 +148,11 @@ main(argc, argv)
                }
        } else {
                if (dp->d_flags & D_REMOVABLE)
                }
        } else {
                if (dp->d_flags & D_REMOVABLE)
-                       strncpy(dp->d_typename, "removable",
-                           sizeof(dp->d_typename));
+                       tyname = "removable";
                else if (dp->d_flags & D_RAMDISK)
                else if (dp->d_flags & D_RAMDISK)
-                       strncpy(dp->d_typename, "simulated",
-                           sizeof(dp->d_typename));
+                       tyname = "simulated";
                else
                else
-                       strncpy(dp->d_typename, "winchester",
-                           sizeof(dp->d_typename));
+                       tyname = "winchester";
        }
        spc = dp->d_secpercyl;
        /*
        }
        spc = dp->d_secpercyl;
        /*
@@ -141,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);
@@ -150,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
@@ -173,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;
@@ -190,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.
@@ -248,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_typename,
+               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);
@@ -293,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 ? "*" : "");
        }
 }
 
        }
 }