BSD 4_4_Lite1 release
[unix-history] / usr / src / sbin / tunefs / tunefs.c
index aed5a79..583dbff 100644 (file)
@@ -2,7 +2,33 @@
  * Copyright (c) 1983, 1993
  *     The Regents of the University of California.  All rights reserved.
  *
  * Copyright (c) 1983, 1993
  *     The Regents of the University of California.  All rights reserved.
  *
- * %sccs.include.redist.c%
+ * 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
  */
 
 #ifndef lint
@@ -12,7 +38,7 @@ static char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)tunefs.c   8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)tunefs.c   8.2 (Berkeley) 4/19/94";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -23,11 +49,17 @@ static char sccsid[] = "@(#)tunefs.c        8.1 (Berkeley) %G%";
 
 #include <ufs/ffs/fs.h>
 
 
 #include <ufs/ffs/fs.h>
 
-#include <unistd.h>
 #include <errno.h>
 #include <errno.h>
+#include <err.h>
+#include <fcntl.h>
 #include <fstab.h>
 #include <stdio.h>
 #include <paths.h>
 #include <fstab.h>
 #include <stdio.h>
 #include <paths.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/* the optimization warning string template */
+#define        OPTWARN "should optimize for %s with minfree %s %d%%"
 
 union {
        struct  fs sb;
 
 union {
        struct  fs sb;
@@ -38,6 +70,12 @@ union {
 int fi;
 long dev_bsize = 1;
 
 int fi;
 long dev_bsize = 1;
 
+void bwrite(daddr_t, char *, int);
+int bread(daddr_t, char *, int);
+void getsb(struct fs *, char *);
+void usage __P((void));
+
+int
 main(argc, argv)
        int argc;
        char *argv[];
 main(argc, argv)
        int argc;
        char *argv[];
@@ -51,7 +89,7 @@ main(argc, argv)
 
        argc--, argv++; 
        if (argc < 2)
 
        argc--, argv++; 
        if (argc < 2)
-               goto usage;
+               usage();
        special = argv[argc - 1];
        fs = getfsfile(special);
        if (fs)
        special = argv[argc - 1];
        fs = getfsfile(special);
        if (fs)
@@ -65,12 +103,11 @@ again:
                        special = device;
                        goto again;
                }
                        special = device;
                        goto again;
                }
-               fprintf(stderr, "tunefs: "); perror(special);
-               exit(1);
+               err(1, "%s", special);
        }
        if ((st.st_mode & S_IFMT) != S_IFBLK &&
            (st.st_mode & S_IFMT) != S_IFCHR)
        }
        if ((st.st_mode & S_IFMT) != S_IFBLK &&
            (st.st_mode & S_IFMT) != S_IFCHR)
-               fatal("%s: not a block or character device", special);
+               errx(10, "%s: not a block or character device", special);
        getsb(&sblock, special);
        for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
                for (cp = &argv[0][1]; *cp; cp++)
        getsb(&sblock, special);
        for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
                for (cp = &argv[0][1]; *cp; cp++)
@@ -83,14 +120,14 @@ again:
                        case 'a':
                                name = "maximum contiguous block count";
                                if (argc < 1)
                        case 'a':
                                name = "maximum contiguous block count";
                                if (argc < 1)
-                                       fatal("-a: missing %s", name);
+                                       errx(10, "-a: missing %s", name);
                                argc--, argv++;
                                i = atoi(*argv);
                                if (i < 1)
                                argc--, argv++;
                                i = atoi(*argv);
                                if (i < 1)
-                                       fatal("%s: %s must be >= 1",
-                                               *argv, name);
-                               fprintf(stdout, "%s changes from %d to %d\n",
-                                       name, sblock.fs_maxcontig, i);
+                                       errx(10, "%s must be >= 1 (was %s)",
+                                           name, *argv);
+                               warnx("%s changes from %d to %d",
+                                   name, sblock.fs_maxcontig, i);
                                sblock.fs_maxcontig = i;
                                continue;
 
                                sblock.fs_maxcontig = i;
                                continue;
 
@@ -98,12 +135,11 @@ again:
                                name =
                                   "rotational delay between contiguous blocks";
                                if (argc < 1)
                                name =
                                   "rotational delay between contiguous blocks";
                                if (argc < 1)
-                                       fatal("-d: missing %s", name);
+                                       errx(10, "-d: missing %s", name);
                                argc--, argv++;
                                i = atoi(*argv);
                                argc--, argv++;
                                i = atoi(*argv);
