Module Name:    src
Committed By:   christos
Date:           Sun Mar  6 22:33:55 UTC 2011

Modified Files:
        src/usr.sbin/repquota: Makefile repquota.c

Log Message:
- KNF
- merge code
- fix error printing
- don't use static buffers


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/usr.sbin/repquota/Makefile
cvs rdiff -u -r1.26 -r1.27 src/usr.sbin/repquota/repquota.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/repquota/Makefile
diff -u src/usr.sbin/repquota/Makefile:1.6 src/usr.sbin/repquota/Makefile:1.7
--- src/usr.sbin/repquota/Makefile:1.6	Sun Mar  6 12:08:43 2011
+++ src/usr.sbin/repquota/Makefile	Sun Mar  6 17:33:55 2011
@@ -1,6 +1,7 @@
 #	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 22:33:55 christos Exp $
 
+WARNS ?= 4
 .include <bsd.own.mk>
 PROG=	repquota
 SRCS=	repquota.c 
@@ -11,7 +12,7 @@
 LDADD=	-lprop
 
 .PATH:	${NETBSDSRCDIR}/usr.bin/quota
-SRCS+=	printquota.c
+SRCS+=	printquota.c quotautil.c
 .PATH:	${NETBSDSRCDIR}/sys/ufs/ufs 
 SRCS+=	quota2_prop.c quota2_subr.c quota1_subr.c
 

Index: src/usr.sbin/repquota/repquota.c
diff -u src/usr.sbin/repquota/repquota.c:1.26 src/usr.sbin/repquota/repquota.c:1.27
--- src/usr.sbin/repquota/repquota.c:1.26	Sun Mar  6 12:08:43 2011
+++ src/usr.sbin/repquota/repquota.c	Sun Mar  6 17:33:55 2011
@@ -1,3 +1,5 @@
+/*	$NetBSD: repquota.c,v 1.27 2011/03/06 22:33:55 christos Exp $	*/
+
 /*
  * Copyright (c) 1980, 1990, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -40,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)repquota.c	8.2 (Berkeley) 11/22/94";
 #else
-__RCSID("$NetBSD: repquota.c,v 1.26 2011/03/06 17:08:43 bouyer Exp $");
+__RCSID("$NetBSD: repquota.c,v 1.27 2011/03/06 22:33:55 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -67,49 +69,42 @@
 #include <ufs/ufs/quota2_prop.h>
 #include <ufs/ufs/quota1.h>
 
-#include <printquota.h>
-
-const char *qfextension[] = INITQFNAMES;
-const char *qfname = QUOTAFILENAME;
+#include "printquota.h"
+#include "quotautil.h"
 
 struct fileusage {
 	struct	fileusage *fu_next;
 	struct	quota2_entry fu_q2e;
-	u_long	fu_id;
+	uint32_t	fu_id;
 	char	fu_name[1];
 	/* actually bigger */
 };
 #define FUHASH 1024	/* must be power of two */
-struct fileusage *fuhead[MAXQUOTAS][FUHASH];
-u_long highid[MAXQUOTAS];	/* highest addid()'ed identifier per type */
+static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
+static uint32_t highid[MAXQUOTAS];	/* highest addid()'ed identifier per type */
 int valid[MAXQUOTAS];
-struct quota2_entry defaultq2e[MAXQUOTAS];
+static struct quota2_entry defaultq2e[MAXQUOTAS];
 
-int	vflag = 0;		/* verbose */
-int	aflag = 0;		/* all file systems */
-int	Dflag = 0;		/* debug */
-int	hflag = 0;		/* humanize */
-int	xflag = 0;		/* export */
-
-
-struct fileusage *addid(u_long, int, const char *);
-int	hasquota(struct fstab *, int, char **);
-struct fileusage *lookup(u_long, int);
-struct fileusage *qremove(u_long, int);
-int	main(int, char **);
-int	oneof(const char *, char **, int);
-int	repquota(const struct statvfs *, int);
-int	repquota2(const struct statvfs *, int);
-int	repquota1(const struct statvfs *, int);
-void	usage(void);
-void	printquotas(int, const struct statvfs *, int);
-void	exportquotas(void);
-void	dqblk2q2e(const struct dqblk *, struct quota2_entry *);
+static int	vflag = 0;		/* verbose */
+static int	aflag = 0;		/* all file systems */
+static int	Dflag = 0;		/* debug */
+static int	hflag = 0;		/* humanize */
+static int	xflag = 0;		/* export */
+
+
+static struct fileusage *addid(uint32_t, int, const char *);
+static struct fileusage *lookup(uint32_t, int);
+static struct fileusage *qremove(uint32_t, int);
+static int	oneof(const char *, char **, int);
+static int	repquota(const struct statvfs *, int);
+static int	repquota2(const struct statvfs *, int);
+static int	repquota1(const struct statvfs *, int);
+static void	usage(void) __attribute__((__noreturn__));
+static void	printquotas(int, const struct statvfs *, int);
+static void	exportquotas(void);
 
 int
