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