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