several updated in configure.
[crywrap.git] / lib / compat / compat.h
blob23de9fe4940df281c659fbf34f89b83751dbe12a
1 /* -*- mode: c; c-file-style: "gnu" -*-
2 * compat.h -- Prototypes for glibc-specific functions
3 * Copyright (C) 2002, 2003 Gergely Nagy <algernon@bonehunter.rulez.org>
5 * This file is part of The BoneHunter compat suite.
7 * The BoneHunter compat suite is free software; you can redistribute
8 * it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * The BoneHunter compat suite is distributed in the hope that it will
13 * be useful, but WITHOUT ANY WARRANTY; without even the implied
14 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /** @file compat.h
23 * Prototypes for glibc-specific functions.
26 #ifndef _BHC_COMPAT_H
27 #define _BHC_COMPAT_H 1 /**< compat.h multi-inclusion guard. */
29 #include "system.h"
31 #ifdef HAVE_STDINT_H
32 #include <stdint.h>
33 #endif
34 #include <dirent.h>
35 #include <errno.h>
36 #include <netdb.h>
37 #include <stdarg.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <time.h>
41 #include <syslog.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 /** C99-style enum initialiser wrapper.
46 * This macro is used to allow non-C99 compilers (hi, AIX!) to compile
47 * BoneHunter software, why allowing C99-enabled ones to... well, to
48 * do stuff.
50 * However, there is no C99 check in configure at the moment, so this
51 * macro is a no-op for now.
53 #define C99_INIT(a)
55 /** Convert a string to long, with error checking.
56 * @param str is the string to convert.
57 * @param result is where the result will go.
59 * @returns Zero on success, -1 otherwise. If there was an error,
60 * result will be left untouched.
62 int bhc_atoi (const char *str, long *result);
64 /** Get the current working directory.
65 * @returns A newly allocated string, containing the current working
66 * directory.
68 char *bhc_getcwd (void);
70 /** Initialiser for bhc_setproctitle().
71 * This saves the start and the end of argv, for later use by
72 * bhc_setproctitle(). Provided of course, that there is no native
73 * setproctitle().
75 * @param argc is the argument count.
76 * @param argv is the argument vector.
77 * @param envp is the environment vector.
79 void bhc_setproctitle_init (int argc, char **argv, char **envp);
81 /** Sets the process title.
82 * Change the name of the process - as appears in ps(1).
84 * @param name is the new title of the process.
86 void bhc_setproctitle (const char *name);
88 /** Convenience wrapper around bhc_realloc().
90 #define XSREALLOC(ptr, type, nmemb) \
91 ptr = (type*) bhc_realloc (ptr, (nmemb) * sizeof (type))
93 void *bhc_malloc (size_t size);
94 void *bhc_realloc (void *ptr, size_t size);
95 void *bhc_calloc (size_t nmemb, size_t size);
96 char *bhc_strdup (const char *src);
97 char *bhc_strndup (const char *src, size_t n);
99 void bhc_exit (int code);
101 /** getsubopt() replacement.
102 * This function is either a wrapper around getsubopt() or a complete
103 * reimplementation.
105 * @param optionp is a pointer to the string to process.
106 * @param tokens references an array of strings containing the known
107 * suboptions.
108 * @param valuep will be used to return the value of the suboption.
110 * @returns Zero on success, -1 otherwise.
112 * @note Modifies #valuep in-place.
114 int bhc_getsubopt (char **optionp, char *const *tokens,
115 char **valuep);
117 #ifndef __DOXYGEN__
119 #ifndef SHUT_RDWR
120 #define SHUT_RDWR 2
121 #endif
123 #ifndef HAVE_SOCKLEN_T
124 #define socklen_t int
125 #endif
127 #ifndef HAVE_ERROR_T
128 #define error_t int
129 #endif
131 #ifndef HAVE_SOCKADDR_STORAGE
132 struct sockaddr_storage
134 uchar ss_len; /* address length */
135 uchar ss_family; /* address family */
136 unsigned char fill[126];
138 #endif
140 #ifndef AI_NUMERICHOST
141 #define AI_NUMERICHOST 0 /* If it is not defined - it is not
142 supported, so define it to zero. */
143 #endif
145 #if !defined(HAVE_BASENAME) || defined (_AIX)
146 char *basename (const char *path);
147 #endif
149 #if !defined(HAVE_DIRNAME) || defined (_AIX)
150 char *dirname (char *path);
151 #endif
153 #ifndef HAVE_VASPRINTF
154 int vasprintf (char **ptr, const char *fmt, va_list a);
155 #endif
157 #ifndef HAVE_ASPRINTF
158 int asprintf (char **ptr, const char *fmt, ...);
159 #endif
161 #ifndef HAVE_VSNPRINTF
162 int vsnprintf (char *str, size_t size, const char *format, va_list ap);
163 #endif
165 #ifndef HAVE_DAEMON
166 int daemon (int nochdir, int noclose);
167 #endif
169 #ifndef HAVE_CONFSTR
170 #define _CS_PATH 1
171 size_t confstr (int name, char *buf, size_t len);
172 #endif
174 #ifndef HAVE_MEMPCPY
175 void *mempcpy (void *TO, const void *FROM, size_t SIZE);
176 #endif
177 #ifndef HAVE_CANONICALIZE_FILE_NAME
178 char *canonicalize_file_name (const char *fn);
179 #endif
180 #ifndef HAVE_STRNDUP
181 char *strndup (const char *s, size_t size);
182 #endif
183 #ifndef HAVE_STRPTIME
184 char *strptime (const char *s, const char *format, struct tm *tm);
185 #endif
187 #ifndef HAVE_GETLINE
188 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
189 #endif
190 #ifndef HAVE_GETDELIM
191 ssize_t
192 getdelim (char **lineptr, size_t *n, int delim, FILE *stream);
193 #endif
195 #ifndef HAVE_ARGP_PARSE
196 struct argp_option
198 char *name;
199 int key;
200 const char *arg;
201 int flags;
202 char *doc;
203 int group;
205 struct argp;
206 struct argp_state;
207 typedef error_t (*argp_parser_t) (int key, char *arg,
208 struct argp_state *state);
209 #define ARGP_ERR_UNKNOWN E2BIG
210 #define ARGP_KEY_END 0x1000001
211 #define ARGP_KEY_ARG 0
213 #define OPTION_ARG_OPTIONAL 0x1
214 #define OPTION_HIDDEN 0x2
215 struct argp
217 const struct argp_option *options;
218 argp_parser_t parser;
219 const char *args_doc;
220 const char *doc;
221 /* The rest is ignored */
222 const void *children;
223 char *(*help_filter) (int key, const char *text, void *input);
224 const char *domain;
227 struct argp_state
229 /* This is deliberately not compatible with glibc's one. We only use
230 ->input anyway. And argv[0]... */
231 void *input;
232 char *argv0;
235 extern const char *argp_program_version;
236 extern const char *argp_program_bug_address;
237 extern void (*argp_program_version_hook) (FILE *stream,
238 struct argp_state *state);
240 error_t argp_parse (const struct argp *argps, int argc, char **argv,
241 unsigned flags, int *arg_index, void *input);
242 error_t argp_error (const struct argp_state *state, char *fmt, ...);
243 #endif
245 #ifndef HAVE_SCANDIR
246 int scandir (const char *dir, struct dirent ***namelist,
247 int (*sd_select)(const struct dirent *),
248 int (*sd_compar)(const struct dirent **,
249 const struct dirent **));
250 #endif
251 #ifndef HAVE_ALPHASORT
252 int alphasort (const struct dirent **a, const struct dirent **b);
253 #endif
254 #ifndef HAVE_SETENV
255 int setenv(const char *name, const char *value, int overwrite);
256 #endif
258 #ifndef HAVE_VSYSLOG
259 void vsyslog(int priority, const char *format, va_list ap);
260 #endif
262 #if !defined(va_copy) && !defined(__va_copy)
263 #define __va_copy(dst,src) memcpy (&dst, &src, sizeof (va_list))
264 #endif
266 #if !defined(va_copy)
267 #define va_copy __va_copy
268 #endif
270 #ifdef _AIX
271 char *strndup (const char *s, size_t size);
272 #endif
274 #ifdef __dietlibc__
275 int strncmp (const char *s1, const char *s2, size_t n);
276 int strcasecmp (const char *s1, const char *s2);
277 #endif
279 #endif /* !__DOXYGEN__ */
281 /* Note: The code below is ugly like hell. Do not even try to look at
282 it. You have been warned. */
283 #ifndef __DOXYGEN__
284 #ifndef BHC_OPTION_DEBUG
285 #define BHC_OPTION_DEBUG 0
286 #endif
288 #if BHC_OPTION_NODEBUG
289 #undef BHC_OPTION_DEBUG
290 #endif
292 #if HAVE___VA_ARGS__
293 # if BHC_OPTION_ALL_LOGGING
294 # define bhc_error(fmt,...) syslog (LOG_WARNING, fmt, __VA_ARGS__)
295 # define bhc_log(fmt,...) syslog (LOG_NOTICE, fmt, __VA_ARGS__)
296 # if BHC_OPTION_DEBUG
297 # define bhc_debug(fmt,...) syslog (LOG_DEBUG, fmt, __VA_ARGS__)
298 # else
299 # define bhc_debug(fmt,...)
300 # endif
301 # else
302 # define bhc_error(fmt,...)
303 # define bhc_log(fmt,...)
304 # define bhc_debug(fmt,...)
305 # endif
306 #else /* HAVE___VA_ARGS__ */
307 # ifdef HAVE_VARIADIC_MACROS
308 # if BHC_OPTION_ALL_LOGGING
309 # define bhc_error(format,args...) \
310 syslog (LOG_WARNING, format , ## args)
311 # define bhc_log(format,args...) \
312 syslog (LOG_NOTICE, format , ## args)
313 # if BHC_OPTION_DEBUG
314 # define bhc_debug(format,args...) \
315 syslog (LOG_DEBUG, format , ## args)
316 # else
317 # define bhc_debug(format,args...)
318 # endif
319 # else
320 # define bhc_error(format,args...)
321 # define bhc_log(format,args...)
322 # define bhc_debug(format,args...)
323 # endif
324 # else
325 void bhc_error (char *fmt, ...);
326 void bhc_log (char *fmt, ...);
327 void bhc_debug (char *fmt, ...);
328 # endif /* !HAVE_VARIADIC_MACROS */
329 #endif /* !HAVE___VA_ARGS__ */
330 #else /* __DOXYGEN__ */
331 /** Log an error condition.
332 * This is a wrapper around syslog(), with priority set to
333 * LOG_WARNING. After called, the caller should return an error
334 * condition.
336 #define bhc_error(fmt,...)
337 /** Log various stuff.
338 * This wrapper is around to log miscellaneous events. Such as served
339 * requests and startup information.
341 #define bhc_log(fmt,...)
342 /** Wrapper for logging debug information.
343 * Every function that wants to emit information only useful
344 * debugging, should use this function. It will not be compiled in if
345 * BHC_OPTION_DEBUG is unset.
347 #define bhc_debug(fmt,...)
348 #endif
350 #endif /* !_BHC_COMPAT_H */
352 /* arch-tag: c8c8c7bc-b88b-4e0a-9c18-51f0fd3c8ba4 */