clarifying licensing
[monky] / src / fs.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * Any original torsmo code is licensed under the BSD license
5  *
6  * All code written since the fork of torsmo is licensed under the GPL
7  *
8  * Please see COPYING for details
9  *
10  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
11  * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
12  * All rights reserved.
13  *
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.
18  *
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/>. 
25  *
26  *  $Id$
27  */
28
29 #include "conky.h"
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36
37 /* linux */
38 #ifdef HAVE_SYS_STATFS_H
39 #include <sys/statfs.h>
40 #endif
41
42 /* freebsd && netbsd */
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
45 #endif
46 #ifdef HAVE_SYS_MOUNT_H
47 #include <sys/mount.h>
48 #endif
49
50 #define MAX_FS_STATS 64
51
52 static struct fs_stat fs_stats_[MAX_FS_STATS];
53 struct fs_stat *fs_stats = fs_stats_;
54
55 static void update_fs_stat(struct fs_stat* fs);
56
57 void update_fs_stats()
58 {
59         unsigned i;
60         for(i=0; i<MAX_FS_STATS; ++i)
61                 if(fs_stats[i].path)
62                         update_fs_stat(&fs_stats[i]);
63 }
64
65 void clear_fs_stats()
66 {
67         unsigned i;
68         for(i=0; i<MAX_FS_STATS; ++i)
69                 if(fs_stats[i].path) {
70                         free(fs_stats[i].path);
71                         fs_stats[i].path = 0;
72                 }
73 }
74
75 struct fs_stat *prepare_fs_stat(const char *s)
76 {
77         struct fs_stat* new = 0;
78         unsigned i;
79         /* lookup existing or get new */
80         for(i=0; i<MAX_FS_STATS; ++i) {
81                 if(fs_stats[i].path) {
82                         if(strcmp(fs_stats[i].path, s) == 0)
83                                 return &fs_stats[i];
84                 } else
85                         new = &fs_stats[i];
86         }
87         /* new path */
88         if(!new) {
89                 ERR("too many fs stats");
90                 return 0;
91         }
92         new->path = strdup(s);
93         update_fs_stat(new);
94         return new;
95 }
96
97 static
98 void update_fs_stat(struct fs_stat* fs)
99 {
100         struct statfs s;
101         if(statfs(fs->path, &s) == 0) {
102                 fs->size = (long long) s.f_blocks * s.f_bsize;
103                 /* bfree (root) or bavail (non-roots) ? */
104                 fs->avail = (long long) s.f_bavail* s.f_bsize;
105                 fs->free = (long long) s.f_bfree * s.f_bsize;;
106         } else {
107                 fs->size = 0;
108                 fs->avail = 0;
109                 fs->free = 0;
110                 ERR("statfs '%s': %s", fs->path, strerror(errno));
111         }
112 }