Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/quotaon use common utils



details:   https://anonhg.NetBSD.org/src/rev/1815aace9010
branches:  trunk
changeset: 762976:1815aace9010
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Mar 06 23:24:33 2011 +0000

description:
use common utils

diffstat:

 usr.sbin/quotaon/Makefile  |    8 ++-
 usr.sbin/quotaon/quotaon.c |  150 +++++++++++---------------------------------
 2 files changed, 45 insertions(+), 113 deletions(-)

diffs (282 lines):

diff -r efc484bd6385 -r 1815aace9010 usr.sbin/quotaon/Makefile
--- a/usr.sbin/quotaon/Makefile Sun Mar 06 23:13:22 2011 +0000
+++ b/usr.sbin/quotaon/Makefile Sun Mar 06 23:24:33 2011 +0000
@@ -1,13 +1,19 @@
 #      from: @(#)Makefile      8.1 (Berkeley) 6/6/93
-#      $NetBSD: Makefile,v 1.6 2011/03/06 17:08:43 bouyer Exp $
+#      $NetBSD: Makefile,v 1.7 2011/03/06 23:24:33 christos Exp $
 
 .include <bsd.own.mk>
+WARNS  ?= 4
+
 PROG=  quotaon
 SRCS=  quotaon.c
 MAN=   quotaon.8
 MLINKS=        quotaon.8 quotaoff.8
 LINKS= ${BINDIR}/quotaon ${BINDIR}/quotaoff
 
+.PATH:  ${NETBSDSRCDIR}/usr.bin/quota
+SRCS+=  quotautil.c
+CPPFLAGS+=-I${NETBSDSRCDIR}/usr.bin/quota
+
 CPPFLAGS+=-I${NETBSDSRCDIR}/sys
 DPADD= ${LIBPROP}
 LDADD= -lprop
diff -r efc484bd6385 -r 1815aace9010 usr.sbin/quotaon/quotaon.c
--- a/usr.sbin/quotaon/quotaon.c        Sun Mar 06 23:13:22 2011 +0000
+++ b/usr.sbin/quotaon/quotaon.c        Sun Mar 06 23:24:33 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: quotaon.c,v 1.24 2011/03/06 17:08:43 bouyer Exp $      */
+/*     $NetBSD: quotaon.c,v 1.25 2011/03/06 23:24:33 christos Exp $    */
 
 /*
  * Copyright (c) 1980, 1990, 1993
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)quotaon.c  8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: quotaon.c,v 1.24 2011/03/06 17:08:43 bouyer Exp $");
+__RCSID("$NetBSD: quotaon.c,v 1.25 2011/03/06 23:24:33 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -61,33 +61,27 @@
 #include <err.h>
 #include <fstab.h>
 #include <stdio.h>
+#include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-const char *qfname = QUOTAFILENAME;
-const char *qfextension[] = INITQFNAMES;
-
-int    aflag;          /* all file systems */
-int    gflag;          /* operate on group quotas */
-int    uflag;          /* operate on user quotas */
-int    vflag;          /* verbose */
+#include "quotautil.h"
 
-int main __P((int, char *[]));
+static int     aflag;          /* all file systems */
+static int     gflag;          /* operate on group quotas */
+static int     uflag;          /* operate on user quotas */
+static int     vflag;          /* verbose */
 
-static void usage __P((void));
-static int quotaonoff __P((struct fstab *, int, int, char *));
-static int oneof __P((const char *, char *[], int));
-static int hasquota __P((struct fstab *, int, char **));
-static int readonly __P((struct fstab *));
+static void usage(void) __attribute__((__noreturn__));
+static int quotaonoff(struct fstab *, int, int, const char *);
+static int readonly(struct fstab *);
 
 int
-main(argc, argv)
-       int argc;
-       char *argv[];
+main(int argc, char *argv[])
 {
        struct fstab *fs;
-       char *qfnp;
+       char qfnp[MAXPATHLEN];
        long argnum, done = 0;
        int i, offmode = 0, errs = 0;
        int ch;
@@ -133,50 +127,44 @@
                    strcmp(fs->fs_type, FSTAB_RW))
                        continue;
                if (aflag) {
-                       if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
+                       if (gflag && hasquota(qfnp, sizeof(qfnp), fs, GRPQUOTA))
                                errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
-                       if (uflag && hasquota(fs, USRQUOTA, &qfnp))
+                       if (uflag && hasquota(qfnp, sizeof(qfnp), fs, USRQUOTA))
                                errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
                        continue;
                }
                if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
                    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
-                       done |= 1 << argnum;
-                       if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
+                       done |= 1U << argnum;
+                       if (gflag && hasquota(qfnp, sizeof(qfnp), fs, GRPQUOTA))
                                errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
-                       if (uflag && hasquota(fs, USRQUOTA, &qfnp))
+                       if (uflag && hasquota(qfnp, sizeof(qfnp), fs, USRQUOTA))
                                errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
                }
        }
        endfsent();
        for (i = 0; i < argc; i++)
