1 static char *sccsid = "@(#)df.c 4.8 (Berkeley) 02/26/82"; 2 3 #include <stdio.h> 4 #include <fstab.h> 5 #include <sys/param.h> 6 #include <sys/filsys.h> 7 #include <sys/fblk.h> 8 #include <sys/stat.h> 9 10 /* 11 * df 12 */ 13 #define NFS 20 /* Max number of filesystems */ 14 15 struct { 16 char path[32]; 17 char spec[32]; 18 } mtab[NFS]; 19 char root[32]; 20 char *mpath(); 21 daddr_t blkno = 1; 22 23 int lflag; 24 int iflag; 25 26 struct filsys sblock; 27 28 int fi; 29 daddr_t alloc(); 30 31 main(argc, argv) 32 int argc; 33 char **argv; 34 { 35 int i; 36 37 while (argc >= 1 && argv[1][0]=='-') { 38 switch (argv[1][1]) { 39 40 case 'i': 41 iflag++; 42 break; 43 44 default: 45 fprintf(stderr, "usage: df [ -i ] [ -l ] [ filsys... ]\n"); 46 exit(0); 47 } 48 argc--, argv++; 49 } 50 i = open("/etc/mtab", 0); 51 if (i >= 0) { 52 read(i, mtab, sizeof mtab); /* Probably returns short */ 53 close(i); 54 } 55 printf("Filesystem Mounted on kbytes used free"); 56 printf(" %% used"); 57 if (iflag) 58 printf(" iused ifree %%iused"); 59 putchar('\n'); 60 if (argc <= 1) { 61 struct fstab *fsp; 62 63 if (setfsent() == 0) 64 perror(FSTAB), exit(1); 65 while (fsp = getfsent()) { 66 if (!strcmp(fsp->fs_type, FSTAB_RW) && 67 !(strcmp(fsp->fs_type, FSTAB_RO))) 68 continue; 69 if (root[0] == 0) 70 strcpy(root, fsp->fs_spec); 71 dfree(fsp->fs_spec, 1); 72 } 73 endfsent(); 74 exit(0); 75 } 76 for (i=1; i<argc; i++) 77 dfree(argv[i], 0); 78 } 79 80 dfree(file, infsent) 81 char *file; 82 int infsent; 83 { 84 daddr_t i; 85 long blocks, free, used, hardway; 86 char *mp; 87 struct stat stbuf; 88 struct fstab *fsp; 89 90 if (stat(file, &stbuf) == 0 && 91 (stbuf.st_mode&S_IFMT) != S_IFCHR && 92 (stbuf.st_mode&S_IFMT) != S_IFBLK) { 93 if (infsent) { 94 fprintf(stderr, "%s: screwy /etc/fstab entry\n", file); 95 return; 96 } 97 setfsent(); 98 while (fsp = getfsent()) { 99 struct stat stb; 100 101 if (stat(fsp->fs_spec, &stb) == 0 && 102 stb.st_rdev == stbuf.st_dev) { 103 file = fsp->fs_spec; 104 endfsent(); 105 goto found; 106 } 107 } 108 endfsent(); 109 fprintf(stderr, "%s: mounted on unknown device\n", file); 110 return; 111 } 112 found: 113 fi = open(file, 0); 114 if (fi < 0) { 115 perror(file); 116 return; 117 } 118 bread((long) 1, (char *)&sblock, sizeof (sblock)); 119 printf("%-12.12s%-14.14s", file, mp = mpath(file)); 120 blocks = (long) sblock.s_fsize - (long)sblock.s_isize; 121 free = sblock.s_tfree; 122 used = blocks - free; 123 printf("%8d%8d%8d", blocks, used, free); 124 printf("%8.0f%%", 125 blocks == 0 ? 0.0 : (double) used / (double)blocks * 100.0); 126 if (iflag) { 127 int inodes = (sblock.s_isize - 2) * INOPB; 128 used = inodes - sblock.s_tinode; 129 printf("%8ld%8ld%8.0f%%", used, sblock.s_tinode, 130 inodes == 0 ? 0.0 : (double)used/(double)inodes*100.0); 131 } 132 printf("\n"); 133 close(fi); 134 } 135 136 bread(bno, buf, cnt) 137 daddr_t bno; 138 char *buf; 139 { 140 int n; 141 extern errno; 142 143 lseek(fi, bno<<BSHIFT, 0); 144 if ((n=read(fi, buf, cnt)) != cnt) { 145 printf("\nread error bno = %ld\n", bno); 146 printf("count = %d; errno = %d\n", n, errno); 147 exit(0); 148 } 149 } 150 151 /* 152 * Given a name like /dev/rrp0h, returns the mounted path, like /usr. 153 */ 154 char *mpath(file) 155 char *file; 156 { 157 register int i; 158 159 if (eq(file, root)) 160 return "/"; 161 for (i=0; i<NFS; i++) 162 if (eq(file, mtab[i].spec)) 163 return (mtab[i].path); 164 return ""; 165 } 166 167 eq(f1, f2) 168 char *f1, *f2; 169 { 170 171 if (strncmp(f1, "/dev/", 5) == 0) 172 f1 += 5; 173 if (strncmp(f2, "/dev/", 5) == 0) 174 f2 += 5; 175 if (!strcmp(f1, f2)) 176 return (1); 177 if (*f1 == 'r' && !strcmp(f1+1, f2)) 178 return (1); 179 if (*f2 == 'r' && !strcmp(f1, f2+1)) 180 return (1); 181 if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0) 182 return (1); 183 return (0); 184 } 185