xref: /netbsd-src/usr.bin/systat/df.c (revision d5eba984e9da5d2dae7a6fe9e33122496c06a7d0)
1*d5eba984Smatt /*	$NetBSD: df.c,v 1.4 2005/12/24 21:14:50 matt Exp $	*/
2756c7041Shubertf 
3756c7041Shubertf /*-
4756c7041Shubertf  * Copyright (c) 1980, 1992, 1993
5756c7041Shubertf  *	The Regents of the University of California.  All rights reserved.
6756c7041Shubertf  * Copyright (c) 2005
7756c7041Shubertf  *	Hubert Feyrer <hubert@feyrer.de>
8756c7041Shubertf  *
9756c7041Shubertf  * Redistribution and use in source and binary forms, with or without
10756c7041Shubertf  * modification, are permitted provided that the following conditions
11756c7041Shubertf  * are met:
12756c7041Shubertf  * 1. Redistributions of source code must retain the above copyright
13756c7041Shubertf  *    notice, this list of conditions and the following disclaimer.
14756c7041Shubertf  * 2. Redistributions in binary form must reproduce the above copyright
15756c7041Shubertf  *    notice, this list of conditions and the following disclaimer in the
16756c7041Shubertf  *    documentation and/or other materials provided with the distribution.
17756c7041Shubertf  * 3. Neither the name of the University nor the names of its contributors
18756c7041Shubertf  *    may be used to endorse or promote products derived from this software
19756c7041Shubertf  *    without specific prior written permission.
20756c7041Shubertf  *
21756c7041Shubertf  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22756c7041Shubertf  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23756c7041Shubertf  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24756c7041Shubertf  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25756c7041Shubertf  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26756c7041Shubertf  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27756c7041Shubertf  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28756c7041Shubertf  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29756c7041Shubertf  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30756c7041Shubertf  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31756c7041Shubertf  * SUCH DAMAGE.
32756c7041Shubertf  */
33756c7041Shubertf 
34756c7041Shubertf #include <sys/cdefs.h>
35756c7041Shubertf #ifndef lint
36*d5eba984Smatt __RCSID("$NetBSD: df.c,v 1.4 2005/12/24 21:14:50 matt Exp $");
37756c7041Shubertf #endif /* not lint */
38756c7041Shubertf 
39756c7041Shubertf #include <sys/param.h>
40756c7041Shubertf #include <sys/stat.h>
41756c7041Shubertf #include <sys/mount.h>
42756c7041Shubertf 
43756c7041Shubertf #include <curses.h>
44756c7041Shubertf #include <math.h>
45756c7041Shubertf #include <stdlib.h>
46756c7041Shubertf #include <string.h>
47756c7041Shubertf #include <util.h>
48756c7041Shubertf 
49756c7041Shubertf #include "systat.h"
50*d5eba984Smatt #include "extern.h"
51756c7041Shubertf 
52756c7041Shubertf static int nfss;
53756c7041Shubertf static struct statvfs *fss;
545057789fSxtraeme static const char *nodisplay[] = {"procfs", "kernfs", "ptyfs", "null", NULL };
55756c7041Shubertf static int displayall=0;
56756c7041Shubertf 
57756c7041Shubertf 
58756c7041Shubertf WINDOW *
opendf(void)59756c7041Shubertf opendf(void)
60756c7041Shubertf {
61756c7041Shubertf 	return (subwin(stdscr, -1, 0, 5, 0));
62756c7041Shubertf }
63756c7041Shubertf 
64756c7041Shubertf void
closedf(WINDOW * w)65756c7041Shubertf closedf(WINDOW *w)
66756c7041Shubertf {
67756c7041Shubertf 	if (w == NULL)
68756c7041Shubertf 		return;
69756c7041Shubertf 	wclear(w);
70756c7041Shubertf 	wrefresh(w);
71756c7041Shubertf 	delwin(w);
72756c7041Shubertf }
73756c7041Shubertf 
74756c7041Shubertf 
75756c7041Shubertf void
showdf(void)76756c7041Shubertf showdf(void)
77756c7041Shubertf {
78756c7041Shubertf 	int i, j, skip;
79756c7041Shubertf 	char s[MNAMELEN];
80756c7041Shubertf 	char s2[MNAMELEN];
81756c7041Shubertf 	float pct;
82756c7041Shubertf 	int64_t used, bsize, bavail, availblks;
83756c7041Shubertf 	int y;
84756c7041Shubertf 
85756c7041Shubertf 	y=2; /* at what line to start displaying */
86756c7041Shubertf 	for (i=0; i<nfss; i++) {
87756c7041Shubertf 		skip = 0;
88756c7041Shubertf 		for(j=0; nodisplay[j] != NULL; j++) {
89756c7041Shubertf 			if (strcmp(nodisplay[j],
90756c7041Shubertf 				   fss[i].f_fstypename) == 0) {
91756c7041Shubertf 				skip=1;
92756c7041Shubertf 				break;
93756c7041Shubertf 			}
94756c7041Shubertf 		}
95756c7041Shubertf 
96756c7041Shubertf 		if (displayall || !skip) {
97756c7041Shubertf 			wmove(wnd, y, 0);
98756c7041Shubertf 			wclrtoeol(wnd);
99756c7041Shubertf 
100756c7041Shubertf 			snprintf(s, sizeof(s), "%s", fss[i].f_mntonname);
101756c7041Shubertf 			mvwaddstr(wnd, y, 0, s);
102756c7041Shubertf 
103756c7041Shubertf 			used = fss[i].f_blocks - fss[i].f_bfree;
104756c7041Shubertf 			bavail = fss[i].f_bavail;
105756c7041Shubertf 			availblks = bavail + used;
106756c7041Shubertf 			bsize = fss[i].f_frsize;
107756c7041Shubertf 
108756c7041Shubertf 			if (availblks == 0) {
109756c7041Shubertf 				pct = 1.0;    /* full pseudo-disk */
110756c7041Shubertf 			} else {
111756c7041Shubertf 				pct = (1.0 * used) / availblks;
112756c7041Shubertf 			}
113756c7041Shubertf 
114756c7041Shubertf #define FREELEN 7
115756c7041Shubertf 			humanize_number(s2, FREELEN, bavail*bsize,
116756c7041Shubertf 					" |", HN_AUTOSCALE,
117756c7041Shubertf 					HN_B | HN_NOSPACE | HN_DECIMAL);
118756c7041Shubertf 			snprintf(s, sizeof(s), "%*s", FREELEN, s2);
119756c7041Shubertf 			mvwaddstr(wnd, y, 25-FREELEN, s);
120756c7041Shubertf #undef FREELEN
121756c7041Shubertf 
122756c7041Shubertf 			mvwhline(wnd, y, 25, 'X', (int)(51*pct));
123756c7041Shubertf 
124756c7041Shubertf 			y++;
125756c7041Shubertf 		}
126756c7041Shubertf 	}
127756c7041Shubertf 	wmove(wnd, y, 0);
128756c7041Shubertf 	wclrtobot(wnd);
129756c7041Shubertf }
130756c7041Shubertf 
131756c7041Shubertf int
initdf(void)132756c7041Shubertf initdf(void)
133756c7041Shubertf {
134756c7041Shubertf   	nfss = getmntinfo(&fss, MNT_NOWAIT);
135756c7041Shubertf 	if (nfss == 0) {
136756c7041Shubertf 		error("init: getmntinfo error");
137756c7041Shubertf 		return(0);
138756c7041Shubertf 	}
139756c7041Shubertf 
140756c7041Shubertf 	mvwaddstr(wnd, 0, 0, "Disk free %age:");
141756c7041Shubertf 	return(1);
142756c7041Shubertf }
143756c7041Shubertf 
144756c7041Shubertf void
fetchdf(void)145756c7041Shubertf fetchdf(void)
146756c7041Shubertf {
147756c7041Shubertf   	nfss = getmntinfo(&fss, MNT_NOWAIT);
148756c7041Shubertf 	if (nfss == 0) {
149756c7041Shubertf 		error("fetch: getmntinfo error");
150756c7041Shubertf 		return;
151756c7041Shubertf 	}
152756c7041Shubertf }
153756c7041Shubertf 
154756c7041Shubertf void
labeldf(void)155756c7041Shubertf labeldf(void)
156756c7041Shubertf {
157756c7041Shubertf 	wmove(wnd, 0, 0);
158756c7041Shubertf 	wclrtoeol(wnd);
159756c7041Shubertf 	mvwaddstr(wnd, 0, 0, "Filesystem");
160756c7041Shubertf 	mvwaddstr(wnd, 0, 18, "Avail");
161756c7041Shubertf 	mvwaddstr(wnd, 0, 26, "Capacity");
162756c7041Shubertf 	mvwaddstr(wnd, 1, 25, "/0%  /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
163756c7041Shubertf }
164756c7041Shubertf 
165756c7041Shubertf void
df_all(char * args)166756c7041Shubertf df_all(char *args)
167756c7041Shubertf {
168756c7041Shubertf 	displayall=1;
169756c7041Shubertf }
170756c7041Shubertf 
171756c7041Shubertf void
df_some(char * args)172756c7041Shubertf df_some(char *args)
173756c7041Shubertf {
174756c7041Shubertf 	displayall=0;
175756c7041Shubertf }
176756c7041Shubertf 
177