Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / systat / df.c
blobfed8b6a701fd6dc581931a6592d46fb116b614ee
1 /* $NetBSD: df.c,v 1.3 2005/08/01 02:38:03 xtraeme Exp $ */
3 /*-
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 2005
7 * Hubert Feyrer <hubert@feyrer.de>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: df.c,v 1.3 2005/08/01 02:38:03 xtraeme Exp $");
37 #endif /* not lint */
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/mount.h>
43 #include <curses.h>
44 #include <math.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <util.h>
49 #include "systat.h"
50 #include "extern.h"
52 static int nfss;
53 static struct statvfs *fss;
54 static const char *nodisplay[] = {"procfs", "kernfs", "ptyfs", "null", NULL };
55 static int displayall=0;
58 WINDOW *
59 opendf(void)
61 return (subwin(stdscr, -1, 0, 5, 0));
64 void
65 closedf(WINDOW *w)
67 if (w == NULL)
68 return;
69 wclear(w);
70 wrefresh(w);
71 delwin(w);
75 void
76 showdf(void)
78 int i, j, skip;
79 char s[MNAMELEN];
80 char s2[MNAMELEN];
81 float pct;
82 int64_t used, bsize, bavail, availblks;
83 int y;
85 y=2; /* at what line to start displaying */
86 for (i=0; i<nfss; i++) {
87 skip = 0;
88 for(j=0; nodisplay[j] != NULL; j++) {
89 if (strcmp(nodisplay[j],
90 fss[i].f_fstypename) == 0) {
91 skip=1;
92 break;
96 if (displayall || !skip) {
97 wmove(wnd, y, 0);
98 wclrtoeol(wnd);
100 snprintf(s, sizeof(s), "%s", fss[i].f_mntonname);
101 mvwaddstr(wnd, y, 0, s);
103 used = fss[i].f_blocks - fss[i].f_bfree;
104 bavail = fss[i].f_bavail;
105 availblks = bavail + used;
106 bsize = fss[i].f_frsize;
108 if (availblks == 0) {
109 pct = 1.0; /* full pseudo-disk */
110 } else {
111 pct = (1.0 * used) / availblks;
114 #define FREELEN 7
115 humanize_number(s2, FREELEN, bavail*bsize,
116 " |", HN_AUTOSCALE,
117 HN_B | HN_NOSPACE | HN_DECIMAL);
118 snprintf(s, sizeof(s), "%*s", FREELEN, s2);
119 mvwaddstr(wnd, y, 25-FREELEN, s);
120 #undef FREELEN
122 mvwhline(wnd, y, 25, 'X', (int)(51*pct));
124 y++;
127 wmove(wnd, y, 0);
128 wclrtobot(wnd);
132 initdf(void)
134 nfss = getmntinfo(&fss, MNT_NOWAIT);
135 if (nfss == 0) {
136 error("init: getmntinfo error");
137 return(0);
140 mvwaddstr(wnd, 0, 0, "Disk free %age:");
141 return(1);
144 void
145 fetchdf(void)
147 nfss = getmntinfo(&fss, MNT_NOWAIT);
148 if (nfss == 0) {
149 error("fetch: getmntinfo error");
150 return;
154 void
155 labeldf(void)
157 wmove(wnd, 0, 0);
158 wclrtoeol(wnd);
159 mvwaddstr(wnd, 0, 0, "Filesystem");
160 mvwaddstr(wnd, 0, 18, "Avail");
161 mvwaddstr(wnd, 0, 26, "Capacity");
162 mvwaddstr(wnd, 1, 25, "/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
165 void
166 df_all(char *args)
168 displayall=1;
171 void
172 df_some(char *args)
174 displayall=0;