2 /* Copyright Gerhard Rieger 2001-2008 */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* useful additions to C library */
9 #include "sysincludes.h"
11 #include "compat.h" /* socklen_t */
18 /* GNU extension, available since glibc 2.1.91 */
19 void *memrchr(const void *s
, int c
, size_t n
) {
20 const unsigned char *t
= ((unsigned char *)s
)+n
;
21 while (--t
>= (unsigned char *)s
) {
24 if (t
< (unsigned char *)s
)
28 #endif /* !HAVE_MEMRCHR */
30 void *memdup(const void *src
, size_t n
) {
33 if ((dest
= Malloc(n
)) == NULL
) {
41 /* search the keyword-table for a match of the leading part of name. */
42 /* returns the pointer to the matching field of the keyword or NULL if no
44 const struct wordent
*keyw(const struct wordent
*keywds
, const char *name
, unsigned int nkeys
) {
45 unsigned int lower
, upper
, mid
;
51 while (upper
- lower
> 1)
53 mid
= (upper
+ lower
) >> 1;
54 if (!(r
= strcasecmp(keywds
[mid
].name
, name
)))
63 if (nkeys
> 0 && !(strcasecmp(keywds
[lower
].name
, name
)))
65 return &keywds
[lower
];
70 /* Linux: setenv(), AIX: putenv() */
72 int setenv(const char *name
, const char *value
, int overwrite
) {
76 if (getenv(name
)) return 0; /* already exists */
78 if ((env
= Malloc(strlen(name
)+strlen(value
)+2)) != NULL
) {
81 sprintf(env
, "%s=%s", name
, value
);
82 if ((result
= putenv(env
)) != 0) { /* AIX docu says "... nonzero ..." */
86 /* linux "man putenv" says: ...this string becomes part of the environment*/
89 #endif /* !HAVE_SETENV */
93 /* sanitize an "untrusted" character. output buffer must provide at least 5
95 Does not append null. returns length out output (currently: max 4) */
96 static size_t sanitize_char(char c
, char *o
, int style
) {
97 int hn
; /* high nibble */
98 int ln
; /* low nibble */
99 int n
; /* written chars */
107 case '\0': *o
++ = '0'; break;
108 case '\a': *o
++ = 'a'; break;
109 case '\b': *o
++ = 'b'; break;
110 case '\t': *o
++ = 't'; break;
111 case '\n': *o
++ = 'n'; break;
112 case '\v': *o
++ = 'v'; break;
113 case '\f': *o
++ = 'f'; break;
114 case '\r': *o
++ = 'r'; break;
115 case '\'': *o
++ = '\''; break;
116 case '\"': *o
++ = '"'; break;
117 case '\\': *o
++ = '\\'; break;
122 *o
++ = (hn
>=10 ? (('A'-1)+(hn
-10)) : ('0'+hn
));
123 *o
++ = (ln
>=10 ? (('A'-1)+(ln
-10)) : ('0'+ln
));
129 /* sanitize "untrusted" text, replacing special control characters with the C
130 string version ("\x"), and replacing unprintable chars with ".".
131 text can grow to four times of input, so keep output buffer long enough!
132 returns a pointer to the first untouched byte of the output buffer.
134 char *sanitize_string(const char *data
, /* input data */
135 size_t bytes
, /* length of input data, >=0 */
136 char *coded
, /* output buffer, must be long enough */
142 c
= *(unsigned char *)data
++;
143 coded
+= sanitize_char(c
, coded
, style
);
149 /* copies a substring out of a given buff
150 returns scratch, \0 terminated; scratch must provide len+1 bytes
152 char *xiosubstr(char *scratch
, const char *str
, size_t from
, size_t len
) {
153 char *scratch0
= scratch
;