add COMPAT_43 flag for now
[unix-history] / usr / src / bin / df / df.c
CommitLineData
bcf1365c
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
7e8aa4e0 11#endif /* not lint */
bcf1365c 12
97dd7000 13#ifndef lint
3b72df1c 14static char sccsid[] = "@(#)df.c 5.6 (Berkeley) %G%";
7e8aa4e0 15#endif /* not lint */
a98f5f82 16
c7b72342 17#include <sys/param.h>
3b72df1c 18#include <ufs/fs.h>
8d335f7d 19#include <sys/stat.h>
f5064bf4 20#include <errno.h>
8a7d0cad
SL
21#include <stdio.h>
22#include <fstab.h>
23#include <mtab.h>
dbdc06d4 24#include "pathnames.h"
8a7d0cad 25
8d335f7d
BJ
26/*
27 * df
28 */
8a7d0cad 29struct mtab mtab[NMOUNT];
a98f5f82
BJ
30char root[32];
31char *mpath();
c7b72342 32
c7b72342
BJ
33int iflag;
34
f4d092e2
KM
35union {
36 struct fs iu_fs;
37 char dummy[SBSIZE];
38} sb;
39#define sblock sb.iu_fs
c7b72342
BJ
40
41int fi;
42daddr_t alloc();
f4d092e2 43char *strcpy();
c7b72342
BJ
44
45main(argc, argv)
a98f5f82
BJ
46 int argc;
47 char **argv;
c7b72342 48{
dbdc06d4
KB
49 extern int errno, optind;
50 int ch, i;
51 char *strerror();
c7b72342 52
dbdc06d4
KB
53 while ((ch = getopt(argc, argv, "i")) != EOF)
54 switch(ch) {
8a7d0cad
SL
55 case 'i':
56 iflag++;
57 break;
dbdc06d4 58 case '?':
8a7d0cad 59 default:
dbdc06d4
KB
60 fprintf(stderr, "usage: df [-i] [filsys ...]\n");
61 exit(1);
8a7d0cad 62 }
dbdc06d4
KB
63 argc -= optind;
64 argv += optind;
65
66 if ((i = open(_PATH_MTAB, 0)) < 0) {
67 fprintf(stderr, "df: %s: %s\n", _PATH_MTAB, strerror(errno));
68 exit(1);
c7b72342 69 }
dbdc06d4
KB
70 (void) read(i, (char *)mtab, sizeof (mtab));
71 (void) close(i);
dda19894 72 sync();
f4d092e2 73 printf("Filesystem kbytes used avail capacity");
c7b72342 74 if (iflag)
f4d092e2
KM
75 printf(" iused ifree %%iused");
76 printf(" Mounted on\n");
dbdc06d4 77 if (!*argv) {
a98f5f82
BJ
78 struct fstab *fsp;
79
8d335f7d 80 if (setfsent() == 0)
3b72df1c 81 perror(_PATH_FSTAB), exit(1);
a98f5f82 82 while (fsp = getfsent()) {
1f65a82f 83 if (strcmp(fsp->fs_type, FSTAB_RW) &&
351ef297
SL
84 strcmp(fsp->fs_type, FSTAB_RO) &&
85 strcmp(fsp->fs_type, FSTAB_RQ))
f0861f40 86 continue;
c7b72342 87 if (root[0] == 0)
f4d092e2 88 (void) strcpy(root, fsp->fs_spec);
a98f5f82 89 dfree(fsp->fs_spec, 1);
c7b72342 90 }
6c5c8ac3 91 (void)endfsent();
c7b72342
BJ
92 exit(0);
93 }
dbdc06d4
KB
94 while (*argv)
95 dfree(*argv++, 0);
c7b72342
BJ
96}
97
a98f5f82
BJ
98dfree(file, infsent)
99 char *file;
100 int infsent;
c7b72342 101{
dbdc06d4 102 extern int errno;
f4d092e2 103 long totalblks, availblks, avail, free, used;
a98f5f82
BJ
104 struct stat stbuf;
105 struct fstab *fsp;
dbdc06d4 106 char *strerror();
a98f5f82
BJ
107
108 if (stat(file, &stbuf) == 0 &&
109 (stbuf.st_mode&S_IFMT) != S_IFCHR &&
110 (stbuf.st_mode&S_IFMT) != S_IFBLK) {
111 if (infsent) {
f6c7ec9d 112 fprintf(stderr, "%s: screwy fstab entry\n", file);
8d335f7d
BJ
113 return;
114 }
6c5c8ac3 115 (void)setfsent();
a98f5f82
BJ
116 while (fsp = getfsent()) {
117 struct stat stb;
118
119 if (stat(fsp->fs_spec, &stb) == 0 &&
120 stb.st_rdev == stbuf.st_dev) {
121 file = fsp->fs_spec;
6c5c8ac3 122 (void)endfsent();
a98f5f82
BJ
123 goto found;
124 }
125 }
6c5c8ac3 126 (void)endfsent();
a98f5f82
BJ
127 fprintf(stderr, "%s: mounted on unknown device\n", file);
128 return;
8d335f7d 129 }
a98f5f82 130found:
dbdc06d4
KB
131 if ((fi = open(file, 0)) < 0) {
132 fprintf(stderr, "df: %s: %s\n", file, strerror(errno));
c7b72342
BJ
133 return;
134 }
6c5c8ac3 135 if (bread((long)SBOFF, (char *)&sblock, SBSIZE) == 0) {
f5064bf4
RC
136 (void) close(fi);
137 return;
138 }
f4d092e2
KM
139 printf("%-12.12s", file);
140 totalblks = sblock.fs_dsize;
141 free = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
142 sblock.fs_cstotal.cs_nffree;
143 used = totalblks - free;
144 availblks = totalblks * (100 - sblock.fs_minfree) / 100;
145 avail = availblks > used ? availblks - used : 0;
dbdc06d4 146 printf("%8ld%8ld%8ld", totalblks * sblock.fs_fsize / 1024,
f4d092e2
KM
147 used * sblock.fs_fsize / 1024, avail * sblock.fs_fsize / 1024);
148 printf("%6.0f%%",
149 availblks == 0 ? 0.0 : (double) used / (double) availblks * 100.0);
c7b72342 150 if (iflag) {
f4d092e2
KM
151 int inodes = sblock.fs_ncg * sblock.fs_ipg;
152 used = inodes - sblock.fs_cstotal.cs_nifree;
153 printf("%8ld%8ld%6.0f%% ", used, sblock.fs_cstotal.cs_nifree,
154 inodes == 0 ? 0.0 : (double)used / (double)inodes * 100.0);
155 } else
156 printf(" ");
157 printf(" %s\n", mpath(file));
158 (void) close(fi);
c7b72342
BJ
159}
160
f4d092e2
KM
161long lseek();
162
6c5c8ac3
KM
163bread(off, buf, cnt)
164 long off;
a98f5f82 165 char *buf;
c7b72342
BJ
166{
167 int n;
168 extern errno;
169
6c5c8ac3 170 (void) lseek(fi, off, 0);
a98f5f82 171 if ((n=read(fi, buf, cnt)) != cnt) {
f5064bf4
RC
172 /* probably a dismounted disk if errno == EIO */
173 if (errno != EIO) {
6c5c8ac3 174 printf("\nread error off = %ld\n", off);
f5064bf4
RC
175 printf("count = %d; errno = %d\n", n, errno);
176 }
177 return (0);
c7b72342 178 }
f5064bf4 179 return (1);
c7b72342
BJ
180}
181
182/*
183 * Given a name like /dev/rrp0h, returns the mounted path, like /usr.
184 */
8a7d0cad
SL
185char *
186mpath(file)
a98f5f82 187 char *file;
c7b72342 188{
8a7d0cad 189 register struct mtab *mp;
c7b72342
BJ
190
191 if (eq(file, root))
8a7d0cad
SL
192 return ("/");
193 for (mp = mtab; mp < mtab + NMOUNT; mp++)
194 if (eq(file, mp->m_dname))
195 return (mp->m_path);
c7b72342
BJ
196 return "";
197}
198
199eq(f1, f2)
a98f5f82 200 char *f1, *f2;
c7b72342 201{
a98f5f82 202
f6c7ec9d 203 if (strncmp(f1, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
c7b72342 204 f1 += 5;
f6c7ec9d 205 if (strncmp(f2, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
c7b72342 206 f2 += 5;
a98f5f82
BJ
207 if (!strcmp(f1, f2))
208 return (1);
209 if (*f1 == 'r' && !strcmp(f1+1, f2))
210 return (1);
211 if (*f2 == 'r' && !strcmp(f1, f2+1))
212 return (1);
c7b72342 213 if (*f1 == 'r' && *f2 == 'r' && strcmp(f1+1, f2+1) == 0)
a98f5f82
BJ
214 return (1);
215 return (0);
c7b72342 216}