-                               fprintf(stdout,
-                                       "%s changes from %dms to %dms\n",
-                                       name, sblock.fs_rotdelay, i);
+                               warnx("%s changes from %dms to %dms",
+                                   name, sblock.fs_rotdelay, i);
                                sblock.fs_rotdelay = i;
                                continue;
 
                                sblock.fs_rotdelay = i;
                                continue;
 
@@ -111,41 +147,40 @@ again:
                                name =
                                  "maximum blocks per file in a cylinder group";
                                if (argc < 1)
                                name =
                                  "maximum blocks per file in a cylinder group";
                                if (argc < 1)
-                                       fatal("-e: missing %s", name);
+                                       errx(10, "-e: missing %s", name);
                                argc--, argv++;
                                i = atoi(*argv);
                                if (i < 1)
                                argc--, argv++;
                                i = atoi(*argv);
                                if (i < 1)
-                                       fatal("%s: %s must be >= 1",
-                                               *argv, name);
-                               fprintf(stdout, "%s changes from %d to %d\n",
-                                       name, sblock.fs_maxbpg, i);
+                                       errx(10, "%s must be >= 1 (was %s)",
+                                           name, *argv);
+                               warnx("%s changes from %d to %d",
+                                   name, sblock.fs_maxbpg, i);
                                sblock.fs_maxbpg = i;
                                continue;
 
                        case 'm':
                                name = "minimum percentage of free space";
                                if (argc < 1)
                                sblock.fs_maxbpg = i;
                                continue;
 
                        case 'm':
                                name = "minimum percentage of free space";
                                if (argc < 1)
-                                       fatal("-m: missing %s", name);
+                                       errx(10, "-m: missing %s", name);
                                argc--, argv++;
                                i = atoi(*argv);
                                if (i < 0 || i > 99)
                                argc--, argv++;
                                i = atoi(*argv);
                                if (i < 0 || i > 99)
-                                       fatal("%s: bad %s", *argv, name);
-                               fprintf(stdout,
-                                       "%s changes from %d%% to %d%%\n",
-                                       name, sblock.fs_minfree, i);
+                                       errx(10, "bad %s (%s)", name, *argv);
+                               warnx("%s changes from %d%% to %d%%",
+                                   name, sblock.fs_minfree, i);
                                sblock.fs_minfree = i;
                                sblock.fs_minfree = i;
-                               if (i >= 10 && sblock.fs_optim == FS_OPTSPACE)
-                                       fprintf(stdout, "should optimize %s",
-                                           "for time with minfree >= 10%\n");
-                               if (i < 10 && sblock.fs_optim == FS_OPTTIME)
-                                       fprintf(stdout, "should optimize %s",
-                                           "for space with minfree < 10%\n");
+                               if (i >= MINFREE &&
+                                   sblock.fs_optim == FS_OPTSPACE)
+                                       warnx(OPTWARN, "time", ">=", MINFREE);
+                               if (i < MINFREE &&
+                                   sblock.fs_optim == FS_OPTTIME)
+                                       warnx(OPTWARN, "space", "<", MINFREE);
                                continue;
 
                        case 'o':
                                name = "optimization preference";
                                if (argc < 1)
                                continue;
 
                        case 'o':
                                name = "optimization preference";
                                if (argc < 1)
-                                       fatal("-o: missing %s", name);
+                                       errx(10, "-o: missing %s", name);
                                argc--, argv++;
                                chg[FS_OPTSPACE] = "space";
                                chg[FS_OPTTIME] = "time";
                                argc--, argv++;
                                chg[FS_OPTSPACE] = "space";
                                chg[FS_OPTTIME] = "time";
@@ -154,32 +189,30 @@ again:
                                else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
                                        i = FS_OPTTIME;
                                else
                                else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
                                        i = FS_OPTTIME;
                                else
-                                       fatal("%s: bad %s (options are `space' or `time')",
-                                               *argv, name);
+                                       errx(10, "bad %s (options are `space' or `time')",
+                                           name);
                                if (sblock.fs_optim == i) {
                                if (sblock.fs_optim == i) {
-                                       fprintf(stdout,
-                                               "%s remains unchanged as %s\n",
-                                               name, chg[i]);
+                                       warnx("%s remains unchanged as %s",
+                                           name, chg[i]);
                                        continue;
                                }
                                        continue;
                                }
