xref: /netbsd/usr.bin/systat/df.c (revision 6550d01e)
1 /*	$NetBSD: df.c,v 1.4 2005/12/24 21:14:50 matt Exp $	*/
2 
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>
8  *
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.
20  *
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.
32  */
33 
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: df.c,v 1.4 2005/12/24 21:14:50 matt Exp $");
37 #endif /* not lint */
38 
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/mount.h>
42 
43 #include <curses.h>
44 #include <math.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <util.h>
48 
49 #include "systat.h"
50 #include "extern.h"
51 
52 static int nfss;
53 static struct statvfs *fss;
54 static const char *nodisplay[] = {"procfs", "kernfs", "ptyfs", "null", NULL };
55 static int displayall=0;
56 
57 
58 WINDOW *
59 opendf(void)
60 {
61 	return (subwin(stdscr, -1, 0, 5, 0));
62 }
63 
64 void
65 closedf(WINDOW *w)
66 {
67 	if (w == NULL)
68 		return;
69 	wclear(w);
70 	wrefresh(w);
71 	delwin(w);
72 }
73 
74 
75 void
76 showdf(void)
77 {
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;
84 
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;
93 			}
94 		}
95 
96 		if (displayall || !skip) {
97 			wmove(wnd, y, 0);
98 			wclrtoeol(wnd);
99 
100 			snprintf(s, sizeof(s), "%s", fss[i].f_mntonname);
101 			mvwaddstr(wnd, y, 0, s);
102 
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;
107 
108 			if (availblks == 0) {
109 				pct = 1.0;    /* full pseudo-disk */
110 			} else {
111 				pct = (1.0 * used) / availblks;
112 			}
113 
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
121 
122 			mvwhline(wnd, y, 25, 'X', (int)(51*pct));
123 
124 			y++;
125 		}
126 	}
127 	wmove(wnd, y, 0);
128 	wclrtobot(wnd);
129 }
130 
131 int
132 initdf(void)
133 {
134   	nfss = getmntinfo(&fss, MNT_NOWAIT);
135 	if (nfss == 0) {
136 		error("init: getmntinfo error");
137 		return(0);
138 	}
139 
140 	mvwaddstr(wnd, 0, 0, "Disk free %age:");
141 	return(1);
142 }
143 
144 void
145 fetchdf(void)
146 {
147   	nfss = getmntinfo(&fss, MNT_NOWAIT);
148 	if (nfss == 0) {
149 		error("fetch: getmntinfo error");
150 		return;
151 	}
152 }
153 
154 void
155 labeldf(void)
156 {
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%");
163 }
164 
165 void
166 df_all(char *args)
167 {
168 	displayall=1;
169 }
170 
171 void
172 df_some(char *args)
173 {
174 	displayall=0;
175 }
176 
177