1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 #include "text_object.h"
39 #include <sys/types.h>
43 #ifdef HAVE_SYS_STATFS_H
44 #include <sys/statfs.h>
47 /* freebsd && netbsd */
48 #ifdef HAVE_SYS_PARAM_H
49 #include <sys/param.h>
51 #ifdef HAVE_SYS_MOUNT_H
52 #include <sys/mount.h>
55 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
59 #define MAX_FS_STATS 64
66 static struct fs_stat fs_stats_[MAX_FS_STATS];
67 struct fs_stat *fs_stats = fs_stats_;
69 static void update_fs_stat(struct fs_stat *fs);
71 void get_fs_type(const char *path, char *result);
73 void update_fs_stats(void)
76 static double last_fs_update = 0.0;
78 if (current_update_time - last_fs_update < 13)
81 for (i = 0; i < MAX_FS_STATS; ++i) {
82 if (fs_stats[i].set) {
83 update_fs_stat(&fs_stats[i]);
86 last_fs_update = current_update_time;
89 void clear_fs_stats(void)
92 for (i = 0; i < MAX_FS_STATS; ++i) {
93 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
97 struct fs_stat *prepare_fs_stat(const char *s)
99 struct fs_stat *new = 0;
102 /* lookup existing or get new */
103 for (i = 0; i < MAX_FS_STATS; ++i) {
104 if (fs_stats[i].set) {
105 if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
114 NORM_ERR("too many fs stats");
117 strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
123 static void update_fs_stat(struct fs_stat *fs)
127 if (statfs(fs->path, &s) == 0) {
128 fs->size = (long long)s.f_blocks * s.f_bsize;
129 /* bfree (root) or bavail (non-roots) ? */
130 fs->avail = (long long)s.f_bavail * s.f_bsize;
131 fs->free = (long long)s.f_bfree * s.f_bsize;
132 get_fs_type(fs->path, fs->type);
137 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
138 NORM_ERR("statfs '%s': %s", fs->path, strerror(errno));
142 void get_fs_type(const char *path, char *result)
145 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
148 if (statfs(path, &s) == 0) {
149 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
151 NORM_ERR("statfs '%s': %s", path, strerror(errno));
155 #else /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
158 FILE *mtab = setmntent("/etc/mtab", "r");
164 NORM_ERR("setmntent /etc/mtab: %s", strerror(errno));
165 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
169 me = getmntent(mtab);
171 // find our path in the mtab
172 search_path = strdup(path);
174 while ((match = strcmp(search_path, me->mnt_dir))
178 fseek(mtab, 0, SEEK_SET);
179 slash = strrchr(search_path, '/');
181 CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
182 if (strlen(slash) == 1) /* trailing slash */
184 else if (strlen(slash) > 1)
187 CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
188 } while (strlen(search_path) > 0);
194 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
197 #endif /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
199 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
203 void init_fs_bar(struct text_object *obj, const char *arg)
207 fb = malloc(sizeof(struct fsbar));
208 memset(fb, 0, sizeof(struct fsbar));
210 fb->w = default_bar_width;
211 fb->h = default_bar_height;
213 arg = scan_bar(arg, &fb->w, &fb->h);
215 while (isspace(*arg)) {
224 fb->fs = prepare_fs_stat(arg);
225 obj->data.opaque = fb;
228 void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size)
231 struct fsbar *fb = obj->data.opaque;
237 val = (double)fb->fs->avail / (double)fb->fs->size;
243 if(output_methods & TO_X) {
244 new_bar(p, fb->w, fb->h, (int)(255 * val));
248 if(!fb->w) fb->w = DEFAULT_BAR_WIDTH_NO_X;
249 new_bar_in_shell(p, p_max_size, (int)(100 * val), fb->w);