summaryrefslogtreecommitdiff
path: root/df.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>1999-10-20 22:08:37 +0000
committerEric Andersen <andersen@codepoet.org>1999-10-20 22:08:37 +0000
commitc49960189a04b73e033016bd0f43fbb950f800e1 (patch)
tree6447b860dad6dd3a463547fa048a4c635d8864c8 /df.c
parent887991c78a1e62beee123d58f6a47f93278f03c9 (diff)
downloadbusybox-w32-c49960189a04b73e033016bd0f43fbb950f800e1.tar.gz
busybox-w32-c49960189a04b73e033016bd0f43fbb950f800e1.tar.bz2
busybox-w32-c49960189a04b73e033016bd0f43fbb950f800e1.zip
Fixed up copyright notices and such
Diffstat (limited to 'df.c')
-rw-r--r--df.c225
1 files changed, 118 insertions, 107 deletions
diff --git a/df.c b/df.c
index 7a72bf8fd..f8a953f7d 100644
--- a/df.c
+++ b/df.c
@@ -1,3 +1,26 @@
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
1#include "internal.h" 24#include "internal.h"
2#include <stdio.h> 25#include <stdio.h>
3#include <mntent.h> 26#include <mntent.h>
@@ -5,42 +28,38 @@
5#include <sys/vfs.h> 28#include <sys/vfs.h>
6#include <fstab.h> 29#include <fstab.h>
7 30
8static const char df_usage[] = "df [filesystem ...]\n" 31static const char df_usage[] = "df [filesystem ...]\n"
9"\n" 32 "\n" "\tPrint the filesystem space used and space available.\n";
10"\tPrint the filesystem space used and space available.\n";
11 33
12 34
13static int 35static int df(char *device, const char *mountPoint)
14df(char* device, const char * mountPoint)
15{ 36{
16 struct statfs s; 37 struct statfs s;
17 long blocks_used; 38 long blocks_used;
18 long blocks_percent_used; 39 long blocks_percent_used;
19 40
20 if ( statfs(mountPoint, &s) != 0 ) { 41 if (statfs(mountPoint, &s) != 0) {
21 perror(mountPoint); 42 perror(mountPoint);
22 return 1; 43 return 1;
23 } 44 }
24 45
25 if ( s.f_blocks > 0 ) { 46 if (s.f_blocks > 0) {
26 blocks_used = s.f_blocks - s.f_bfree; 47 blocks_used = s.f_blocks - s.f_bfree;
27 blocks_percent_used = (long) 48 blocks_percent_used = (long)
28 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5); 49 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);
29 if ( strcmp(device, "/dev/root")==0) 50 if (strcmp(device, "/dev/root") == 0)
30 device=(getfsfile ("/"))->fs_spec; 51 device = (getfsfile("/"))->fs_spec;
31 52
32 printf( 53 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
33 "%-20s %9ld %9ld %9ld %3ld%% %s\n", 54 device,
34 device, 55 (long) (s.f_blocks * (s.f_bsize / 1024.0)),
35 (long)(s.f_blocks * (s.f_bsize / 1024.0)), 56 (long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)),
36 (long)((s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)), 57 (long) (s.f_bavail * (s.f_bsize / 1024.0)),
37 (long)(s.f_bavail * (s.f_bsize / 1024.0)), 58 blocks_percent_used, mountPoint);
38 blocks_percent_used, 59
39 mountPoint); 60 }
40 61
41 } 62 return 0;
42
43 return 0;
44} 63}
45 64
46/* 65/*
@@ -50,87 +69,79 @@ df(char* device, const char * mountPoint)
50 * Given any other file (or directory), find the mount table entry for its 69 * Given any other file (or directory), find the mount table entry for its
51 * filesystem. 70 * filesystem.
52 */ 71 */
53extern struct mntent * 72extern struct mntent *findMountPoint(const char *name, const char *table)
54findMountPoint(const char* name, const char* table)
55{ 73{
56 struct stat s; 74 struct stat s;
57 dev_t mountDevice; 75 dev_t mountDevice;
58 FILE * mountTable; 76 FILE *mountTable;
59 struct mntent * mountEntry; 77 struct mntent *mountEntry;
60 78
61 if ( stat(name, &s) != 0 ) 79 if (stat(name, &s) != 0)
62 return 0; 80 return 0;
63 81
64 if ( (s.st_mode & S_IFMT) == S_IFBLK ) 82 if ((s.st_mode & S_IFMT) == S_IFBLK)
65 mountDevice = s.st_rdev; 83 mountDevice = s.st_rdev;
66 else 84 else
67 mountDevice = s.st_dev; 85 mountDevice = s.st_dev;
68 86
69 87
70 if ( (mountTable = setmntent(table, "r")) == 0 ) 88 if ((mountTable = setmntent(table, "r")) == 0)
71 return 0; 89 return 0;
72 90
73 while ( (mountEntry = getmntent(mountTable)) != 0 ) { 91 while ((mountEntry = getmntent(mountTable)) != 0) {
74 if ( strcmp(name, mountEntry->mnt_dir) == 0 92 if (strcmp(name, mountEntry->mnt_dir) == 0
75 || strcmp(name, mountEntry->mnt_fsname) == 0 ) /* String match. */ 93 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
76 break; 94 break;
77 if ( stat(mountEntry->mnt_fsname, &s) == 0 95 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
78 && s.st_rdev == mountDevice ) /* Match the device. */ 96 break;
79 break; 97 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
80 if ( stat(mountEntry->mnt_dir, &s) == 0 98 break;
81 && s.st_dev == mountDevice ) /* Match the directory's mount point. */ 99 }
82 break; 100 endmntent(mountTable);
83 } 101 return mountEntry;
84 endmntent(mountTable);
85 return mountEntry;
86} 102}
87 103
88 104
89 105
90extern int 106extern int df_main(int argc, char **argv)
91df_main(int argc, char * * argv)
92{ 107{
93 printf("%-20s %-14s %s %s %s %s\n", "Filesystem", 108 printf("%-20s %-14s %s %s %s %s\n", "Filesystem",
94 "1k-blocks", "Used", "Available", "Use%", "Mounted on"); 109 "1k-blocks", "Used", "Available", "Use%", "Mounted on");
95 110
96 if ( argc > 1 ) { 111 if (argc > 1) {
97 struct mntent* mountEntry; 112 struct mntent *mountEntry;
98 int status; 113 int status;
99 114
100 while ( argc > 1 ) { 115 while (argc > 1) {
101 if ( (mountEntry = findMountPoint(argv[1], "/proc/mounts")) == 0 ) 116 if ((mountEntry = findMountPoint(argv[1], "/proc/mounts")) ==
102 { 117 0) {
103 fprintf(stderr, "%s: can't find mount point.\n" ,argv[1]); 118 fprintf(stderr, "%s: can't find mount point.\n", argv[1]);
104 return 1; 119 return 1;
105 } 120 }
106 status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir); 121 status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
107 if ( status != 0 ) 122 if (status != 0)
108 return status; 123 return status;
109 argc--; 124 argc--;
110 argv++; 125 argv++;
111 }
112 return 0;
113 }
114 else {
115 FILE * mountTable;
116 struct mntent * mountEntry;
117
118 mountTable = setmntent("/proc/mounts", "r");
119 if ( mountTable == 0) {
120 perror("/proc/mounts");
121 exit( FALSE);
122 }
123
124 while ( (mountEntry = getmntent (mountTable))) {
125 int status=df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
126 if (status)
127 return status;
128 }
129 endmntent(mountTable);
130 } 126 }
131
132 return 0; 127 return 0;
133} 128 } else {
134 129 FILE *mountTable;
130 struct mntent *mountEntry;
131
132 mountTable = setmntent("/proc/mounts", "r");
133 if (mountTable == 0) {
134 perror("/proc/mounts");
135 exit(FALSE);
136 }
135 137
138 while ((mountEntry = getmntent(mountTable))) {
139 int status = df(mountEntry->mnt_fsname, mountEntry->mnt_dir);
140 if (status)
141 return status;
142 }
143 endmntent(mountTable);
144 }
136 145
146 return 0;
147}