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
23 * Prototypes for glibc-specific functions.
27 #define _BHC_COMPAT_H 1 /**< compat.h multi-inclusion guard. */
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
50 * However, there is no C99 check in configure at the moment, so this
51 * macro is a no-op for now.
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
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
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
105 * @param optionp is a pointer to the string to process.
106 * @param tokens references an array of strings containing the known
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
,
123 #ifndef HAVE_SOCKLEN_T
124 #define socklen_t int
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];
140 #ifndef AI_NUMERICHOST
141 #define AI_NUMERICHOST 0 /* If it is not defined - it is not
142 supported, so define it to zero. */
145 #if !defined(HAVE_BASENAME) || defined (_AIX)
146 char *basename (const char *path
);
149 #if !defined(HAVE_DIRNAME) || defined (_AIX)
150 char *dirname (char *path
);
153 #ifndef HAVE_VASPRINTF
154 int vasprintf (char **ptr
, const char *fmt
, va_list a
);
157 #ifndef HAVE_ASPRINTF
158 int asprintf (char **ptr
, const char *fmt
, ...);
161 #ifndef HAVE_VSNPRINTF
162 int vsnprintf (char *str
, size_t size
, const char *format
, va_list ap
);
166 int daemon (int nochdir
, int noclose
);
171 size_t confstr (int name
, char *buf
, size_t len
);
175 void *mempcpy (void *TO
, const void *FROM
, size_t SIZE
);
177 #ifndef HAVE_CANONICALIZE_FILE_NAME
178 char *canonicalize_file_name (const char *fn
);
181 char *strndup (const char *s
, size_t size
);
183 #ifndef HAVE_STRPTIME
184 char *strptime (const char *s
, const char *format
, struct tm
*tm
);
188 ssize_t
getline(char **lineptr
, size_t *n
, FILE *stream
);
190 #ifndef HAVE_GETDELIM
192 getdelim (char **lineptr
, size_t *n
, int delim
, FILE *stream
);
195 #ifndef HAVE_ARGP_PARSE
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
217 const struct argp_option
*options
;
218 argp_parser_t parser
;
219 const char *args_doc
;
221 /* The rest is ignored */
222 const void *children
;
223 char *(*help_filter
) (int key
, const char *text
, void *input
);
229 /* This is deliberately not compatible with glibc's one. We only use
230 ->input anyway. And argv[0]... */
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
, ...);
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
**));
251 #ifndef HAVE_ALPHASORT
252 int alphasort (const struct dirent
**a
, const struct dirent
**b
);
255 int setenv(const char *name
, const char *value
, int overwrite
);
259 void vsyslog(int priority
, const char *format
, va_list ap
);
262 #if !defined(va_copy) && !defined(__va_copy)
263 #define __va_copy(dst,src) memcpy (&dst, &src, sizeof (va_list))
266 #if !defined(va_copy)
267 #define va_copy __va_copy
271 char *strndup (const char *s
, size_t size
);
275 int strncmp (const char *s1
, const char *s2
, size_t n
);
276 int strcasecmp (const char *s1
, const char *s2
);
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. */
284 #ifndef BHC_OPTION_DEBUG
285 #define BHC_OPTION_DEBUG 0
288 #if BHC_OPTION_NODEBUG
289 #undef BHC_OPTION_DEBUG
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__)
299 # define bhc_debug(fmt,...)
302 # define bhc_error(fmt,...)
303 # define bhc_log(fmt,...)
304 # define bhc_debug(fmt,...)
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)
317 # define bhc_debug(format,args...)
320 # define bhc_error(format,args...)
321 # define bhc_log(format,args...)
322 # define bhc_debug(format,args...)
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
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,...)
350 #endif /* !_BHC_COMPAT_H */
352 /* arch-tag: c8c8c7bc-b88b-4e0a-9c18-51f0fd3c8ba4 */