-main(argc, argv)
-	int argc;
-	char **argv;
+main(int argc, char **argv)
 {
 	int gflag = 0, uflag = 0, errs = 0;
 	long i, argnum, done = 0;
@@ -158,7 +153,7 @@
 
 	nfst = getmntinfo(&fst, MNT_WAIT);
 	if (nfst == 0)
-		errx(2, "no filesystems mounted!");
+		errx(1, "no filesystems mounted!");
 	for (i = 0; i < nfst; i++) {
 		if ((fst[i].f_flag & ST_QUOTA) == 0)
 			continue;
@@ -171,7 +166,7 @@
 		}
 		if ((argnum = oneof(fst[i].f_mntonname, argv, argc)) >= 0 ||
 		    (argnum = oneof(fst[i].f_mntfromname, argv, argc)) >= 0) {
-			done |= 1 << argnum;
+			done |= 1U << argnum;
 			if (gflag)
 				errs += repquota(&fst[i], GRPQUOTA);
 			if (uflag)
@@ -181,36 +176,35 @@
 	if (xflag)
 		exportquotas();
 	for (i = 0; i < argc; i++)
-		if ((done & (1 << i)) == 0)
-			fprintf(stderr, "%s not mounted\n", argv[i]);
-	exit(errs);
+		if ((done & (1U << i)) == 0)
+			warnx("%s not mounted", argv[i]);
+	return errs;
 }
 
-void
-usage()
-{
-	fprintf(stderr, "usage:\n"
-		"\trepquota [-D] [-v] [-g] [-u] -a\n"
-		"\trepquota [-D] [-v] [-g] [-u] filesys ...\n"
-		"\trepquota -x [-D] [-g] [-u] filesys\n");
+static void
+usage(void)
+{
+	const char *p = getprogname();
+	fprintf(stderr, "usage: %s [-D] [-v] [-g] [-u] -a\n"
+		"\t%s [-D] [-v] [-g] [-u] filesys ...\n"
+		"\t%s -x [-D] [-g] [-u] filesys\n", p, p, p);
 	exit(1);
 }
 
-int
+static int
 repquota(const struct statvfs *vfs, int type)
 {
 	if (repquota2(vfs, type) != 0)
-		return (repquota1(vfs, type));
+		return repquota1(vfs, type);
 	return 0;
 }
 
-int
+static int
 repquota2(const struct statvfs *vfs, int type)
 {
 	prop_dictionary_t dict, data, cmd;
 	prop_array_t cmds, datas;
 	struct plistref pref;
-	int error;
 	int8_t error8, version = 0;
 	prop_object_iterator_t cmditer, dataiter;
 	struct quota2_entry *q2ep;
@@ -241,16 +235,14 @@
 	if (quotactl(vfs->f_mntonname, &pref) != 0)
 		err(1, "quotactl");
 
-	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 (Dflag)
 		printf("reply from kernel:\n%s\n",
 		    prop_dictionary_externalize(dict));
-	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");
 	}
 	cmditer = prop_array_iterator(cmds);
 	if (cmditer == NULL)
@@ -268,10 +260,10 @@
 		if (error8) {
 			prop_object_release(dict);
 			if (error8 != EOPNOTSUPP) {
-				fprintf(stderr, "get %s quotas: %s\n",
-				    qfextension[type], strerror(error8));
+				errno = error8;
+				warn("get %s quotas", qfextension[type]);
 			}
-			return (error8);
+			return error8;
 		}
 		datas = prop_dictionary_get(cmd, "data");
 		if (datas == NULL)
@@ -310,11 +302,9 @@
 				q2ep->q2e_uid = id;
 			}
 				
-			error = quota2_dict_get_q2e_usage(data, q2ep);
-			if (error) {
-				errx(1, "quota2_dict_get_q2e_usage: %s\n",
-				    strerror(error));
-			}
+			errno = quota2_dict_get_q2e_usage(data, q2ep);
+			if (errno)
+				err(1, "quota2_dict_get_q2e_usage");
 		}
 		prop_object_iterator_release(dataiter);
 	}
@@ -322,16 +312,17 @@
 	prop_object_release(dict);
 	if (xflag == 0)
 		printquotas(type, vfs, version);
-	return (0);
+	return 0;
 }
 
