install correct aliases file
[unix-history] / usr / src / usr.sbin / repquota / repquota.c
CommitLineData
2a18434a
KM
1/*
2 * Copyright (c) 1980 Regents of the University of California.
f9ac90b4
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b8c620d6
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2a18434a
KM
16 */
17
18#ifndef lint
19char copyright[] =
20"@(#) Copyright (c) 1980 Regents of the University of California.\n\
21 All rights reserved.\n";
f9ac90b4 22#endif /* not lint */
2a18434a 23
fe35ac74 24#ifndef lint
b8c620d6 25static char sccsid[] = "@(#)repquota.c 5.6 (Berkeley) %G%";
f9ac90b4 26#endif /* not lint */
fe35ac74
KM
27
28/*
29 * Quota report
30 */
31#include <stdio.h>
824e2e71 32#include <errno.h>
fe35ac74 33#include <sys/param.h>
fe35ac74 34#include <sys/quota.h>
6c299bfc
KM
35#include <sys/stat.h>
36#include <fstab.h>
37#include <pwd.h>
fe35ac74 38
6c299bfc
KM
39#define LOGINNAMESIZE 8
40struct fileusage {
41 struct fileusage *fu_next;
42 struct dqblk fu_dqblk;
43 u_short fu_uid;
44 char fu_name[LOGINNAMESIZE + 1];
45};
46#define FUHASH 997
47struct fileusage *fuhead[FUHASH];
48struct fileusage *lookup();
49struct fileusage *adduid();
50int highuid;
fe35ac74 51
6c299bfc
KM
52long done;
53struct passwd *getpwent();
fe35ac74 54
6c299bfc
KM
55int vflag; /* verbose */
56int aflag; /* all file systems */
57
58char *qfname = "quotas";
6c299bfc 59struct dqblk zerodqblk;
fe35ac74
KM
60
61main(argc, argv)
6c299bfc 62 int argc;
fe35ac74
KM
63 char **argv;
64{
6c299bfc
KM
65 register struct fstab *fs;
66 register struct passwd *pw;
67 register struct fileusage *fup;
68 char quotafile[MAXPATHLEN];
69 int i, errs = 0;
fe35ac74 70
6c299bfc
KM
71again:
72 argc--, argv++;
73 if (argc > 0 && strcmp(*argv, "-v") == 0) {
74 vflag++;
75 goto again;
76 }
77 if (argc > 0 && strcmp(*argv, "-a") == 0) {
78 aflag++;
79 goto again;
80 }
81 if (argc <= 0 && !aflag) {
82 fprintf(stderr, "Usage:\n\t%s\n\t%s\n",
83 "repquota [-v] -a",
84 "repquota [-v] filesys ...");
fe35ac74
KM
85 exit(1);
86 }
6c299bfc
KM
87 setpwent();
88 while ((pw = getpwent()) != 0) {
89 fup = lookup(pw->pw_uid);
27cff295 90 if (fup == 0) {
6c299bfc 91 fup = adduid(pw->pw_uid);
27cff295
JL
92 strncpy(fup->fu_name, pw->pw_name, sizeof(fup->fu_name));
93 }
6c299bfc
KM
94 }
95 endpwent();
96 setfsent();
97 while ((fs = getfsent()) != NULL) {
98 if (aflag &&
99 (fs->fs_type == 0 || strcmp(fs->fs_type, "rq") != 0))
fe35ac74 100 continue;
6c299bfc
KM
101 if (!aflag &&
102 !(oneof(fs->fs_file, argv, argc) ||
103 oneof(fs->fs_spec, argv, argc)))
fe35ac74 104 continue;
6c299bfc 105 (void) sprintf(quotafile, "%s/%s", fs->fs_file, qfname);
2dd469d2 106 errs += repquota(fs->fs_spec, fs->fs_file, quotafile);
fe35ac74 107 }
6c299bfc
KM
108 endfsent();
109 for (i = 0; i < argc; i++)
110 if ((done & (1 << i)) == 0)
111 fprintf(stderr, "%s not found in /etc/fstab\n",
112 argv[i]);
113 exit(errs);
114}
fe35ac74 115
2dd469d2 116repquota(fsdev, fsfile, qffile)
6c299bfc 117 char *fsdev;
2dd469d2 118 char *fsfile;
6c299bfc
KM
119 char *qffile;
120{
121 register struct fileusage *fup;
122 FILE *qf;
123 u_short uid;
124 struct dqblk dqbuf;
125 struct stat statb;
824e2e71
KM
126 static int warned = 0;
127 extern int errno;
fe35ac74 128
6c299bfc 129 if (vflag)
2dd469d2 130 fprintf(stdout, "*** Quota report for %s (%s)\n", fsdev, fsfile);
6c299bfc
KM
131 qf = fopen(qffile, "r");
132 if (qf == NULL) {
133 perror(qffile);
134 return (1);
135 }
136 if (fstat(fileno(qf), &statb) < 0) {
137 perror(qffile);
2dd469d2 138 fclose(qf);
6c299bfc
KM
139 return (1);
140 }
824e2e71
KM
141 if (quota(Q_SYNC, 0, statb.st_dev, 0) < 0 &&
142 errno == EINVAL && !warned && vflag) {
143 warned++;
144 fprintf(stdout,
145 "*** Warning: Quotas are not compiled into this kernel\n");
146 }
6c299bfc
KM
147 for (uid = 0; ; uid++) {
148 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
149 if (feof(qf))
150 break;
6c299bfc 151 fup = lookup(uid);
2dd469d2
S
152 if ((fup == 0 || fup->fu_name[0] == 0) &&
153 dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
154 continue;
6c299bfc
KM
155 if (fup == 0)
156 fup = adduid(uid);
157 fup->fu_dqblk = dqbuf;
158 }
159 printf(" Block limits File limits\n");
160 printf("User used soft hard warn used soft hard warn\n");
161 for (uid = 0; uid <= highuid; uid++) {
162 fup = lookup(uid);
163 if (fup == 0)
164 continue;
3e5a0658 165 if (fup->fu_dqblk.dqb_curinodes == 0 &&
6c299bfc 166 fup->fu_dqblk.dqb_curblocks == 0)
fe35ac74 167 continue;
6c299bfc
KM
168 if (fup->fu_name[0] != '\0')
169 printf("%-10s", fup->fu_name);
fe35ac74 170 else
6c299bfc
KM
171 printf("#%-9d", uid);
172 printf("%c%c%8d%8d%8d %5d %5d %5d %5d %5d\n",
173 fup->fu_dqblk.dqb_bsoftlimit &&
174 fup->fu_dqblk.dqb_curblocks >=
175 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
176 fup->fu_dqblk.dqb_isoftlimit &&
177 fup->fu_dqblk.dqb_curinodes >=
178 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
285a6d40
JB
179 dbtob(fup->fu_dqblk.dqb_curblocks) / 1024,
180 dbtob(fup->fu_dqblk.dqb_bsoftlimit) / 1024,
181 dbtob(fup->fu_dqblk.dqb_bhardlimit) / 1024,
6c299bfc
KM
182 fup->fu_dqblk.dqb_bwarn,
183 fup->fu_dqblk.dqb_curinodes,
184 fup->fu_dqblk.dqb_isoftlimit,
185 fup->fu_dqblk.dqb_ihardlimit,
186 fup->fu_dqblk.dqb_iwarn);
187 fup->fu_dqblk = zerodqblk;
fe35ac74 188 }
2dd469d2 189 fclose(qf);
6c299bfc
KM
190 return (0);
191}
192
193oneof(target, list, n)
194 char *target, *list[];
195 register int n;
196{
197 register int i;
198
199 for (i = 0; i < n; i++)
200 if (strcmp(target, list[i]) == 0) {
201 done |= 1 << i;
202 return (1);
203 }
204 return (0);
fe35ac74
KM
205}
206
6c299bfc
KM
207struct fileusage *
208lookup(uid)
209 u_short uid;
fe35ac74 210{
6c299bfc 211 register struct fileusage *fup;
fe35ac74 212
6c299bfc
KM
213 for (fup = fuhead[uid % FUHASH]; fup != 0; fup = fup->fu_next)
214 if (fup->fu_uid == uid)
215 return (fup);
216 return ((struct fileusage *)0);
217}
218
219struct fileusage *
220adduid(uid)
221 u_short uid;
222{
223 struct fileusage *fup, **fhp;
2dd469d2 224 extern char *calloc();
6c299bfc
KM
225
226 fup = lookup(uid);
227 if (fup != 0)
228 return (fup);
229 fup = (struct fileusage *)calloc(1, sizeof(struct fileusage));
230 if (fup == 0) {
231 fprintf(stderr, "out of memory for fileusage structures\n");
232 exit(1);
233 }
234 fhp = &fuhead[uid % FUHASH];
235 fup->fu_next = *fhp;
236 *fhp = fup;
237 fup->fu_uid = uid;
238 if (uid > highuid)
239 highuid = uid;
240 return (fup);
fe35ac74 241}