summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/quota/quota.c231
1 files changed, 130 insertions, 101 deletions
diff --git a/usr.bin/quota/quota.c b/usr.bin/quota/quota.c
index 7d689881f97..8b932f29ec8 100644
--- a/usr.bin/quota/quota.c
+++ b/usr.bin/quota/quota.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: quota.c,v 1.14 2000/02/01 03:23:36 deraadt Exp $ */
+/* $OpenBSD: quota.c,v 1.15 2000/10/18 22:15:29 pjanzen Exp $ */
/*
* Copyright (c) 1980, 1990, 1993
@@ -44,7 +44,7 @@ static char copyright[] =
#ifndef lint
/*static char sccsid[] = "from: @(#)quota.c 8.1 (Berkeley) 6/6/93";*/
-static char rcsid[] = "$OpenBSD: quota.c,v 1.14 2000/02/01 03:23:36 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: quota.c,v 1.15 2000/10/18 22:15:29 pjanzen Exp $";
#endif /* not lint */
/*
@@ -56,17 +56,21 @@ static char rcsid[] = "$OpenBSD: quota.c,v 1.14 2000/02/01 03:23:36 deraadt Exp
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/socket.h>
+
#include <ufs/ufs/quota.h>
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <fstab.h>
+#include <grp.h>
+#include <netdb.h>
+#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
-#include <fstab.h>
-#include <ctype.h>
#include <string.h>
-#include <pwd.h>
-#include <grp.h>
-#include <errno.h>
+#include <time.h>
+#include <unistd.h>
-#include <netdb.h>
#include <rpc/rpc.h>
#include <rpc/pmap_prot.h>
#include <rpcsvc/rquota.h>
@@ -82,13 +86,31 @@ struct quotause {
};
#define FOUND 0x01
-char *timeprt __P((time_t seconds));
-struct quotause *getprivs __P((long id, int quotatype));
+int alldigits __P((char *));
+int callaurpc __P((char *, int, int, int, xdrproc_t, void *,
+ xdrproc_t, void *));
+int main __P((int, char **));
+int getnfsquota __P((struct statfs *, struct fstab *, struct quotause *,
+ long, int));
+struct quotause *getprivs __P((long id, int quotatype));
+int getufsquota __P((struct statfs *, struct fstab *, struct quotause *,
+ long, int));
+void heading __P((int, u_long, const char *, const char *));
+void showgid __P((gid_t));
+void showgrpname __P((const char *));
+void showquotas __P((int, u_long, const char *));
+void showuid __P((uid_t));
+void showusrname __P((const char *));
+char *timeprt __P((time_t seconds));
+int ufshasquota __P((struct fstab *, int, char **));
+void usage __P((void));
int qflag;
int vflag;
+int
main(argc, argv)
+ int argc;
char *argv[];
{
int ngroups;
@@ -126,10 +148,8 @@ main(argc, argv)
if (gflag) {
mygid = getgid();
ngroups = getgroups(NGROUPS, gidset);
- if (ngroups < 0) {
- perror("quota: getgroups");
- exit(1);
- }
+ if (ngroups < 0)
+ err(1, "getgroups");
showgid(mygid);
for (i = 0; i < ngroups; i++)
if (gidset[i] != mygid)
@@ -157,27 +177,30 @@ main(argc, argv)
}
exit(0);
}
+ /* NOTREACHED */
}
+void
usage()
{
fprintf(stderr, "%s\n%s\n%s\n",
- "Usage: quota [-guqv]",
- "\tquota [-qv] -u username ...",
- "\tquota [-qv] -g groupname ...");
+ "Usage: quota [-guqv]",
+ "\tquota [-qv] -u username ...",
+ "\tquota [-qv] -g groupname ...");
exit(1);
}
/*
* Print out quotas for a specified user identifier.
*/
+void
showuid(uid)
uid_t uid;
{
struct passwd *pwd = getpwuid(uid);
uid_t myuid;
- char *name;
+ const char *name;
if (pwd == NULL)
name = "(no account)";
@@ -185,7 +208,7 @@ showuid(uid)
name = pwd->pw_name;
myuid = getuid();
if (uid != myuid && myuid != 0) {
- printf("quota: %s (uid %u): permission denied\n", name, uid);
+ warnx("%s (uid %u): permission denied", name, uid);
return;
}
showquotas(USRQUOTA, uid, name);
@@ -194,20 +217,20 @@ showuid(uid)
/*
* Print out quotas for a specifed user name.
*/
+void
showusrname(name)
- char *name;
+ const char *name;
{
struct passwd *pwd = getpwnam(name);
uid_t myuid;
if (pwd == NULL) {
- fprintf(stderr, "quota: %s: unknown user\n", name);
+ warnx("%s: unknown user", name);
return;
}
myuid = getuid();
if (pwd->pw_uid != myuid && myuid != 0) {
- fprintf(stderr, "quota: %s (uid %u): permission denied\n",
- pwd->pw_name, pwd->pw_uid);
+ warnx("%s (uid %u): permission denied", pwd->pw_name, pwd->pw_uid);
return;
}
showquotas(USRQUOTA, pwd->pw_uid, pwd->pw_name);
@@ -216,14 +239,15 @@ showusrname(name)
/*
* Print out quotas for a specified group identifier.
*/
+void
showgid(gid)
gid_t gid;
{
struct group *grp = getgrgid(gid);
int ngroups;
gid_t mygid, gidset[NGROUPS];
- register int i;
- char *name;
+ int i;
+ const char *name;
if (grp == NULL)
name = "(no entry)";
@@ -232,7 +256,7 @@ showgid(gid)
mygid = getgid();
ngroups = getgroups(NGROUPS, gidset);
if (ngroups < 0) {
- perror("quota: getgroups");
+ warn("getgroups");
return;
}
if (gid != mygid) {
@@ -240,9 +264,7 @@ showgid(gid)
if (gid == gidset[i])
break;
if (i >= ngroups && getuid() != 0) {
- fprintf(stderr,
- "quota: %s (gid %u): permission denied\n",
- name, gid);
+ warnx("%s (gid %u): permission denied", name, gid);
return;
}
}
@@ -252,22 +274,23 @@ showgid(gid)
/*
* Print out quotas for a specifed group name.
*/
+void
showgrpname(name)
- char *name;
+ const char *name;
{
struct group *grp = getgrnam(name);
int ngroups;
gid_t mygid, gidset[NGROUPS];
- register int i;
+ int i;
if (grp == NULL) {
- fprintf(stderr, "quota: %s: unknown group\n", name);
+ warnx("%s: unknown group", name);
return;
}
mygid = getgid();
ngroups = getgroups(NGROUPS, gidset);
if (ngroups < 0) {
- perror("quota: getgroups");
+ warn("getgroups");
return;
}
if (grp->gr_gid != mygid) {
@@ -275,8 +298,7 @@ showgrpname(name)
if (grp->gr_gid == gidset[i])
break;
if (i >= ngroups && getuid() != 0) {
- fprintf(stderr,
- "quota: %s (gid %d): permission denied\n",
+ warnx("%s (gid %u): permission denied",
grp->gr_name, grp->gr_gid);
return;
}
@@ -284,16 +306,16 @@ showgrpname(name)
showquotas(GRPQUOTA, grp->gr_gid, grp->gr_name);
}
+void
showquotas(type, id, name)
int type;
u_long id;
- char *name;
+ const char *name;
{
- register struct quotause *qup;
+ struct quotause *qup;
struct quotause *quplist;
char *msgi, *msgb, *nam;
- uid_t myuid, fd, lines = 0;
- static int first;
+ uid_t lines = 0;
static time_t now;
if (now == 0)
@@ -311,21 +333,23 @@ showquotas(type, id, name)
qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit)
msgi = "File limit reached on";
else if (qup->dqblk.dqb_isoftlimit &&
- qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit)
+ qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
if (qup->dqblk.dqb_itime > now)
msgi = "In file grace period on";
else
msgi = "Over file quota on";
+ }
msgb = (char *)0;
if (qup->dqblk.dqb_bhardlimit &&
qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit)
msgb = "Block limit reached on";
else if (qup->dqblk.dqb_bsoftlimit &&
- qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit)
+ qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
if (qup->dqblk.dqb_btime > now)
msgb = "In block grace period on";
else
msgb = "Over block quota on";
+ }
if (qflag) {
if ((msgi != (char *)0 || msgb != (char *)0) &&
lines++ == 0)
@@ -347,20 +371,23 @@ showquotas(type, id, name)
nam = "";
}
printf("%15s%8d%c%7d%8d%8s"
- , nam
- , dbtob(qup->dqblk.dqb_curblocks) / 1024
- , (msgb == (char *)0) ? ' ' : '*'
- , dbtob(qup->dqblk.dqb_bsoftlimit) / 1024
- , dbtob(qup->dqblk.dqb_bhardlimit) / 1024
- , (msgb == (char *)0) ? ""
- : timeprt(qup->dqblk.dqb_btime));
+ , nam
+ , (int)(dbtob((u_quad_t)qup->dqblk.dqb_curblocks)
+ / 1024)
+ , (msgb == (char *)0) ? ' ' : '*'
+ , (int)(dbtob((u_quad_t)qup->dqblk.dqb_bsoftlimit)
+ / 1024)
+ , (int)(dbtob((u_quad_t)qup->dqblk.dqb_bhardlimit)
+ / 1024)
+ , (msgb == (char *)0) ? ""
+ : timeprt(qup->dqblk.dqb_btime));
printf("%8d%c%7d%8d%8s\n"
- , qup->dqblk.dqb_curinodes
- , (msgi == (char *)0) ? ' ' : '*'
- , qup->dqblk.dqb_isoftlimit
- , qup->dqblk.dqb_ihardlimit
- , (msgi == (char *)0) ? ""
- : timeprt(qup->dqblk.dqb_itime)
+ , qup->dqblk.dqb_curinodes
+ , (msgi == (char *)0) ? ' ' : '*'
+ , qup->dqblk.dqb_isoftlimit
+ , qup->dqblk.dqb_ihardlimit
+ , (msgi == (char *)0) ? ""
+ : timeprt(qup->dqblk.dqb_itime)
);
continue;
}
@@ -369,25 +396,26 @@ showquotas(type, id, name)
heading(type, id, name, "none");
}
+void
heading(type, id, name, tag)
int type;
u_long id;
- char *name, *tag;
+ const char *name, *tag;
{
- printf("Disk quotas for %s %s (%cid %d): %s\n", qfextension[type],
+ printf("Disk quotas for %s %s (%cid %ld): %s\n", qfextension[type],
name, *qfextension[type], id, tag);
if (!qflag && tag[0] == '\0') {
printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n"
- , "Filesystem"
- , "blocks"
- , "quota"
- , "limit"
- , "grace"
- , "files"
- , "quota"
- , "limit"
- , "grace"
+ , "Filesystem"
+ , "blocks"
+ , "quota"
+ , "limit"
+ , "grace"
+ , "files"
+ , "quota"
+ , "limit"
+ , "grace"
);
}
}
@@ -411,14 +439,16 @@ timeprt(seconds)
minutes = (seconds + 30) / 60;
hours = (minutes + 30) / 60;
if (hours >= 36) {
- snprintf(buf, sizeof buf, "%ddays", (hours + 12) / 24);
+ (void)snprintf(buf, sizeof buf, "%ddays",
+ (int)((hours + 12) / 24));
return (buf);
}
if (minutes >= 60) {
- snprintf(buf, sizeof buf, "%2d:%d", minutes / 60, minutes % 60);
+ (void)snprintf(buf, sizeof buf, "%2d:%d",
+ (int)(minutes / 60), (int)(minutes % 60));
return (buf);
}
- snprintf(buf, sizeof buf, "%2d", minutes);
+ (void)snprintf(buf, sizeof buf, "%2d", (int)minutes);
return (buf);
}
@@ -427,29 +457,25 @@ timeprt(seconds)
*/
struct quotause *
getprivs(id, quotatype)
- register long id;
+ long id;
int quotatype;
{
- register struct quotause *qup, *quptail;
- register struct fstab *fs;
+ struct quotause *qup, *quptail;
+ struct fstab *fs;
struct quotause *quphead;
struct statfs *fst;
int nfst, i;
- qup = quphead = (struct quotause *)0;
+ qup = quphead = NULL;
nfst = getmntinfo(&fst, MNT_WAIT);
- if (nfst == 0) {
- fprintf(stderr, "quota: no filesystems mounted!\n");
- exit(2);
- }
+ if (nfst == 0)
+ errx(2, "no filesystems mounted!");
setfsent();
- for (i=0; i<nfst; i++) {
+ for (i = 0; i < nfst; i++) {
if (qup == NULL) {
- if ((qup = (struct quotause *)malloc(sizeof *qup)) == NULL) {
- fprintf(stderr, "quota: out of memory\n");
- exit(2);
- }
+ if ((qup = (struct quotause *)malloc(sizeof *qup)) == NULL)
+ errx(2, "out of memory");
}
if (strncmp(fst[i].f_fstypename, "nfs", MFSNAMELEN) == 0) {
if (getnfsquota(&fst[i], NULL, qup, id, quotatype) == 0)
@@ -490,8 +516,9 @@ getprivs(id, quotatype)
/*
* Check to see if a particular quota is to be enabled.
*/
+int
ufshasquota(fs, type, qfnamep)
- register struct fstab *fs;
+ struct fstab *fs;
int type;
char **qfnamep;
{
@@ -499,16 +526,18 @@ ufshasquota(fs, type, qfnamep)
static char buf[BUFSIZ];
char *opt, *cp;
+ cp = NULL;
if (!initname) {
- snprintf(usrname, sizeof usrname, "%s%s",
+ (void)snprintf(usrname, sizeof usrname, "%s%s",
qfextension[USRQUOTA], qfname);
- snprintf(grpname, sizeof grpname, "%s%s",
+ (void)snprintf(grpname, sizeof grpname, "%s%s",
qfextension[GRPQUOTA], qfname);
initname = 1;
}
strncpy(buf, fs->fs_mntops, sizeof buf);
+ buf[sizeof(buf) - 1] = '\0';
for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
- if (cp = strchr(opt, '='))
+ if ((cp = strchr(opt, '=')))
*cp++ = '\0';
if (type == USRQUOTA && strcmp(opt, usrname) == 0)
break;
@@ -521,7 +550,7 @@ ufshasquota(fs, type, qfnamep)
*qfnamep = cp;
return (1);
}
- (void) snprintf(buf, sizeof buf, "%s/%s.%s",
+ (void)snprintf(buf, sizeof buf, "%s/%s.%s",
fs->fs_file, qfname, qfextension[type]);
*qfnamep = buf;
return (1);
@@ -544,10 +573,10 @@ getufsquota(fst, fs, qup, id, quotatype)
if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
if ((fd = open(qfpathname, O_RDONLY)) < 0) {
- perror(qfpathname);
+ warn("%s", qfpathname);
return (0);
}
- (void) lseek(fd, (off_t)(id * sizeof(struct dqblk)), SEEK_SET);
+ (void)lseek(fd, (off_t)(id * sizeof(struct dqblk)), SEEK_SET);
switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
case 0: /* EOF */
/*
@@ -559,8 +588,7 @@ getufsquota(fst, fs, qup, id, quotatype)
case sizeof(struct dqblk): /* OK */
break;
default: /* ERROR */
- fprintf(stderr, "quota: read error");
- perror(qfpathname);
+ warn("read error `%s'", qfpathname);
close(fd);
return (0);
}
@@ -597,8 +625,7 @@ getnfsquota(fst, fs, qup, id, quotatype)
*/
cp = strchr(fst->f_mntfromname, ':');
if (cp == NULL) {
- fprintf(stderr, "cannot find hostname for %s\n",
- fst->f_mntfromname);
+ warnx("cannot find hostname for %s", fst->f_mntfromname);
return (0);
}
@@ -621,8 +648,7 @@ getnfsquota(fst, fs, qup, id, quotatype)
case Q_NOQUOTA:
break;
case Q_EPERM:
- fprintf(stderr, "quota permission error, host: %s\n",
- fst->f_mntfromname);
+ warnx("permission error, host: %s", fst->f_mntfromname);
break;
case Q_OK:
gettimeofday(&tv, NULL);
@@ -651,8 +677,7 @@ getnfsquota(fst, fs, qup, id, quotatype)
*cp = ':';
return (1);
default:
- fprintf(stderr, "bad rpc result, host: %s\n",
- fst->f_mntfromname);
+ warnx("bad rpc result, host: %s", fst->f_mntfromname);
break;
}
*cp = ':';
@@ -662,8 +687,11 @@ getnfsquota(fst, fs, qup, id, quotatype)
int
callaurpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
char *host;
- xdrproc_t inproc, outproc;
- char *in, *out;
+ int prognum, versnum, procnum;
+ xdrproc_t inproc;
+ void *in;
+ xdrproc_t outproc;
+ void *out;
{
struct sockaddr_in server_addr;
enum clnt_stat clnt_stat;
@@ -696,15 +724,16 @@ callaurpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
return ((int) clnt_stat);
}
+int
alldigits(s)
- register char *s;
+ char *s;
{
- register c;
+ int c;
c = *s++;
do {
if (!isdigit(c))
return (0);
- } while (c = *s++);
+ } while ((c = *s++));
return (1);
}