1 /**
2 * \file popt/system.h
3 */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8
9 #include <ctype.h>
10
11 /* XXX isspace(3) has i18n encoding signednesss issues on Solaris. */
12 #define _isspaceptr(_chp) isspace((int)(*(unsigned char *)(_chp)))
13
14 #ifdef HAVE_MCHECK_H
15 #include <mcheck.h>
16 #endif
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 void * xmalloc (size_t size);
22
23 void * xcalloc (size_t nmemb, size_t size);
24
25 void * xrealloc (void * ptr, size_t size);
26
27 char * xstrdup (const char *str);
28
29 #if !defined(HAVE_STPCPY)
30 /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */
stpcpy(char * dest,const char * src)31 static inline char * stpcpy (char *dest, const char * src) {
32 register char *d = dest;
33 register const char *s = src;
34
35 do
36 *d++ = *s;
37 while (*s++ != '\0');
38 return d - 1;
39 }
40 #endif
41
42 /* Memory allocation via macro defs to get meaningful locations from mtrace() */
43 #if defined(HAVE_MCHECK_H) && defined(__GNUC__)
44 #define vmefail() (fprintf(stderr, "virtual memory exhausted.\n"), exit(EXIT_FAILURE), NULL)
45 #define xmalloc(_size) (malloc(_size) ? : vmefail())
46 #define xcalloc(_nmemb, _size) (calloc((_nmemb), (_size)) ? : vmefail())
47 #define xrealloc(_ptr, _size) (realloc((_ptr), (_size)) ? : vmefail())
48 #define xstrdup(_str) (strcpy((malloc(strlen(_str)+1) ? : vmefail()), (_str)))
49 #else
50 #define xmalloc(_size) malloc(_size)
51 #define xcalloc(_nmemb, _size) calloc((_nmemb), (_size))
52 #define xrealloc(_ptr, _size) realloc((_ptr), (_size))
53 #define xstrdup(_str) strdup(_str)
54 #endif /* defined(HAVE_MCHECK_H) && defined(__GNUC__) */
55
56 #if defined(HAVE_SECURE_GETENV)
57 #define getenv(_s) secure_getenv(_s)
58 #elif defined(HAVE___SECURE_GETENV)
59 #define getenv(_s) __secure_getenv(_s)
60 #endif
61
62 #if !defined(__GNUC__) && !defined(__attribute__)
63 #define __attribute__(x)
64 #endif
65 #define UNUSED(x) x __attribute__((__unused__))
66
67 #include "popt.h"
68