-int repquota1(const struct statvfs *vfs, int type)
+static int
+repquota1(const struct statvfs *vfs, int type)
 {
-	char *qfpathname;
+	char qfpathname[MAXPATHLEN];
 	struct fstab *fs;
 	struct fileusage *fup;
 	FILE *qf;
-	u_long id;
+	uint32_t id;
 	struct dqblk dqbuf;
 	time_t bgrace = MAX_DQ_TIME, igrace = MAX_DQ_TIME;
 
@@ -343,15 +334,15 @@
 	}
 	endfsent();
 	if (fs == NULL) {
-		fprintf(stderr, "%s not found in fstab\n", vfs->f_mntonname);
+		warnx("%s not found in fstab", vfs->f_mntonname);
 		return 1;
 	}
-	if (!hasquota(fs, type, &qfpathname))
+	if (!hasquota(qfpathname, sizeof(qfpathname), fs, type))
 		return 0;
 		
 	if ((qf = fopen(qfpathname, "r")) == NULL) {
-		perror(qfpathname);
-		return (1);
+		warn("Cannot open `%s'", qfpathname);
+		return 1;
 	}
 	for (id = 0; ; id++) {
 		fread(&dqbuf, sizeof(struct dqblk), 1, qf);
@@ -383,19 +374,21 @@
 	valid[type] = 1;
 	if (xflag == 0)
 		printquotas(type, vfs, 1);
-	return (0);
+	return 0;
 }
 
-void
+static void
 printquotas(int type, const struct statvfs *vfs, int version)
 {
 	static int multiple = 0;
-	u_long id;
+	uint32_t id;
 	int i;
 	struct fileusage *fup;
+	struct quota2_val *q;
 	const char *timemsg[N_QL];
 	char overchar[N_QL];
-	static time_t now;
+	time_t now;
+	char b0[20], b1[20], b2[20], b3[20];
 
 	switch(type) {
 	case  GRPQUOTA:
@@ -403,7 +396,7 @@
 		struct group *gr;
 		setgrent();
 		while ((gr = getgrent()) != 0)
-			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
+			(void)addid(gr->gr_gid, GRPQUOTA, gr->gr_name);
 		endgrent();
 		break;
 		}
@@ -412,53 +405,50 @@
 		struct passwd *pw;
 		setpwent();
 		while ((pw = getpwent()) != 0)
-			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
+			(void)addid(pw->pw_uid, USRQUOTA, pw->pw_name);
 		endpwent();
 		break;
 		}
 	default:
-		errx(1, "unknown quota type %d\n", type);
+		errx(1, "unknown quota type %d", type);
 	}
 
-	if (now == 0)
-		time(&now);
+	time(&now);
 
 	if (multiple++)
 		printf("\n");
 	if (vflag)
