Move a big chunk of code out of a loop into its own function.

This commit is contained in:
dholland 2011-11-30 16:07:28 +00:00
parent bfbef3b99c
commit 0cde282c93

View File

@ -1,4 +1,4 @@
/* $NetBSD: quota.c,v 1.40 2011/11/27 13:24:32 dholland Exp $ */ /* $NetBSD: quota.c,v 1.41 2011/11/30 16:07:28 dholland Exp $ */
/* /*
* Copyright (c) 1980, 1990, 1993 * Copyright (c) 1980, 1990, 1993
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
#if 0 #if 0
static char sccsid[] = "@(#)quota.c 8.4 (Berkeley) 4/28/95"; static char sccsid[] = "@(#)quota.c 8.4 (Berkeley) 4/28/95";
#else #else
__RCSID("$NetBSD: quota.c,v 1.40 2011/11/27 13:24:32 dholland Exp $"); __RCSID("$NetBSD: quota.c,v 1.41 2011/11/30 16:07:28 dholland Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -89,6 +89,7 @@ static struct quotause *getprivs(id_t, int);
static void heading(int, id_t, const char *, const char *); static void heading(int, id_t, const char *, const char *);
static void showgid(gid_t); static void showgid(gid_t);
static void showgrpname(const char *); static void showgrpname(const char *);
static void showonequota(int, id_t, const char *, struct quotause *);
static void showquotas(int, id_t, const char *); static void showquotas(int, id_t, const char *);
static void showuid(uid_t); static void showuid(uid_t);
static void showusrname(const char *); static void showusrname(const char *);
@ -100,6 +101,7 @@ static int hflag = 0;
static int dflag = 0; static int dflag = 0;
static int Dflag = 0; static int Dflag = 0;
static uid_t myuid; static uid_t myuid;
static int needheading;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -318,23 +320,38 @@ showquotas(int type, id_t id, const char *name)
{ {
struct quotause *qup; struct quotause *qup;
struct quotause *quplist; struct quotause *quplist;
const char *msgi, *msgb, *nam, *timemsg;
int lines = 0;
static time_t now;
char b0[20], b1[20], b2[20], b3[20];
if (now == 0) needheading = 1;
time(&now);
quplist = getprivs(id, type); quplist = getprivs(id, type);
for (qup = quplist; qup; qup = qup->next) { for (qup = quplist; qup; qup = qup->next) {
showonequota(type, id, name, qup);
}
if (!qflag) {
/* In case nothing printed, issue a header saying "none" */
heading(type, id, name, "none");
}
}
static void
showonequota(int type, id_t id, const char *name, struct quotause *qup)
{
char b0[20], b1[20], b2[20], b3[20];
const char *msgi, *msgb, *nam, *timemsg;
int ql_stat; int ql_stat;
struct quotaval *q = qup->qv; struct quotaval *q = qup->qv;
static time_t now;
if (now == 0) {
time(&now);
}
if (!vflag && if (!vflag &&
q[QUOTA_LIMIT_BLOCK].qv_softlimit == UQUAD_MAX && q[QUOTA_LIMIT_BLOCK].qv_softlimit == UQUAD_MAX &&
q[QUOTA_LIMIT_BLOCK].qv_hardlimit == UQUAD_MAX && q[QUOTA_LIMIT_BLOCK].qv_hardlimit == UQUAD_MAX &&
q[QUOTA_LIMIT_FILE].qv_softlimit == UQUAD_MAX && q[QUOTA_LIMIT_FILE].qv_softlimit == UQUAD_MAX &&
q[QUOTA_LIMIT_FILE].qv_hardlimit == UQUAD_MAX) q[QUOTA_LIMIT_FILE].qv_hardlimit == UQUAD_MAX)
continue; return;
ql_stat = quota_check_limit(q[QUOTA_LIMIT_FILE].qv_usage, 1, ql_stat = quota_check_limit(q[QUOTA_LIMIT_FILE].qv_usage, 1,
q[QUOTA_LIMIT_FILE].qv_softlimit, q[QUOTA_LIMIT_FILE].qv_softlimit,
q[QUOTA_LIMIT_FILE].qv_hardlimit, q[QUOTA_LIMIT_FILE].qv_hardlimit,
@ -370,19 +387,19 @@ showquotas(int type, id_t id, const char *name)
msgb = NULL; msgb = NULL;
} }
if (qflag) { if (qflag) {
if ((msgi != NULL || msgb != NULL) && if (msgi != NULL) {
lines++ == 0)
heading(type, id, name, ""); heading(type, id, name, "");
if (msgi != NULL)
printf("\t%s %s\n", msgi, qup->fsname); printf("\t%s %s\n", msgi, qup->fsname);
if (msgb != NULL) }
if (msgb != NULL) {
heading(type, id, name, "");
printf("\t%s %s\n", msgb, qup->fsname); printf("\t%s %s\n", msgb, qup->fsname);
continue; }
return;
} }
if (vflag || dflag || msgi || msgb || if (vflag || dflag || msgi || msgb ||
q[QUOTA_LIMIT_BLOCK].qv_usage || q[QUOTA_LIMIT_BLOCK].qv_usage ||
q[QUOTA_LIMIT_FILE].qv_usage) { q[QUOTA_LIMIT_FILE].qv_usage) {
if (lines++ == 0)
heading(type, id, name, ""); heading(type, id, name, "");
nam = qup->fsname; nam = qup->fsname;
if (strlen(qup->fsname) > 4) { if (strlen(qup->fsname) > 4) {
@ -427,16 +444,17 @@ showquotas(int type, id_t id, const char *name)
intprt(b3, 8, q[QUOTA_LIMIT_FILE].qv_hardlimit, intprt(b3, 8, q[QUOTA_LIMIT_FILE].qv_hardlimit,
0, hflag), 0, hflag),
timemsg); timemsg);
continue; return;
} }
}
if (!qflag && lines == 0)
heading(type, id, name, "none");
} }
static void static void
heading(int type, id_t id, const char *name, const char *tag) heading(int type, id_t id, const char *name, const char *tag)
{ {
if (needheading == 0)
return;
needheading = 0;
if (dflag) if (dflag)
printf("Default %s disk quotas: %s\n", printf("Default %s disk quotas: %s\n",
ufs_quota_class_names[type], tag); ufs_quota_class_names[type], tag);