1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include <sys/types.h>
37 #ifdef HAVE_SYS_STATFS_H
38 #include <sys/statfs.h>
41 /* freebsd && netbsd */
42 #ifdef HAVE_SYS_PARAM_H
43 #include <sys/param.h>
45 #ifdef HAVE_SYS_MOUNT_H
46 #include <sys/mount.h>
49 #ifndef HAVE_STRUCT_STATFS_F_FSTYPENAME
53 #define MAX_FS_STATS 64
55 static struct fs_stat fs_stats_[MAX_FS_STATS];
56 struct fs_stat *fs_stats = fs_stats_;
58 static void update_fs_stat(struct fs_stat *fs);
60 static char* get_fs_type(const char* path);
62 void update_fs_stats()
66 for (i = 0; i < MAX_FS_STATS; ++i) {
67 if (fs_stats[i].path) {
68 update_fs_stat(&fs_stats[i]);
77 for (i = 0; i < MAX_FS_STATS; ++i) {
78 if (fs_stats[i].path) {
79 free(fs_stats[i].path);
80 free(fs_stats[i].type);
81 fs_stats[i].path = NULL;
86 struct fs_stat *prepare_fs_stat(const char *s)
88 struct fs_stat *new = 0;
91 /* lookup existing or get new */
92 for (i = 0; i < MAX_FS_STATS; ++i) {
93 if (fs_stats[i].path) {
94 if (strcmp(fs_stats[i].path, s) == 0) {
103 ERR("too many fs stats");
106 new->path = strdup(s);
111 static void update_fs_stat(struct fs_stat *fs)
115 if (statfs(fs->path, &s) == 0) {
116 fs->size = (long long) s.f_blocks * s.f_bsize;
117 /* bfree (root) or bavail (non-roots) ? */
118 fs->avail = (long long) s.f_bavail * s.f_bsize;
119 fs->free = (long long) s.f_bfree * s.f_bsize;
120 fs->type = get_fs_type(fs->path);
125 fs->type = "unknown";
126 ERR("statfs '%s': %s", fs->path, strerror(errno));
130 static char* get_fs_type(const char* path)
133 #ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME
136 if(statfs(path, &s) == 0)
137 return s.f_fstypename;
139 ERR("statfs '%s': %s", path, strerror(errno));
143 /* TODO: walk up the directory tree so it works on
144 * on paths that are not actually mount points. */
146 FILE* mtab = setmntent( "/etc/mtab", "r" );
149 ERR("setmntent /etc/mtab: %s", strerror(errno));
153 struct mntent* me = getmntent(mtab);
155 // find our path in the mtab
156 while(getmntent(mtab) && strcmp(path,me->mnt_dir));
161 return strdup(me->mnt_type);
163 #endif // HAVE_STRUCT_STATFS_F_FSTYPENAME