| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "autoconfig.h" |
| 23 |
|
| 24 |
#include <sys/types.h> |
| 25 |
#include <ctype.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <stdarg.h> |
| 30 |
|
| 31 |
#ifdef HAVE_ALLOCA_H |
| 32 |
#include <alloca.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifndef HAVE_STRSEP |
| 36 |
char *strsep(char **str, const char *delims) |
| 37 |
{ |
| 38 |
char *token; |
| 39 |
|
| 40 |
if (!*str) { |
| 41 |
|
| 42 |
return NULL; |
| 43 |
} |
| 44 |
|
| 45 |
token = *str; |
| 46 |
while (**str != '\0') { |
| 47 |
if (strchr(delims, **str)) { |
| 48 |
**str = '\0'; |
| 49 |
(*str)++; |
| 50 |
return token; |
| 51 |
} |
| 52 |
(*str)++; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
*str = NULL; |
| 57 |
|
| 58 |
return token; |
| 59 |
} |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifndef HAVE_SETENV |
| 63 |
int setenv(const char *name, const char *value, int overwrite) |
| 64 |
{ |
| 65 |
unsigned char *buf; |
| 66 |
int buflen; |
| 67 |
|
| 68 |
buflen = strlen(name) + strlen(value) + 2; |
| 69 |
buf = alloca(buflen); |
| 70 |
|
| 71 |
if (!overwrite && getenv(name)) |
| 72 |
return 0; |
| 73 |
|
| 74 |
snprintf(buf, buflen, "%s=%s", name, value); |
| 75 |
|
| 76 |
return putenv(buf); |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
#ifndef HAVE_UNSETENV |
| 81 |
int unsetenv(const char *name) |
| 82 |
{ |
| 83 |
return setenv(name, "", 0); |
| 84 |
} |
| 85 |
#endif |
| 86 |
|
| 87 |
#ifndef HAVE_STRCASESTR |
| 88 |
static char *upper(const char *orig, char *buf, int bufsize) |
| 89 |
{ |
| 90 |
int i = 0; |
| 91 |
|
| 92 |
while (i < (bufsize - 1) && orig[i]) { |
| 93 |
buf[i] = toupper(orig[i]); |
| 94 |
i++; |
| 95 |
} |
| 96 |
|
| 97 |
buf[i] = '\0'; |
| 98 |
|
| 99 |
return buf; |
| 100 |
} |
| 101 |
|
| 102 |
char *strcasestr(const char *haystack, const char *needle) |
| 103 |
{ |
| 104 |
char *u1, *u2; |
| 105 |
int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1; |
| 106 |
|
| 107 |
u1 = alloca(u1len); |
| 108 |
u2 = alloca(u2len); |
| 109 |
if (u1 && u2) { |
| 110 |
char *offset; |
| 111 |
if (u2len > u1len) { |
| 112 |
|
| 113 |
return NULL; |
| 114 |
} |
| 115 |
offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len)); |
| 116 |
if (offset) { |
| 117 |
|
| 118 |
return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1))); |
| 119 |
} else { |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
} else { |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
} |
| 126 |
#endif |
| 127 |
|
| 128 |
#ifndef HAVE_STRNLEN |
| 129 |
size_t strnlen(const char *s, size_t n) |
| 130 |
{ |
| 131 |
size_t len; |
| 132 |
|
| 133 |
for (len = 0; len < n; len++) |
| 134 |
if (s[len] == '\0') |
| 135 |
break; |
| 136 |
|
| 137 |
return len; |
| 138 |
} |
| 139 |
#endif |
| 140 |
|
| 141 |
#if !defined(HAVE_STRNDUP) |
| 142 |
char *strndup(const char *s, size_t n) |
| 143 |
{ |
| 144 |
size_t len = strnlen(s, n); |
| 145 |
char *new = malloc(len + 1); |
| 146 |
|
| 147 |
if (!new) |
| 148 |
return NULL; |
| 149 |
|
| 150 |
new[len] = '\0'; |
| 151 |
return memcpy(new, s, len); |
| 152 |
} |
| 153 |
#endif |
| 154 |
|
| 155 |
#if !defined(HAVE_VASPRINTF) |
| 156 |
int vasprintf(char **strp, const char *fmt, va_list ap) |
| 157 |
{ |
| 158 |
int size; |
| 159 |
va_list ap2; |
| 160 |
char s; |
| 161 |
|
| 162 |
*strp = NULL; |
| 163 |
va_copy(ap2, ap); |
| 164 |
size = vsnprintf(&s, 1, fmt, ap2); |
| 165 |
va_end(ap2); |
| 166 |
*strp = malloc(size + 1); |
| 167 |
if (!*strp) |
| 168 |
return -1; |
| 169 |
vsnprintf(*strp, size + 1, fmt, ap); |
| 170 |
|
| 171 |
return size; |
| 172 |
} |
| 173 |
#endif |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
#if !defined(HAVE_ASPRINTF) |
| 195 |
int asprintf(char **str, const char *fmt, ...) |
| 196 |
{ |
| 197 |
va_list ap; |
| 198 |
int ret; |
| 199 |
|
| 200 |
*str = NULL; |
| 201 |
va_start(ap, fmt); |
| 202 |
ret = vasprintf(str, fmt, ap); |
| 203 |
va_end(ap); |
| 204 |
|
| 205 |
return ret; |
| 206 |
} |
| 207 |
#endif |
| 208 |
|
| 209 |
#ifndef HAVE_GETLOADAVG |
| 210 |
#ifdef linux |
| 211 |
|
| 212 |
int getloadavg(double *list, int nelem) |
| 213 |
{ |
| 214 |
FILE *LOADAVG; |
| 215 |
double avg[3] = { 0.0, 0.0, 0.0 }; |
| 216 |
int i, res = -1; |
| 217 |
|
| 218 |
if ((LOADAVG = fopen("/proc/loadavg", "r"))) { |
| 219 |
fscanf(LOADAVG, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]); |
| 220 |
res = 0; |
| 221 |
fclose(LOADAVG); |
| 222 |
} |
| 223 |
|
| 224 |
for (i = 0; (i < nelem) && (i < 3); i++) { |
| 225 |
list[i] = avg[i]; |
| 226 |
} |
| 227 |
|
| 228 |
return res; |
| 229 |
} |
| 230 |
#else |
| 231 |
|
| 232 |
|
| 233 |
int getloadavg(double *list, int nelem) |
| 234 |
{ |
| 235 |
int i; |
| 236 |
|
| 237 |
for (i = 0; i < nelem; i++) { |
| 238 |
list[i] = 0.1; |
| 239 |
} |
| 240 |
return -1; |
| 241 |
} |
| 242 |
#endif |
| 243 |
#endif |