00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #ifndef _GU_H
00040 #define _GU_H 1
00041
00042 #include <stdio.h>
00043 #include <sys/types.h>
00044 #include <sys/stat.h>
00045 #include <stdarg.h>
00046 #include <setjmp.h>
00047
00048
00049
00050
00051
00052
00053 typedef int gu_boolean;
00054
00055
00056 #ifndef FALSE
00057 #define FALSE 0
00058 #endif
00059 #ifndef TRUE
00060 #define TRUE !FALSE
00061 #endif
00062
00063
00064
00065
00066
00067
00068 #define UNIX_755 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
00069 #define UNIX_644 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
00070 #define UNIX_660 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
00071 #define UNIX_640 (S_IRUSR | S_IWUSR | S_IRGRP)
00072 #define UNIX_600 (S_IRUSR | S_IWUSR)
00073 #define UNIX_022 (S_IWGRP | S_IWOTH)
00074 #define UNIX_002 S_IWOTH
00075 #define UNIX_077 (S_IRWXG | S_IRWXO)
00076
00077
00078
00079
00080
00082 #define lmatch(a, b) (!strncmp(a, b, sizeof(b) - 1))
00083
00085 #define lmatchp(a, b) (!strncmp(a, b, sizeof(b) - 1) ? a + sizeof(b) - 1 + strspn(a + sizeof(b) - 1, " \t") : NULL)
00086
00088 #define rmatch(a, b) (strlen(a) >= strlen(b) && strcmp(a + strlen(a) - strlen(b), b) == 0)
00089
00092 #define lmatchsp(a, b) (!strncmp(a, b, sizeof(b) - 1) && isspace(a[sizeof(b) - 1]) ? a + sizeof(b) - 1 + strspn(a + sizeof(b) - 1, " \t") : NULL)
00093
00095 #define gu_write_string(fd, s) (write(fd, s, sizeof(s) - 1))
00096
00097
00098
00099
00100
00101
00102 void *gu_alloc(size_t number, size_t size);
00103 void *gu_realloc(void *ptr, size_t number, size_t size);
00104 char *gu_strdup(const char *string);
00105 char *gu_strndup(const char *string, size_t len);
00106 char *gu_restrdup(char *ptr, size_t *number, const char *string);
00107 void gu_free(void *ptr);
00108
00109
00110 void gu_alloc_checkpoint(void);
00111 int gu_alloc_checkpoint_get(void);
00112 void gu_alloc_checkpoint_put(int n);
00113 void _gu_alloc_assert(const char *file, int line, int assertion);
00114 #define gu_alloc_assert(assertion) _gu_alloc_assert(__FILE__, __LINE__, assertion)
00115
00116
00117
00118
00119
00120
00121 struct GU_INI_ENTRY
00122 {
00123 char *name;
00124 const char *values;
00125 int nvalues;
00126 } ;
00127
00128
00129 enum GU_INI_TYPES {
00130 GU_INI_TYPE_SKIP,
00131 GU_INI_TYPE_NONNEG_INT,
00132 GU_INI_TYPE_STRING,
00133 GU_INI_TYPE_NONEMPTY_STRING,
00134 GU_INI_TYPE_POSITIVE_DOUBLE,
00135 GU_INI_TYPE_NONNEG_DOUBLE,
00136 GU_INI_TYPE_END
00137 } ;
00138
00139 struct GU_INI_ENTRY *gu_ini_section_load(FILE *file, const char section_name[]);
00140 const struct GU_INI_ENTRY *gu_ini_section_get_value(const struct GU_INI_ENTRY *section, const char key_name[]);
00141 void gu_ini_section_free(struct GU_INI_ENTRY *section);
00142 const char *gu_ini_value_index(const struct GU_INI_ENTRY *array, int array_index, const char *default_value);
00143 int gu_ini_assign(const struct GU_INI_ENTRY *array, ...);
00144 int gu_ini_vassign(const struct GU_INI_ENTRY *array, va_list args);
00145 const char *gu_ini_scan_list(const char file_name[], const char section_name[], const char key_name[], ...);
00146 char *gu_ini_query(const char file_name[], const char section_name[], const char key_name[], int index, const char default_value[]);
00147 int gu_ini_section_from_sample(const char filename[], const char section_name[]);
00148
00149
00150
00151
00152
00153 char *gu_getline(char *line, int *space_available, FILE *fstream);
00154 char *gu_strerror(int n);
00155 int gu_wildmat(const char *text, const char *p);
00156 int compile_string_escapes(char *s);
00157 char *gu_strsep(char **stringp, const char *delim);
00158 char *gu_strsep_quoted(char **stringp, const char *delim, const char *discard);
00159 void gu_set_cloexec(int fd);
00160 void gu_nonblock(int fd, gu_boolean on);
00161 void gu_trim_whitespace_right(char *s);
00162 int gu_strcasecmp(const char *s1, const char *s2);
00163 int gu_strncasecmp(const char *s1, const char *s2, int n);
00164 int gu_lock_exclusive(int filenum, int waitmode);
00165 int gu_torf(const char *s);
00166 int gu_torf_setBOOL(gu_boolean *b, const char *s);
00167 double gu_getdouble(const char *);
00168 const char *gu_dtostr(double);
00169 void ASCIIZ_to_padded(char *padded, const char *asciiz, int len);
00170 void padded_to_ASCIIZ(char *asciiz, const char *padded, int len);
00171 gu_boolean padded_cmp(const char *padded1, const char *padded2, int len);
00172 gu_boolean padded_icmp(const char *padded1, const char *padded2, int len);
00173 int gu_sscanf(const char *input, const char *pattern, ...);
00174 void gu_sscanf_checkpoint(void);
00175 void gu_sscanf_rollback(void);
00176 int gu_fscanf(FILE *input, const char *format, ...);
00177 void gu_daemon(mode_t daemon_umask);
00178 int disk_space(const char *path, unsigned int *free_blocks, unsigned int *free_files);
00179 void gu_wordwrap(char *string, int width);
00180 void (*signal_interupting(int signum, void (*handler)(int sig)))(int);
00181 void (*signal_restarting(int signum, void (*handler)(int sig)))(int);
00182 int gu_vsnprintf (char *str, size_t count, const char *fmt, va_list args);
00183 int gu_snprintf(char *str, size_t count, const char *fmt, ...);
00184 int gu_vasprintf(char **ptr, const char *format, va_list ap);
00185 int gu_asprintf(char **ptr, const char *format, ...);
00186 size_t gu_strlcpy(char *dst, const char *src, size_t siz);
00187 size_t gu_strlcat(char *dst, const char *src, size_t siz);
00188 int gu_mkstemp(char *template);
00189 char *gu_strsignal(int signum);
00190 int gu_snprintfcat(char *buffer, size_t max, const char *format, ...);
00191 int gu_timeval_cmp(const struct timeval *t1, const struct timeval *t2);
00192 void gu_timeval_sub(struct timeval *t1, const struct timeval *t2);
00193 void gu_timeval_add(struct timeval *t1, const struct timeval *t2);
00194 void gu_timeval_cpy(struct timeval *t1, const struct timeval *t2);
00195 void gu_timeval_zero(struct timeval *t);
00196 int gu_runl(const char *myname, FILE *errors, const char *progname, ...);
00197
00198
00199
00200
00201
00202
00203
00204
00205 #define ANSWER int
00206 #define ANSWER_UNKNOWN -1
00207 #define ANSWER_FALSE 0
00208 #define ANSWER_TRUE 1
00209
00210
00211
00212
00213
00214 struct gu_getopt_opt
00215 {
00216 const char *name;
00217 int code;
00218 gu_boolean needsarg;
00219 } ;
00220
00221 struct gu_getopt_state
00222 {
00223 int argc;
00224 char **argv;
00225 const char *opt_chars;
00226 const struct gu_getopt_opt *opt_words;
00227 int optind;
00228 char *optarg;
00229 const char *name;
00230 int x;
00231 int len;
00232 char scratch[3];
00233 char *putback;
00234 } ;
00235
00236 void gu_getopt_init(struct gu_getopt_state *state, int argc, char **argv, const char *opt_chars, const struct gu_getopt_opt *opt_words);
00237 int ppr_getopt(struct gu_getopt_state *state);
00238 void gu_getopt_default(const char myname[], int optchar, const struct gu_getopt_state *getopt_state, FILE *errors);
00239
00240
00241
00242
00243
00244 struct OPTIONS_STATE {
00245 int magic;
00246
00247 const char *options;
00248
00249 int index;
00250 int index_of_name;
00251 int index_of_prev_name;
00252
00253 const char *error;
00254
00255 int next_time_skip;
00256 };
00257
00258 void options_start(const char *options_str, struct OPTIONS_STATE *o);
00259 int options_get_one(struct OPTIONS_STATE *o, char *name, int maxnamelen, char *value, int maxvaluelen);
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 extern char gu_exception[];
00283 extern int gu_exception_code;
00284 extern int gu_exception_try_depth;
00285 extern int gu_exception_temp;
00286 extern int gu_exception_debug;
00287
00295 #define gu_Try { \
00296 jmp_buf gu_exception_jmp_buf; \
00297 int gu_exception_setjmp_retcode; \
00298 int gu_exception_pop = 1; \
00299 gu_Try_funct(&gu_exception_jmp_buf); \
00300 if((gu_exception_setjmp_retcode = setjmp(gu_exception_jmp_buf)) == 0)
00301
00302 void gu_Try_funct(jmp_buf *p_jmp_buf);
00303
00304
00305
00306
00307 void gu_Throw(const char message[], ...)
00308 #ifdef __GNUC__
00309 __attribute__ (( noreturn, format (printf, 1, 2) ))
00310 #endif
00311 ;
00312 void gu_CodeThrow(int code, const char message[], ...)
00313 #ifdef __GNUC__
00314 __attribute__ (( noreturn, format (printf, 2, 3) ))
00315 #endif
00316 ;
00317
00318
00319
00320
00321 void gu_ReThrow(void)
00322 #ifdef __GNUC__
00323 __attribute__ (( noreturn ))
00324 #endif
00325 ;
00326
00341 #define gu_Final \
00342 gu_exception_try_depth--; \
00343 gu_exception_pop = 0; \
00344 if(1)
00345
00352 #define gu_Catch \
00353 gu_exception_try_depth -= gu_exception_pop; \
00354 gu_exception_temp = gu_exception_setjmp_retcode; \
00355 } \
00356 if(gu_exception_temp != 0)
00357
00358
00359
00360
00361
00363 struct gu_snmp
00364 {
00365 int socket;
00366 const char *community;
00367 unsigned int request_id;
00368 char result[1024];
00369 int result_len;
00370 };
00371
00372 struct gu_snmp *gu_snmp_open(unsigned long int ip_address, const char community[]);
00373 void gu_snmp_close(struct gu_snmp *p);
00374 void gu_snmp_get(struct gu_snmp *p, ...);
00375
00376 #define GU_SNMP_INT 1
00377 #define GU_SNMP_STR 2
00378 #define GU_SNMP_BIT 3
00380
00381
00382
00383
00384
00385 void *gu_pcs_new(void);
00386 void *gu_pcs_new_pcs(void **pcs);
00387 void *gu_pcs_new_cstr(const char cstr[]);
00388 void gu_pcs_free(void **pcs);
00389 char *gu_pcs_free_keep_cstr(void **pcs);
00390 void gu_pcs_debug(void **pcs, const char name[]);
00391 void *gu_pcs_snapshot(void **pcs);
00392 void gu_pcs_grow(void **pcs, int size);
00393 void gu_pcs_set_cstr(void **pcs, const char cstr[]);
00394 void gu_pcs_set_pcs(void **pcs, void **pcs2);
00395 const char *gu_pcs_get_cstr(void **pcs);
00396 char *gu_pcs_get_editable_cstr(void **pcs);
00397 int gu_pcs_length(void **pcs);
00398 int gu_pcs_truncate(void **pcs, size_t newlen);
00399 void gu_pcs_append_char(void **pcs, int c);
00400 void gu_pcs_append_cstr(void **pcs, const char cstr[]);
00401 void gu_pcs_append_pcs(void **pcs, void **pcs2);
00402 int gu_pcs_cmp(void **pcs1, void **pcs2);
00403 int gu_pcs_hash(void **pcs_key);
00404
00405
00406
00407
00408
00409
00410 #ifndef HAVE_STRSIGNAL
00411 #define strsignal(signum) gu_strsignal(signum)
00412 #endif
00413 #ifndef HAVE_SNPRINTF
00414 #define snprintf gu_snprintf
00415 #endif
00416 #ifndef HAVE_VSNPRINTF
00417 #define vsnprintf(s, n, format, ap) gu_vsnprintf(s, n, format, ap)
00418 #endif
00419 #ifndef HAVE_MKSTEMP
00420 #define mkstemp(template) gu_mkstemp(template)
00421 #endif
00422 #ifndef HAVE_STRLCPY
00423 #define strlcpy(a,b,c) gu_strlcpy(a,b,c)
00424 #endif
00425 #ifndef HAVE_STRLCAT
00426 #define strlcat(a,b,c) gu_strlcat(a,b,c)
00427 #endif
00428 #if 1
00429 #define strerror(err) gu_strerror(err)
00430 #endif
00431
00432 #endif
00433
00434