-                               fprintf(stdout,
-                                       "%s changes from %s to %s\n",
-                                       name, chg[sblock.fs_optim], chg[i]);
+                               warnx("%s changes from %s to %s",
+                                   name, chg[sblock.fs_optim], chg[i]);
                                sblock.fs_optim = i;
                                sblock.fs_optim = i;
-                               if (sblock.fs_minfree >= 10 && i == FS_OPTSPACE)
-                                       fprintf(stdout, "should optimize %s",
-                                           "for time with minfree >= 10%\n");
-                               if (sblock.fs_minfree < 10 && i == FS_OPTTIME)
-                                       fprintf(stdout, "should optimize %s",
-                                           "for space with minfree < 10%\n");
+                               if (sblock.fs_minfree >= MINFREE &&
+                                   i == FS_OPTSPACE)
+                                       warnx(OPTWARN, "time", ">=", MINFREE);
+                               if (sblock.fs_minfree < MINFREE &&
+                                   i == FS_OPTTIME)
+                                       warnx(OPTWARN, "space", "<", MINFREE);
                                continue;
 
                        default:
                                continue;
 
                        default:
-                               fatal("-%c: unknown flag", *cp);
+                               usage();
                        }
        }
        if (argc != 1)
                        }
        }
        if (argc != 1)
-               goto usage;
+               usage();
        bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
        if (Aflag)
                for (i = 0; i < sblock.fs_ncg; i++)
        bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
        if (Aflag)
                for (i = 0; i < sblock.fs_ncg; i++)
@@ -187,7 +220,12 @@ again:
                            (char *)&sblock, SBSIZE);
        close(fi);
        exit(0);
                            (char *)&sblock, SBSIZE);
        close(fi);
        exit(0);
-usage:
+}
+
+void
+usage()
+{
+
        fprintf(stderr, "Usage: tunefs tuneup-options special-device\n");
        fprintf(stderr, "where tuneup-options are:\n");
        fprintf(stderr, "\t-a maximum contiguous blocks\n");
        fprintf(stderr, "Usage: tunefs tuneup-options special-device\n");
        fprintf(stderr, "where tuneup-options are:\n");
        fprintf(stderr, "\t-a maximum contiguous blocks\n");
@@ -198,47 +236,42 @@ usage:
        exit(2);
 }
 
        exit(2);
 }
 
+void
 getsb(fs, file)
        register struct fs *fs;
        char *file;
 {
 
        fi = open(file, 2);
 getsb(fs, file)
        register struct fs *fs;
        char *file;
 {
 
        fi = open(file, 2);
-       if (fi < 0) {
-               fprintf(stderr, "tunefs: %s: %s\n", file, strerror(errno));
-               exit(3);
-       }
-       if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE)) {
-               fprintf(stderr, "tunefs: %s: bad super block\n", file);
-               exit(4);
-       }
-       if (fs->fs_magic != FS_MAGIC) {
-               fprintf(stderr, "tunefs: %s: bad magic number\n", file);
-               exit(5);
-       }
+       if (fi < 0)
+               err(3, "cannot open %s", file);
+       if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
+               err(4, "%s: bad super block", file);
+       if (fs->fs_magic != FS_MAGIC)
+               err(5, "%s: bad magic number", file);
        dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
 }
 
        dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
 }
 
+void
 bwrite(blk, buf, size)
 bwrite(blk, buf, size)
-       char *buf;
        daddr_t blk;
        daddr_t blk;
-       register size;
+       char *buf;
+       int size;
 {
 {
-       if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0) {
-               perror("FS SEEK");
-               exit(6);
-       }
-       if (write(fi, buf, size) != size) {
-               perror("FS WRITE");
-               exit(7);
-       }
+
+       if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
+               err(6, "FS SEEK");
+       if (write(fi, buf, size) != size)
+               err(7, "FS WRITE");
 }
 
 }
 
+int
 bread(bno, buf, cnt)
        daddr_t bno;
        char *buf;
 bread(bno, buf, cnt)
        daddr_t bno;
        char *buf;
+       int cnt;
 {
 {
-       register i;
+       int i;
 
        if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
                return(1);
 
        if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
                return(1);
@@ -249,14 +282,3 @@ bread(bno, buf, cnt)
        }
        return (0);
 }
        }
        return (0);
 }
-
-/* VARARGS1 */
-fatal(fmt, arg1, arg2)
-       char *fmt, *arg1, *arg2;
-{
-
-       fprintf(stderr, "tunefs: ");
-       fprintf(stderr, fmt, arg1, arg2);
-       putc('\n', stderr);
-       exit(10);
-}