94b6b823180b861da819f01e90082949d351d8c6
[oweals/busybox.git] / df.c
1 /*
2  * Mini df implementation for busybox
3  *
4  * Copyright (C) 1999 by Lineo, inc.
5  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6  * based on original code by (I think) Bruce Perens <bruce@pixar.com>.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26 #include <mntent.h>
27 #include <sys/stat.h>
28 #include <sys/vfs.h>
29 #include <fstab.h>
30
31 static const char df_usage[] = "df [filesystem ...]\n"
32     "\n" "\tPrint the filesystem space used and space available.\n";
33
34 extern const char mtab_file[]; /* Defined in utility.c */
35
36 static int df(char *device, const char *mountPoint)
37 {
38     struct statfs s;
39     long blocks_used;
40     long blocks_percent_used;
41
42     if (statfs(mountPoint, &s) != 0) {
43         perror(mountPoint);
44         return 1;
45     }
46
47     if (s.f_blocks > 0) {
48         blocks_used = s.f_blocks - s.f_bfree;
49         blocks_percent_used = (long)
50             (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);
51         if (strcmp(device, "/dev/root") == 0)
52             device = (getfsfile("/"))->fs_spec;
53
54         printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
55                device,
56                (long) (s.f_blocks * (s.f_bsize / 1024.0)),
57                (long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)),
58                (long) (s.f_bavail * (s.f_bsize / 1024.0)),
59                blocks_percent_used, mountPoint);
60
61     }
62
63     return 0;
64 }
65
66 /*
67  * Given a block device, find the mount table entry if that block device
68  * is mounted.
69  *
70  * Given any other file (or directory), find the mount table entry for its
71  * filesystem.
72  */
73 extern struct mntent *findMountPoint(const char *name, const char *table)
74 {
75     struct stat s;
76     dev_t mountDevice;
77     FILE *mountTable;
78     struct mntent *mountEntry;
79
80     if (stat(name, &s) != 0)
81         return 0;
82
83     if ((s.st_mode & S_IFMT) == S_IFBLK)
84         mountDevice = s.st_rdev;
85     else
86         mountDevice = s.st_dev;
87
88
89     if ((mountTable = setmntent(table, "r")) == 0)
90         return 0;
91
92     while ((mountEntry = getmntent(mountTable)) != 0) {
93         if (strcmp(name, mountEntry->mnt_dir) == 0
94             || strcmp(name, mountEntry->mnt_fsname) == 0)       /* String match. */
95             break;
96         if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice)  /* Match the device. */
97             break;
98         if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice)      /* Match the directory's mount point. */
99             break;
100     }
101     endmntent(mountTable);
102     return mountEntry;
103 }
104
105
106
107 extern int df_main(int argc, char **argv)
108 {
109     printf("%-20s %-14s %s %s %s %s\n", "Filesystem",
110            "1k-blocks", "Used", "Available", "Use%", "Mounted on");
111
112     if (argc > 1) {
113         struct mntent *mountEntry;
114         int status;
115
116         while (argc > 1) {
117             if ((mountEntry = findMountPoint(argv[1], mtab_file)) ==
118                 0) {
119                 fprintf(stderr, "%s: can't find mount point.\n", argv[1]);
120                 return 1;
121             }
122             status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
123             if (status != 0)
124                 return status;
125             argc--;
126             argv++;
127         }
128         return 0;
129     } else {
130         FILE *mountTable;
131         struct mntent *mountEntry;
132
133         mountTable = setmntent(mtab_file, "r");
134         if (mountTable == 0) {
135             perror(mtab_file);
136             exit(FALSE);
137         }
138
139         while ((mountEntry = getmntent(mountTable))) {
140             int status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
141             if (status)
142                 return status;
143         }
144         endmntent(mountTable);
145     }
146
147     return 0;
148 }