-               if ((done & (1 << i)) == 0)
+               if ((done & (1U << i)) == 0)
                        warnx("%s not found in fstab", argv[i]);
-       exit(errs);
+       return errs;
 }
 
 static void
-usage()
+usage(void)
 {
-
-       (void) fprintf(stderr, "usage:\n\t%s [-g] [-u] [-v] -a\n",
-           getprogname());
-       (void) fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n",
-           getprogname());
+       const char *p = getprogname();
+       (void) fprintf(stderr, "Usage: %s [-g] [-u] [-v] -a\n"
+           "\t%s [-g] [-u] [-v] filesys ...\n", p, p);
        exit(1);
 }
 
 static int
-quotaonoff(fs, offmode, type, qfpathname)
-       struct fstab *fs;
-       int offmode, type;
-       char *qfpathname;
+quotaonoff( struct fstab *fs, int offmode, int type, const char *qfpathname)
 {
        const char *mode = (offmode == 1) ? "off" : "on";
        prop_dictionary_t dict, data, cmd;
        prop_array_t cmds, datas;
        struct plistref pref;
-       int error;
        int8_t error8;
 
        dict = quota2_prop_create();
@@ -184,7 +172,7 @@
        datas = prop_array_create();
 
        if (strcmp(fs->fs_file, "/") && readonly(fs))
-               return (1);
+               return 1;
 
        if (dict == NULL || cmds == NULL || datas == NULL)
                errx(1, "can't allocate proplist");
@@ -218,14 +206,12 @@
                return(1);
        }
 
-       if ((error = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
-               errx(1, "prop_dictionary_recv_syscall: %s\n",
-                   strerror(error));
-       }
+       if ((errno = prop_dictionary_recv_syscall(&pref, &dict)) != 0)
+               err(1, "prop_dictionary_recv_syscall");
 
-       if ((error = quota2_get_cmds(dict, &cmds)) != 0) {
-               errx(1, "quota2_get_cmds: %s\n", strerror(error));
-       }
+       if ((errno = quota2_get_cmds(dict, &cmds)) != 0)
+               err(1, "quota2_get_cmds");
+
        /* only one command, no need to iter */
        cmd = prop_array_get(cmds, 0);
        if (cmd == NULL)
@@ -235,8 +221,8 @@
                err(1, "prop_get(return)");
 
        if (error8) {
-               warnx("quota%s for %s: %s", mode, fs->fs_file,
-                   strerror(error8));
+               errno = error8;
+               warn("quota%s for %s", mode, fs->fs_file);
                return 1;
        }
 
@@ -244,74 +230,14 @@
                printf("%s: %s quotas turned %s\n",
                    fs->fs_file, qfextension[type], mode);
        }
-       return (0);
-}
-
-/*
- * Check to see if target appears in list of size cnt.
- */
-static int
-oneof(target, list, cnt)
-       const char *target;
-       char *list[];
-       int cnt;
-{
-       int i;
-
-       for (i = 0; i < cnt; i++)
-               if (strcmp(target, list[i]) == 0)
-                       return (i);
-       return (-1);
-}
-
-/*
- * Check to see if a particular quota is to be enabled.
- */
-static int
-hasquota(fs, type, qfnamep)
-       struct fstab *fs;
-       int type;
-       char **qfnamep;
-{
-       char *opt;
-       char *cp = NULL;
-       static char initname, usrname[100], grpname[100];
-       static char buf[BUFSIZ];
-
-       if (!initname) {
-               (void) snprintf(usrname, sizeof(usrname), "%s%s",
-                   qfextension[USRQUOTA], qfname);
-               (void) snprintf(grpname, sizeof(grpname), "%s%s",
-                   qfextension[GRPQUOTA], qfname);
-               initname = 1;
-       }
-       strcpy(buf, fs->fs_mntops);
-       for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
-               if ((cp = strchr(opt, '=')) != NULL)
-                       *cp++ = '\0';
-               if (type == USRQUOTA && strcmp(opt, usrname) == 0)
-                       break;
-               if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
-                       break;
-       }
-       if (!opt)
-               return (0);
-       if (cp) {
-               *qfnamep = cp;
-               return (1);
-       }
-       (void) snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file, qfname,
-           qfextension[type]);
-       *qfnamep = buf;
-       return (1);
+       return 0;
 }
 
 /*
  * Verify file system is mounted and not readonly.
  */
 static int
-readonly(fs)
-       struct fstab *fs;
+readonly(struct fstab *fs)
 {
        struct statvfs fsbuf;
 
@@ -319,11 +245,11 @@
            strcmp(fsbuf.f_mntonname, fs->fs_file) ||
            strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
                printf("%s: not mounted\n", fs->fs_file);
-               return (1);
+               return 1;
        }
        if (fsbuf.f_flag & MNT_RDONLY) {
                printf("%s: mounted read-only\n", fs->fs_file);
-               return (1);
+               return 1;
        }
-       return (0);
+       return 0;
 }



Home | Main Index | Thread Index | Old Index