2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
14 #include <sys/types.h>
18 #ifdef HAVE_SYS_STATFS_H
19 #include <sys/statfs.h>
22 /* freebsd && netbsd */
23 #ifdef HAVE_SYS_PARAM_H
24 #include <sys/param.h>
26 #ifdef HAVE_SYS_MOUNT_H
27 #include <sys/mount.h>
30 /* TODO: benchmark which is faster, fstatvfs() or pre-opened fd and
31 * statvfs() (fstatvfs() would handle mounts I think...) */
33 static struct fs_stat fs_stats_[64];
34 struct fs_stat *fs_stats = fs_stats_;
36 void update_fs_stats()
40 for (i = 0; i < 16; i++) {
41 if (fs_stats[i].fd <= 0)
44 fstatfs(fs_stats[i].fd, &s);
46 fs_stats[i].size = (long long) s.f_blocks * s.f_bsize;
47 /* bfree (root) or bavail (non-roots) ? */
48 fs_stats[i].avail = (long long) s.f_bavail * s.f_bsize;
55 for (i = 0; i < 16; i++) {
57 close(fs_stats[i].fd);
61 if (fs_stats[i].path != NULL) {
62 free(fs_stats[i].path);
63 fs_stats[i].path = NULL;
68 struct fs_stat *prepare_fs_stat(const char *s)
72 for (i = 0; i < 16; i++) {
73 struct fs_stat *fs = &fs_stats[i];
75 if (fs->path && strcmp(fs->path, s) == 0)
79 /* when compiled with icc, it crashes when leaving function and open()
80 * is used, I don't know why */
82 /* this icc workaround didn't seem to work */
85 FILE *fp = fopen(s, "r");
93 fs->fd = open(s, O_RDONLY);
95 if (fs->fd <= 0) { /* 0 isn't error but actually it is :) */
96 ERR("open '%s': %s", s, strerror(errno));
100 fs->path = strdup(s);
106 ERR("too many fs stats");