1: /*
   2:  * Disk usage by user
   3:  */
   4: 
   5: char    *dargv[] = {
   6: #ifndef MENLO
   7:     "/dev/rrp3",
   8: #else
   9:     "/dev/rrp0",  /* /we -- pag 7/1/79 */
  10: #endif
  11:     0
  12: };
  13: 
  14: #include <stdio.h>
  15: #include <ctype.h>
  16: #include <pwd.h>
  17: #include <sys/param.h>
  18: #include <sys/ino.h>
  19: #include <sys/inode.h>
  20: #include <sys/filsys.h>
  21: 
  22: #define ITABSZ  256
  23: #define ISIZ    (BSIZE/sizeof(struct dinode))
  24: #define NUID    300
  25: struct  filsys  sblock;
  26: struct  dinode  itab[ITABSZ];
  27: struct du
  28: {
  29:     long    blocks;
  30:     long    nfiles;
  31:     int uid;
  32:     char    *name;
  33: } du[NUID];
  34: #define TSIZE   500
  35: int sizes[TSIZE];
  36: long    overflow;
  37: 
  38: int nflg;
  39: int fflg;
  40: int cflg;
  41: 
  42: int fi;
  43: unsigned    ino;
  44: unsigned    nfiles;
  45: 
  46: struct  passwd  *getpwent();
  47: char    *malloc();
  48: char    *copy();
  49: 
  50: main(argc, argv)
  51: char **argv;
  52: {
  53:     register int n;
  54:     register struct passwd *lp;
  55:     register char **p;
  56: 
  57:     for(n=0; n<NUID; n++)
  58:         du[n].uid = n;
  59:     while((lp=getpwent()) != 0) {
  60:         n = lp->pw_uid;
  61:         if (n>NUID)
  62:             continue;
  63:         if(du[n].name)
  64:             continue;
  65:         du[n].name = copy(lp->pw_name);
  66:     }
  67:     if (argc == 1) {
  68:         for (p = dargv; *p;) {
  69:             check(*p++);
  70:             report();
  71:         }
  72:         return(0);
  73:     }
  74:     while (--argc) {
  75:         argv++;
  76:         if (argv[0][0]=='-') {
  77:             if (argv[0][1]=='n')
  78:                 nflg++;
  79:             else if (argv[0][1]=='f')
  80:                 fflg++;
  81:             else if (argv[0][1]=='c')
  82:                 cflg++;
  83:         } else {
  84:             check(*argv);
  85:             report();
  86:         }
  87:     }
  88:     return(0);
  89: }
  90: 
  91: check(file)
  92: char *file;
  93: {
  94:     register unsigned i, j;
  95:     register c;
  96: 
  97:     fi = open(file, 0);
  98:     if (fi < 0) {
  99:         printf("cannot open %s\n", file);
 100:         return;
 101:     }
 102:     printf("%s:\n", file);
 103:     sync();
 104:     bread(1, (char *)&sblock, sizeof sblock);
 105:     nfiles = (sblock.s_isize-2)*(BSIZE/sizeof(struct dinode));
 106:     ino = 0;
 107:     if (nflg) {
 108:         if (isdigit(c = getchar()))
 109:             ungetc(c, stdin);
 110:         else while (c!='\n' && c != EOF)
 111:             c = getchar();
 112:     }
 113:     for(i=2; ino<nfiles; i += ITABSZ/ISIZ) {
 114:         bread(i, (char *)itab, sizeof itab);
 115:         for (j=0; j<ITABSZ && ino<nfiles; j++) {
 116:             ino++;
 117:             acct(&itab[j]);
 118:         }
 119:     }
 120: }
 121: 
 122: acct(ip)
 123: register struct dinode *ip;
 124: {
 125:     register n;
 126:     register char *np;
 127:     static fino;
 128: 
 129:     if ((ip->di_mode&IFMT) == 0)
 130:         return;
 131:     if (cflg) {
 132:         if ((ip->di_mode&IFMT)!=IFDIR && (ip->di_mode&IFMT)!=IFREG)
 133:             return;
 134:         n = (ip->di_size+BSIZE-1)/BSIZE;
 135:         if (n >= TSIZE) {
 136:             overflow += n;
 137:             n = TSIZE-1;
 138:         }
 139:         sizes[n]++;
 140:         return;
 141:     }
 142:     if (ip->di_uid >= NUID)
 143:         return;
 144:     du[ip->di_uid].blocks += (ip->di_size+BSIZE-1)/BSIZE;
 145:     du[ip->di_uid].nfiles++;
 146:     if (nflg) {
 147:     tryagain:
 148:         if (fino==0)
 149:             if (scanf("%d", &fino)<=0)
 150:                 return;
 151:         if (fino > ino)
 152:             return;
 153:         if (fino<ino) {
 154:             while ((n=getchar())!='\n' && n!=EOF)
 155:                 ;
 156:             fino = 0;
 157:             goto tryagain;
 158:         }
 159:         if (np = du[ip->di_uid].name)
 160:             printf("%.7s	", np);
 161:         else
 162:             printf("%d	", ip->di_uid);
 163:         while ((n = getchar())==' ' || n=='\t')
 164:             ;
 165:         putchar(n);
 166:         while (n!=EOF && n!='\n') {
 167:             n = getchar();
 168:             putchar(n);
 169:         }
 170:         fino = 0;
 171:     }
 172: }
 173: 
 174: bread(bno, buf, cnt)
 175: unsigned bno;
 176: char *buf;
 177: {
 178: 
 179:     lseek(fi, (long)bno*BSIZE, 0);
 180:     if (read(fi, buf, cnt) != cnt) {
 181:         printf("read error %u\n", bno);
 182:         exit(1);
 183:     }
 184: }
 185: 
 186: qcmp(p1, p2)
 187: register struct du *p1, *p2;
 188: {
 189:     if (p1->blocks > p2->blocks)
 190:         return(-1);
 191:     if (p1->blocks < p2->blocks)
 192:         return(1);
 193:     return(strcmp(p1->name, p2->name));
 194: }
 195: 
 196: report()
 197: {
 198:     register i;
 199: 
 200:     if (nflg)
 201:         return;
 202:     if (cflg) {
 203:         long t = 0;
 204:         for (i=0; i<TSIZE-1; i++)
 205:             if (sizes[i]) {
 206:                 t += i*sizes[i];
 207:                 printf("%d	%d	%D\n", i, sizes[i], t);
 208:             }
 209:         printf("%d	%d	%D\n", TSIZE-1, sizes[TSIZE-1], overflow+t);
 210:         return;
 211:     }
 212:     qsort(du, NUID, sizeof(du[0]), qcmp);
 213:     for (i=0; i<NUID; i++) {
 214:         if (du[i].blocks==0)
 215:             return;
 216:         printf("%5D\t", du[i].blocks);
 217:         if (fflg)
 218:             printf("%5D\t", du[i].nfiles);
 219:         if (du[i].name)
 220:             printf("%s\n", du[i].name);
 221:         else
 222:             printf("#%d\n", du[i].uid);
 223:     }
 224: }
 225: 
 226: char *
 227: copy(s)
 228: char *s;
 229: {
 230:     register char *p;
 231:     register n;
 232: 
 233:     for(n=0; s[n]; n++)
 234:         ;
 235:     p = malloc((unsigned)n+1);
 236:     for(n=0; p[n] = s[n]; n++)
 237:         ;
 238:     return(p);
 239: }

Defined functions

acct defined in line 122; used 1 times
bread defined in line 174; used 2 times
check defined in line 91; used 2 times
copy defined in line 226; used 2 times
main defined in line 50; never used
qcmp defined in line 186; used 1 times
report defined in line 196; used 2 times

Defined variables

cflg defined in line 40; used 3 times
dargv defined in line 5; used 1 times
  • in line 68
du defined in line 33; used 14 times
fflg defined in line 39; used 2 times
fi defined in line 42; used 4 times
ino defined in line 43; used 6 times
itab defined in line 26; used 3 times
nfiles defined in line 44; used 6 times
nflg defined in line 38; used 4 times
overflow defined in line 36; used 2 times
sblock defined in line 25; used 3 times
sizes defined in line 35; used 5 times

Defined struct's

du defined in line 27; used 2 times
  • in line 187(2)

Defined macros

ISIZ defined in line 23; used 1 times
ITABSZ defined in line 22; used 3 times
NUID defined in line 24; used 6 times
TSIZE defined in line 34; used 6 times
Last modified: 1981-07-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 933
Valid CSS Valid XHTML 1.0 Strict