8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / acct / acctdusg.c
bloba32e263cb534d912d71bee1f97175211bb9f3b5d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
26 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
31 #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.8 */
33 * acctdusg [-u file] [-p file] > dtmp-file
34 * -u file for names of files not charged to anyone
35 * -p get password info from file
36 * reads std input (normally from find / -print)
37 * and computes disk resource consumption by login
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <pwd.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <limits.h>
46 #include <libcmdutils.h>
48 #include "acctdef.h"
50 struct disk {
51 struct disk *next; /* next entry at same hash tbl index */
52 uid_t dsk_uid; /* user id of login name */
53 blkcnt_t dsk_du; /* disk usage */
54 char dsk_name[NSZ+1]; /* login name */
55 char validuser; /* set if the uid exists */
58 static char *pfile = NULL;
59 static FILE *nchrg = NULL;
60 static avl_tree_t *tree = NULL;
62 static struct disk *usglist[MAXUSERS]; /* holds data on disk usg by uid */
63 #define HASHKEY(x) ((int)((unsigned int)(x) % MAXUSERS))
65 static struct disk *hash_insert(uid_t);
66 static struct disk *hash_find(uid_t);
67 static void openerr(char *);
68 static void output(void);
69 static void validate_entry(struct disk *, struct passwd *);
70 static void charge(char *);
71 #ifdef DEBUG
72 static void pdisk(void);
73 #endif
75 int
76 main(int argc, char **argv)
78 char fbuf[PATH_MAX+1], *fb;
79 FILE *pwf;
80 int c;
81 struct passwd *pw;
82 struct disk *entry;
84 while ((c = getopt(argc, argv, "p:u:")) != EOF) {
85 switch (c) {
86 case 'u':
87 if ((nchrg = fopen(optarg, "w")) == NULL)
88 openerr(optarg);
89 (void) chmod(optarg, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
90 break;
91 case 'p':
92 pfile = optarg;
93 break;
94 default:
95 exit(1);
99 if (pfile) {
100 if ((pwf = fopen(pfile, "r")) == NULL) {
101 openerr(pfile);
103 /* fill usglist with the user's in the passwd file */
104 while ((pw = fgetpwent(pwf)) != NULL) {
105 if ((entry = hash_find(pw->pw_uid)) == NULL)
106 entry = hash_insert(pw->pw_uid);
107 validate_entry(entry, pw);
109 (void) fclose(pwf);
112 /* charge the files listed in names to users listed in the usglist */
113 while (fgets(fbuf, sizeof (fbuf), stdin) != NULL) {
114 if ((fb = strchr(fbuf, '\n')) != NULL) {
116 * replace the newline char at the end of the
117 * filename with a null character
119 *fb = '\0';
121 charge(fbuf);
124 output();
126 if (nchrg)
127 (void) fclose(nchrg);
128 #ifdef DEBUG
129 pdisk();
130 #endif
131 return (0);
135 * create a new entry and insert.
137 static struct disk *
138 hash_insert(uid_t uid)
140 struct disk *curdisk;
141 int key = HASHKEY(uid);
143 if ((curdisk = malloc(sizeof (struct disk))) == NULL) {
144 (void) fprintf(stderr, "acctdusg: cannot allocate memory "
145 "for hash table entry\n");
146 exit(1);
148 curdisk->dsk_uid = uid;
149 curdisk->dsk_du = 0;
150 curdisk->validuser = 0; /* initially invalid */
151 curdisk->next = usglist[key];
152 usglist[key] = curdisk;
153 return (curdisk);
157 * return the disk entry for given uid. return NULL if not found.
159 static struct disk *
160 hash_find(uid_t uid)
162 struct disk *curdisk;
164 for (curdisk = usglist[HASHKEY(uid)];
165 curdisk != NULL; curdisk = curdisk->next) {
166 if (curdisk->dsk_uid == uid) {
167 return (curdisk);
170 return (NULL);
173 static void
174 openerr(char *file)
176 (void) fprintf(stderr, "Cannot open %s\n", file);
177 exit(1);
180 static void
181 output(void)
183 int index;
184 struct disk *entry;
186 for (index = 0; index < MAXUSERS; index++) {
187 for (entry = usglist[index];
188 entry != NULL; entry = entry->next) {
189 if (entry->dsk_du != 0) {
190 (void) printf("%ld\t%s\t%lld\n",
191 entry->dsk_uid,
192 entry->dsk_name,
193 entry->dsk_du);
200 * Initialize the disk entry for a valid passwd entry.
202 static void
203 validate_entry(struct disk *entry, struct passwd *pw)
205 (void) strlcpy(entry->dsk_name, pw->pw_name,
206 sizeof (entry->dsk_name));
207 entry->validuser = 1;
210 static void
211 charge(char *n)
213 struct stat statb;
214 struct disk *entry;
215 struct passwd *pw;
217 if (lstat(n, &statb) == -1)
218 return;
221 * do not count the duplicate entries.
223 if (statb.st_nlink > 1) {
224 switch (add_tnode(&tree, statb.st_dev, statb.st_ino)) {
225 case 0:
226 /* already exist */
227 return;
228 case 1:
229 /* added */
230 break;
231 default:
232 perror("acctdusg");
233 exit(1);
238 * st_blocks is not defined for character/block special files.
240 if (S_ISCHR(statb.st_mode) || S_ISBLK(statb.st_mode))
241 statb.st_blocks = 0;
244 * If -p is given, we've all loaded the passwd entries.
245 * Files with unknown uid should go into nchrg. Otherwise
246 * (without -p), we try creating new entry for the uid.
248 if ((entry = hash_find(statb.st_uid)) == NULL) {
249 if (pfile == NULL) {
250 pw = getpwuid(statb.st_uid);
251 entry = hash_insert(statb.st_uid);
252 if (pw != NULL) {
253 validate_entry(entry, pw);
258 if (entry != NULL && entry->validuser) {
259 entry->dsk_du += statb.st_blocks;
260 } else if (nchrg) {
261 (void) fprintf(nchrg, "%9ld\t%7llu\t%s\n",
262 statb.st_uid, statb.st_blocks, n);
266 #ifdef DEBUG
267 static void
268 pdisk()
270 int index;
271 struct disk *entry;
273 for (index = 0; index < MAXUSERS; index++) {
274 for (entry = usglist[index];
275 entry != NULL; entry = entry->next) {
276 (void) fprintf(stderr, "%.8s\t%9ld\t%7llu\n",
277 entry->dsk_name,
278 entry->dsk_uid,
279 entry->dsk_du);
284 #endif