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