-		fprintf(stdout,
-		    "*** Report for %s quotas on %s (%s, version %d)\n",
+		printf("*** Report for %s quotas on %s (%s, version %d)\n",
 		    qfextension[type], vfs->f_mntonname, vfs->f_mntfromname,
 		    version);
-	printf("                        Block limits               File limits\n");
+	printf("                        Block limits               "
+	    "File limits\n");
 	printf(type == USRQUOTA ? "User " : "Group");
-	printf("            used     soft     hard  grace      used    soft    hard  grace\n");
+	printf("            used     soft     hard  grace      used"
+	    "soft    hard  grace\n");
 	for (id = 0; id <= highid[type]; id++) {
 		fup = qremove(id, type);
+		q = fup->fu_q2e.q2e_val;
 		if (fup == 0)
 			continue;
 		for (i = 0; i < N_QL; i++) {
-			switch (QL_STATUS(quota2_check_limit(
-			     &fup->fu_q2e.q2e_val[i], 1, now))) {
+			switch (QL_STATUS(quota2_check_limit(&q[i], 1, now))) {
 			case QL_S_DENY_HARD:
 			case QL_S_DENY_GRACE:
 			case QL_S_ALLOW_SOFT:
-				timemsg[i] = timeprt(now,
-				    fup->fu_q2e.q2e_val[i].q2v_time, 7);
+				timemsg[i] = timeprt(b0, 8, now, q[i].q2v_time);
 				overchar[i] = '+';
 				break;
 			default:
 				timemsg[i] =  (vflag && version == 2) ?
-				    timeprt(0,
-					fup->fu_q2e.q2e_val[i].q2v_grace, 7):
-				    "";
+				    timeprt(b0, 8, 0, q[i].q2v_grace) : "";
 				overchar[i] = '-';
 				break;
 			}
 		}
 
-		if (fup->fu_q2e.q2e_val[QL_BLOCK].q2v_cur == 0 &&
-		    fup->fu_q2e.q2e_val[QL_FILE].q2v_cur == 0 && vflag == 0 &&
+		if (q[QL_BLOCK].q2v_cur == 0 &&
+		    q[QL_FILE].q2v_cur == 0 && vflag == 0 &&
 		    overchar[QL_BLOCK] == '-' && overchar[QL_FILE] == '-')
 			continue;
 		if (strlen(fup->fu_name) > 9)
@@ -467,29 +457,23 @@
 			printf("%-10s", fup->fu_name);
 		printf("%c%c%9s%9s%9s%7s",
 			overchar[QL_BLOCK], overchar[QL_FILE],
-			intprt(fup->fu_q2e.q2e_val[QL_BLOCK].q2v_cur,
-				HN_B, hflag, 9),
-			intprt(fup->fu_q2e.q2e_val[QL_BLOCK].q2v_softlimit,
-				HN_B, hflag, 9),
-			intprt(fup->fu_q2e.q2e_val[QL_BLOCK].q2v_hardlimit,
-				HN_B, hflag, 9),
+			intprt(b1, 10, q[QL_BLOCK].q2v_cur, HN_B, hflag),
+			intprt(b2, 10, q[QL_BLOCK].q2v_softlimit, HN_B, hflag),
+			intprt(b3, 10, q[QL_BLOCK].q2v_hardlimit, HN_B, hflag),
 			timemsg[QL_BLOCK]);
 		printf("  %8s%8s%8s%7s\n",
-			intprt(fup->fu_q2e.q2e_val[QL_FILE].q2v_cur,
-				0, hflag, 8),
-			intprt(fup->fu_q2e.q2e_val[QL_FILE].q2v_softlimit,
-				0, hflag, 8),
-			intprt(fup->fu_q2e.q2e_val[QL_FILE].q2v_hardlimit,
-				0, hflag, 8),
+			intprt(b1, 9, q[QL_FILE].q2v_cur, 0, hflag),
+			intprt(b2, 9, q[QL_FILE].q2v_softlimit, 0, hflag),
+			intprt(b3, 9, q[QL_FILE].q2v_hardlimit, 0, hflag),
 			timemsg[QL_FILE]);
 		free(fup);
 	}
 }
 
-void
-exportquotas()
+static void
+exportquotas(void)
 {
-	u_long id;
+	uint32_t id;
 	struct fileusage *fup;
 	prop_dictionary_t dict, data;
 	prop_array_t cmds, datas;
@@ -543,57 +527,15 @@
 /*
  * Check to see if target appears in list of size cnt.
  */
-int
-oneof(target, list, cnt)
-	const char *target;
-	char *list[];
-	int cnt;
+static int
+oneof(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.
- */
-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) {
-		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
-		sprintf(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) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
-	*qfnamep = buf;
-	return (1);
+			return i;
+	return -1;
 }
 
 /*
@@ -602,24 +544,20 @@
  * Lookup an id of a specific type.
  */
 struct fileusage *
-lookup(id, type)
-	u_long id;
-	int type;
+lookup(uint32_t id, int type)
 {
 	struct fileusage *fup;
 
 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
 		if (fup->fu_id == id)
-			return (fup);
-	return ((struct fileusage *)0);
+			return fup;
+	return NULL;
 }
 /*
  * Lookup and remove an id of a specific type.
  */
-struct fileusage *
-qremove(id, type)
-	u_long id;
-	int type;
+static struct fileusage *
+qremove(uint32_t id, int type)
 {
 	struct fileusage *fup, **fupp;
 
@@ -627,29 +565,26 @@
 		fup = *fupp;
 		if (fup->fu_id == id) {
 			*fupp = fup->fu_next;
-			return (fup);
+			return fup;
 		}
 		fupp = &fup->fu_next;
 	}
-	return ((struct fileusage *)0);
+	return NULL;
 }
 
 /*
  * Add a new file usage id if it does not already exist.
  */
-struct fileusage *
-addid(id, type, name)
-	u_long id;
-	int type;
-	const char *name;
+static struct fileusage *
+addid(uint32_t id, int type, const char *name)
 {
 	struct fileusage *fup, **fhp;
 	struct group *gr = NULL;
 	struct passwd *pw = NULL;
-	int len;
+	size_t len;
 
 	if ((fup = lookup(id, type)) != NULL) {
-		return (fup);
+		return fup;
 	}
 	if (name == NULL) {
 		switch(type) {
@@ -673,10 +608,8 @@
 		len = strlen(name);
 	else
 		len = 10;
-	if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL) {
-		fprintf(stderr, "out of memory for fileusage structures\n");
-		exit(1);
-	}
+	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
+		err(1, "out of memory for fileusage structures");
 	fhp = &fuhead[type][id & (FUHASH - 1)];
 	fup->fu_next = *fhp;
 	*fhp = fup;
@@ -686,8 +619,8 @@
 	if (name) {
 		memmove(fup->fu_name, name, len + 1);
 	} else {
-		sprintf(fup->fu_name, "%lu", (u_long)id);
+		snprintf(fup->fu_name, sizeof(fup->fu_name), "%u", id);
 	}
 	fup->fu_q2e = defaultq2e[type];
-	return (fup);
+	return fup;
 }

Reply via email to