Code

control: Updated standards-version to 3.9.5 -- no changes.
[pkg-rrdtool.git] / src / rrd_utils.c
1 /**
2  * RRDtool - src/rrd_utils.c
3  * Copyright (C) 2009 Kevin Brintnall
4  * Copyright (C) 2008 Sebastian Harl
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   kevin brintnall <kbrint@rufus.net>
21  *   Sebastian Harl <sh@tokkee.org>
22  **/
24 #include "rrd_tool.h"
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #ifndef _MSC_VER
33 #include <libgen.h>
34 #include <unistd.h>
35 #endif
36 #ifdef WIN32
37 #       define random() rand()
38 #       define srandom(x) srand(x)
39 #       define getpid() 0
40 #endif /* WIN32 */
42 #ifndef S_ISDIR
43 #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
44 #endif
46 /* make sure that the random number generator seeded EXACTLY ONCE */
47 long rrd_random(void)
48 {
49     static int rand_init = 0;
50     if (!rand_init) {
51         srandom((unsigned int) time(NULL) + (unsigned int) getpid());
52         rand_init++;
53     }
55     return random();
56 }
58 /* rrd_add_ptr: add a pointer to a dynamically sized array of pointers,
59  * realloc as necessary.  returns 1 on success, 0 on failure.
60  */
62 int rrd_add_ptr(void ***dest, size_t *dest_size, void *src)
63 {
64     void **temp;
66     assert(dest != NULL);
68     temp = (void **) rrd_realloc(*dest, (*dest_size+1) * sizeof(*dest));
69     if (!temp)
70         return 0;
72     *dest = temp;
73     temp[*dest_size] = src;
74     (*dest_size)++;
76     return 1;
77 }
79 /* like rrd_add_ptr, but calls strdup() on a string first. */
80 int rrd_add_strdup(char ***dest, size_t *dest_size, char *src)
81 {
82     char *dup_src;
83     int add_ok;
85     assert(dest != NULL);
86     assert(src  != NULL);
88     dup_src = strdup(src);
89     if (!dup_src)
90         return 0;
92     add_ok = rrd_add_ptr((void ***)dest, dest_size, (void *)dup_src);
93     if (!add_ok)
94         free(dup_src);
96     return add_ok;
97 }
99 void rrd_free_ptrs(void ***src, size_t *cnt)
101     void **sp;
103     assert(src != NULL);
104     sp = *src;
106     if (sp == NULL)
107         return;
109     while (*cnt > 0) {
110         (*cnt)--;
111         free(sp[*cnt]);
112     }
114     free (sp);
115     *src = NULL;
118 /* recursively create the directory named by 'pathname'
119  * (similar to "mkdir -p" on the command line) */
120 int rrd_mkdir_p(const char *pathname, mode_t mode)
122     struct stat sb;
124     char *pathname_copy;
125     char *base_dir;
127     if ((NULL == pathname) || ('\0' == *pathname)) {
128         errno = EINVAL;
129         return -1;
130     }
132     if (0 == stat(pathname, &sb)) {
133         if (! S_ISDIR(sb.st_mode)) {
134             errno = ENOTDIR;
135             return -1;
136         }
137         return 0;
138     }
140     /* keep errno as set by stat() */
141     if (ENOENT != errno)
142         return -1;
144     /* dirname might modify its first argument */
145     if (NULL == (pathname_copy = strdup(pathname)))
146         return -1;
148 #ifndef _MSC_VER
149     /* the data pointedd too by dirname might change too (bsd) */
150     if (NULL == (base_dir = strdup(dirname(pathname_copy)))) {
151         free(pathname_copy);
152         return -1;
153     }
154 #else
155     _splitpath(pathname_copy, NULL, base_dir, NULL, NULL);
156 #endif
158     if (0 != rrd_mkdir_p(base_dir, mode)) {
159         int orig_errno = errno;
160         free(pathname_copy);
161 #ifndef _MSC_VER
162         free(base_dir);
163 #endif
164         errno = orig_errno;
165         return -1;
166     }
168     free(pathname_copy);
169 #ifndef _MSC_VER
170     free(base_dir);
171 #endif
173     /* keep errno as set by mkdir() */
174 #ifdef _MSC_VER
175     if (0 != mkdir(pathname))
176         return -1;
177 #else
178     if (0 != mkdir(pathname, mode))
179         return -1;
180 #endif
181     return 0;
182 } /* rrd_mkdir_p */