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 void get_fs_type(const char *path, char *result);
62 void update_fs_stats(void)
66 for (i = 0; i < MAX_FS_STATS; ++i) {
67 if (fs_stats[i].set) {
68 update_fs_stat(&fs_stats[i]);
73 void clear_fs_stats(void)
76 for (i = 0; i < MAX_FS_STATS; ++i) {
77 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
81 struct fs_stat *prepare_fs_stat(const char *s)
83 struct fs_stat *new = 0;
86 /* lookup existing or get new */
87 for (i = 0; i < MAX_FS_STATS; ++i) {
88 if (fs_stats[i].set) {
89 if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
98 ERR("too many fs stats");
101 strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
107 static void update_fs_stat(struct fs_stat *fs)
111 if (statfs(fs->path, &s) == 0) {
112 fs->size = (long long)s.f_blocks * s.f_bsize;
113 /* bfree (root) or bavail (non-roots) ? */
114 fs->avail = (long long)s.f_bavail * s.f_bsize;
115 fs->free = (long long)s.f_bfree * s.f_bsize;
116 get_fs_type(fs->path, fs->type);
121 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
122 ERR("statfs '%s': %s", fs->path, strerror(errno));
126 void get_fs_type(const char *path, char *result)
129 #ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME
132 if (statfs(path, &s) == 0) {
133 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
135 ERR("statfs '%s': %s", path, strerror(errno));
139 #else /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
141 /* TODO: walk up the directory tree so it works on on paths that are not actually mount points. */
144 FILE *mtab = setmntent("/etc/mtab", "r");
147 ERR("setmntent /etc/mtab: %s", strerror(errno));
148 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
152 me = getmntent(mtab);
154 // find our path in the mtab
155 while (strcmp(path, me->mnt_dir) && getmntent(mtab));
159 if (me && !strcmp(path, me->mnt_dir)) {
160 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
163 #endif /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
165 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);