-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.h
307 lines (245 loc) · 6.2 KB
/
util.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#ifndef _CP_UTIL_H
#define _CP_UTIL_H
#include "common.h"
#include "config.h"
#ifdef CP_HAS_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
__BEGIN_DECLS
/**
* assorted goodies
*/
#ifndef CP_HAS_STRLCAT
#define strlcat(d,s,n) strncat(d,s,(n-1))
#endif /* CP_HAS_STRLCAT */
#ifndef CP_HAS_STRLCPY
#define strlcpy strncpy
#endif /* CP_HAS_STRLCPY */
#ifndef CP_HAS_SNPRINTF
#ifdef _WINDOWS
#define snprintf _snprintf
#define CP_HAS_SNPRINTF
#endif /* _WINDOWS */
#endif /* CP_HAS_SNPRINTF */
#ifndef CP_HAS_STRDUP
/**
* Copies cp_string into newly allocated memory.
*
* @retval pointer to copied cp_string
* @retval NULL if src was NULL
*/
CPROPS_DLL
char *strdup(char *src);
#endif /* missing strdup */
#ifndef CP_HAS_STRNDUP
/**
* Copies up to maxlen characters of src cp_string into newly allocated memory.
*
* @retval pointer to copied cp_string
* @retval NULL if src was NULL
*/
CPROPS_DLL
char *strndup(char *src, int maxlen);
#endif /* missing strndup */
#ifndef CP_HAS_STRCASECMP
#ifdef _WINDOWS
#define strcasecmp _stricmp
#else
int strcasecmp(const char *a, const char *b);
#endif
#endif /* CP_HAS_STRNCASECMP */
#ifndef CP_HAS_STRNCASECMP
#ifdef _WINDOWS
#define strncasecmp _strnicmp
#else
int strncasecmp(const char *a, const char *b, size_t len);
#endif
#endif /* CP_HAS_STRNCASECMP */
#ifndef CP_HAS_GETTIMEOFDAY
#ifdef _WINDOWS
CPROPS_DLL
int gettimeofday(struct timeval *res, struct timezone *tz);
#endif
#endif /* missing gettimeofday */
/* cp_sleep - a platform independent function to sleep n seconds */
CPROPS_DLL
int cp_sleep(int sec);
/**
* @todo check for buffer overflow
*/
CPROPS_DLL
char *str_trim_cpy(char *dst, char *src);
/**
* scans a cp_string for the first occurence of ch within the first len bytes at
* most. searching for the null character will return a pointer to the
* terminating null.
*/
CPROPS_DLL
char *strnchr(char *str, char ch, int len);
/**
* Map character 't', 'T' to true.
*/
CPROPS_DLL
int parse_boolean(char *value);
/**
* Map integer value to characters 't', 'f'.
*/
#define format_boolean(val) ((val) ? "t" : "f")
/**
* Get the current time as long value (UNIX).
*/
CPROPS_DLL
long get_timestamp();
/**
* Return the cp_string or if NULL an empty cp_string.
*/
CPROPS_DLL
char *dbfmt(char *str);
/**
* Retrieves the ip-adress of the system where it is running on.
*/
CPROPS_DLL
unsigned long get_current_ip();
/**
* Retrieve the ip-address of the system with the 'hostname'.
*/
CPROPS_DLL
unsigned long get_host_ip(char *hostname);
/**
* Formats an ip-address (IPv4) with dot notation.
*/
CPROPS_DLL
char *ip_to_string(unsigned long ip, char *buf, size_t len);
/**
* converts hex to url encoded binary (eg ABCD => %AB%CD) and returns a newly
* allocated cp_string
*/
CPROPS_DLL
char *hex_url_encode(char *hex);
/**
* return a static string coresponding to the posix regcomp error code given
* as a parameter. the error descriptions come from the gnu man page.
*/
CPROPS_DLL
char *regex_compilation_error(int rc);
/**
* return a static string describing the stat return code given as a parameter.
* the resulting error string contains 2 '%s' sequences - the first one could
* be used for the program name, function description etc, the second one
* for the stat()ed path.
*/
CPROPS_DLL
char *stat_error_fmt(int err);
/**
* check if the directory specified by path exists
*/
CPROPS_DLL
int checkdir(char *path);
/**
* generates a 16 character id based on the current time. The generated id
* followed by a terminating null character are written into buf.
*/
CPROPS_DLL
void gen_id_str(char *buf);
/**
* generates a 16 character id based on the given time. The generated id
* followed by a terminating null character are written into buf.
*/
CPROPS_DLL
void gen_tm_id_str(char *buf, struct timeval *tm);
/**
* get a timestamp for the last modification to the file designated by path
*/
CPROPS_DLL
time_t last_change_time(char *path);
/** duplicate an integer */
CPROPS_DLL
int *intdup(int *src);
/** duplicate a long integer */
CPROPS_DLL
long *longdup(long *src);
/** duplicate a floating point value */
CPROPS_DLL
float *floatdup(float *src);
/** duplicate a double precision floating point value */
CPROPS_DLL
double *doubledup(double *src);
/** duplicate an array */
CPROPS_DLL
void *memdup(void *src, int len);
/** convert a hex string to an integer value */
CPROPS_DLL
int xtoi(char *p);
/** convert a hex string to a long value */
CPROPS_DLL
int xtol(char *p);
/** return flipped string in a newly allocated buffer */
CPROPS_DLL
char *reverse_string(char *str);
/** flip a string in place */
CPROPS_DLL
char *reverse_string_in_place(char *str);
/** remove all occurrences of letters from str */
CPROPS_DLL
char *filter_string(char *str, char *letters);
/** convert string to lower case characters */
CPROPS_DLL
char *to_lowercase(char *str);
/** convert string to upper case characters */
CPROPS_DLL
char *to_uppercase(char *str);
#ifndef CP_HAS_GETOPT
extern CPROPS_DLL char *optarg;
CPROPS_DLL
int getopt(int argc, char *argv[], char *fmt);
#endif /* no getopt */
#ifndef CP_HAS_INET_NTOP
#ifdef _WINDOWS
CPROPS_DLL
char *inet_ntop(int af, const void *src, char *dst, size_t cnt);
#endif /* _WINDOWS */
#endif /* CP_HAS_INET_NTOP */
#ifndef CP_HAS_DLFCN_H
#ifdef _WINDOWS
CPROPS_DLL
void *dlopen(char *file, int mode);
CPROPS_DLL
int dlclose(void *handle);
CPROPS_DLL
void *dlsym(void *handle, char *name);
CPROPS_DLL
char *dlerror();
/* none if this is actually supported by the win32 api, just define the symbols
* so the build doesn't break.
*/
#define RTLD_LAZY 0
#define RTLD_NOW 0
#define RTLD_LOCAL 0
#define RTLD_GLOBAL 0
#endif /* WINDOWS */
#endif /* CP_HAS_DLFCN_H */
#ifndef CP_HAS_GETCWD
#ifdef _WINDOWS
#include "direct.h"
#define getcwd _getcwd
#endif /* _WINDOWS */
#endif /* CP_HAS_GETCWD */
#ifdef _WINDOWS
/* convenience function to create a process under windows */
CPROPS_DLL
int create_proc(char *path,
void *child_stdin,
void *child_stdout,
void *child_stderr,
char **envp);
#endif /* _WINDOWS */
CPROPS_DLL
void replace_char(char *buf, char from, char to);
CPROPS_DLL
char *ssl_err_inf(int err);
/* ----------------------------------------------------------------- */
__END_DECLS
/** @} */
#endif