1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file contains excerpts of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include "sysincludes.h"
#include "mtools.h"
#ifndef HAVE_STRDUP
char *strdup(const char *str)
{
char *nstr;
if (str == (char*)0)
return 0;
nstr = (char*)malloc((strlen(str) + 1));
if (nstr == (char*)0)
{
(void)fprintf(stderr, "strdup(): not enough memory to duplicate `%s'\n",
str);
exit(1);
}
(void)strcpy(nstr, str);
return nstr;
}
#endif /* HAVE_STRDUP */
#ifndef HAVE_MEMCPY
/*
* Copy contents of memory (with possible overlapping).
*/
char *memcpy(char *s1, const char *s2, size_t n)
{
bcopy(s2, s1, n);
return(s1);
}
#endif
#ifndef HAVE_MEMSET
/*
* Copies the character c, n times to string s
*/
char *memset(char *s, char c, size_t n)
{
char *s1 = s;
while (n > 0) {
--n;
*s++ = c;
}
return(s1);
}
#endif /* HAVE_MEMSET */
#ifndef HAVE_STRPBRK
/*
* Return ptr to first occurrence of any character from `brkset'
* in the character string `string'; NULL if none exists.
*/
char *strpbrk(const char *string, const char *brkset)
{
register char *p;
if (!string || !brkset)
return(0);
do {
for (p = brkset; *p != '\0' && *p != *string; ++p)
;
if (*p != '\0')
return(string);
}
while (*string++);
return(0);
}
#endif /* HAVE_STRPBRK */
#ifndef HAVE_STRTOUL
static int getdigit(char a, int max)
{
int dig;
if(a < '0')
return -1;
if(a <= '9') {
dig = a - '0';
} else if(a >= 'a')
dig = a - 'a' + 10;
else if(a >= 'A')
dig = a - 'A' + 10;
if(dig >= max)
return -1;
else
return dig;
}
unsigned long strtoul(const char *string, char **eptr, int base)
{
int accu, dig;
if(base < 1 || base > 36) {
if(string[0] == '0') {
switch(string[1]) {
case 'x':
case 'X':
return strtoul(string+2, eptr, 16);
case 'b':
case 'B':
return strtoul(string+2, eptr, 2);
default:
return strtoul(string, eptr, 8);
}
}
return strtoul(string, eptr, 10);
}
if(base == 16 && string[0] == '0' &&
(string[1] == 'x' || string[1] == 'X'))
string += 2;
if(base == 2 && string[0] == '0' &&
(string[1] == 'b' || string[1] == 'B'))
string += 2;
accu = 0;
while( (dig = getdigit(*string, base)) != -1 ) {
accu = accu * base + dig;
string++;
}
if(eptr)
*eptr = (char *) string;
return accu;
}
#endif /* HAVE_STRTOUL */
#ifndef HAVE_STRSPN
/* Return the length of the maximum initial segment
of S which contains only characters in ACCEPT. */
size_t strspn(const char *s, const char *accept)
{
register CONST char *p;
register CONST char *a;
register size_t count = 0;
for (p = s; *p != '\0'; ++p)
{
for (a = accept; *a != '\0'; ++a)
if (*p == *a)
break;
if (*a == '\0')
return count;
else
++count;
}
return count;
}
#endif /* HAVE_STRSPN */
#ifndef HAVE_STRCSPN
/* Return the length of the maximum inital segment of S
which contains no characters from REJECT. */
size_t strcspn (const char *s, const char *reject)
{
register size_t count = 0;
while (*s != '\0')
if (strchr (reject, *s++) == NULL)
++count;
else
return count;
return count;
}
#endif /* HAVE_STRCSPN */
#ifndef HAVE_STRERROR
char *strerror(int errno)
{
return sys_errlist[errno];
}
#endif
#ifndef HAVE_STRCASECMP
/* Compare S1 and S2, ignoring case, returning less than, equal to or
greater than zero if S1 is lexiographically less than,
equal to or greater than S2. */
int strcasecmp(const char *s1, const char *s2)
{
register const unsigned char *p1 = (const unsigned char *) s1;
register const unsigned char *p2 = (const unsigned char *) s2;
unsigned char c1, c2;
if (p1 == p2)
return 0;
do
{
c1 = tolower (*p1++);
c2 = tolower (*p2++);
if (c1 == '\0')
break;
}
while (c1 == c2);
return c1 - c2;
}
#endif
#ifndef HAVE_STRCASECMP
/* Compare S1 and S2, ignoring case, returning less than, equal to or
greater than zero if S1 is lexiographically less than,
equal to or greater than S2. */
int strncasecmp(const char *s1, const char *s2, size_t n)
{
register const unsigned char *p1 = (const unsigned char *) s1;
register const unsigned char *p2 = (const unsigned char *) s2;
unsigned char c1, c2;
if (p1 == p2)
return 0;
c1 = c2 = 1;
while (c1 && c1 == c2 && n-- > 0)
{
c1 = tolower (*p1++);
c2 = tolower (*p2++);
}
return c1 - c2;
}
#endif
#ifndef HAVE_GETPASS
char *getpass(const char *prompt)
{
static char password[129];
int l;
fprintf(stderr,"%s",prompt);
fgets(password, 128, stdin);
l = strlen(password);
if(l && password[l-1] == '\n')
password[l-1] = '\0';
return password;
}
#endif
#ifndef HAVE_ATEXIT
#ifdef HAVE_ON_EXIT
int atexit(void (*function)(void))
{
return on_exit( (void(*)(int,void*)) function, 0);
}
#else
typedef struct exitCallback {
void (*function) (void);
struct exitCallback *next;
} exitCallback_t;
static exitCallback_t *callback = 0;
int atexit(void (*function) (void))
{
exitCallback_t *newCallback;
newCallback = New(exitCallback_t);
if(!newCallback) {
fprintf(stderr,"Out of memory error\n");
exit(1);
}
newCallback->function = function;
newCallback->next = callback;
callback = newCallback;
return 0;
}
#undef exit
void myexit(int code)
{
void (*function)(void);
while(callback) {
function = callback->function;
callback = callback->next;
function();
}
exit(code);
}
#endif
#endif
|