1 /* Copyright 1991 Free Software Foundation, Inc.
2 * Copyright 1997,1999-2002,2007-2009 Alain Knaff.
3 * This file is part of mtools.
5 * Mtools is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Mtools is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
18 #include "sysincludes.h"
24 char *strdup(const char *str)
31 nstr = (char*)malloc((strlen(str) + 1));
35 (void)fprintf(stderr, "strdup(): not enough memory to duplicate `%s'\n",
40 (void)strcpy(nstr, str);
44 #endif /* HAVE_STRDUP */
48 wchar_t *wcsdup(const wchar_t *wcs)
52 if (wcs == (wchar_t*)0)
55 nwcs = (wchar_t*)calloc(wcslen(wcs) + 1, sizeof(wchar_t));
57 if (nwcs == (wchar_t*)0)
59 (void)fprintf(stderr, "wcsdup(): not enough memory to duplicate `%ls'\n",
64 (void)wcscpy(nwcs, wcs);
68 #endif /* HAVE_WCSDUP */
73 * Copy contents of memory (with possible overlapping).
75 char *memcpy(char *s1, const char *s2, size_t n)
84 * Copies the character c, n times to string s
86 char *memset(char *s, char c, size_t n)
96 #endif /* HAVE_MEMSET */
101 char * strchr (const char* s, int c)
104 while (*s && *s != c) s++;
115 char * strrchr (const char* s1, int c)
117 char* s = (char*) s1;
118 char* start = (char*) s;
121 while (*s != c && (unsigned long) s != (unsigned long) start) s--;
122 if ((unsigned long) s == (unsigned long) start && *s != c)
132 * Return ptr to first occurrence of any character from `brkset'
133 * in the character string `string'; NULL if none exists.
135 char *strpbrk(const char *string, const char *brkset)
139 if (!string || !brkset)
142 for (p = brkset; *p != '\0' && *p != *string; ++p)
150 #endif /* HAVE_STRPBRK */
154 static int getdigit(char a, int max)
172 unsigned long strtoul(const char *string, char **eptr, int base)
176 if(base < 1 || base > 36) {
177 if(string[0] == '0') {
181 return strtoul(string+2, eptr, 16);
184 return strtoul(string+2, eptr, 2);
186 return strtoul(string, eptr, 8);
189 return strtoul(string, eptr, 10);
191 if(base == 16 && string[0] == '0' &&
192 (string[1] == 'x' || string[1] == 'X'))
195 if(base == 2 && string[0] == '0' &&
196 (string[1] == 'b' || string[1] == 'B'))
199 while( (dig = getdigit(*string, base)) != -1 ) {
200 accu = accu * base + dig;
204 *eptr = (char *) string;
207 #endif /* HAVE_STRTOUL */
210 long strtol(const char *string, char **eptr, int base)
215 return -(long) strtoul(string+1, eptr, base);
219 return (long) strtoul(string, eptr, base);
227 /* Return the length of the maximum initial segment
228 of S which contains only characters in ACCEPT. */
229 size_t strspn(const char *s, const char *accept)
233 register size_t count = 0;
235 for (p = s; *p != '\0'; ++p)
237 for (a = accept; *a != '\0'; ++a)
248 #endif /* HAVE_STRSPN */
251 /* Return the length of the maximum inital segment of S
252 which contains no characters from REJECT. */
253 size_t strcspn (const char *s, const char *reject)
255 register size_t count = 0;
258 if (strchr (reject, *s++) == NULL)
266 #endif /* HAVE_STRCSPN */
268 #ifndef HAVE_STRERROR
270 #ifndef DECL_SYS_ERRLIST
271 extern char *sys_errlist[];
274 char *strerror(int errno)
276 return sys_errlist[errno];
280 #ifndef HAVE_STRCASECMP
281 /* Compare S1 and S2, ignoring case, returning less than, equal to or
282 greater than zero if S1 is lexiographically less than,
283 equal to or greater than S2. */
284 int strcasecmp(const char *s1, const char *s2)
286 register const unsigned char *p1 = (const unsigned char *) s1;
287 register const unsigned char *p2 = (const unsigned char *) s2;
288 unsigned char c1, c2;
295 c1 = tolower (*p1++);
296 c2 = tolower (*p2++);
307 #ifndef HAVE_WCSCASECMP
308 /* Compare S1 and S2, ignoring case, returning less than, equal to or
309 greater than zero if S1 is lexiographically less than,
310 equal to or greater than S2. */
311 int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
313 register const wchar_t *p1 = s1;
314 register const wchar_t *p2 = s2;
322 c1 = towlower (*p1++);
323 c2 = towlower (*p2++);
335 #ifndef HAVE_STRCASECMP
336 /* Compare S1 and S2, ignoring case, returning less than, equal to or
337 greater than zero if S1 is lexiographically less than,
338 equal to or greater than S2. */
339 int strncasecmp(const char *s1, const char *s2, size_t n)
341 register const unsigned char *p1 = (const unsigned char *) s1;
342 register const unsigned char *p2 = (const unsigned char *) s2;
343 unsigned char c1, c2;
349 while (c1 && c1 == c2 && n-- > 0)
351 c1 = tolower (*p1++);
352 c2 = tolower (*p2++);
360 char *getpass(const char *prompt)
362 static char password[129];
365 fprintf(stderr,"%s",prompt);
366 fgets(password, 128, stdin);
367 l = strlen(password);
368 if(l && password[l-1] == '\n')
369 password[l-1] = '\0';
378 int atexit(void (*function)(void))
380 return on_exit( (void(*)(int,void*)) function, 0);
384 typedef struct exitCallback {
385 void (*function) (void);
386 struct exitCallback *next;
389 static exitCallback_t *callback = 0;
391 int atexit(void (*function) (void))
393 exitCallback_t *newCallback;
395 newCallback = New(exitCallback_t);
400 newCallback->function = function;
401 newCallback->next = callback;
402 callback = newCallback;
407 void myexit(int code)
409 void (*function)(void);
412 function = callback->function;
413 callback = callback->next;
423 static const char PATH_SEP = '/';
425 /*#ifndef HAVE_BASENAME*/
426 const char *_basename(const char *filename)
430 ptr = strrchr(filename, PATH_SEP);
434 #ifdef OS_mingw32msvc
435 ptr = strrchr(filename, '\\');
444 /* Strip the suffix ".exe" from the argument, if present. */
445 void _stripexe(char *filename)
448 ptr = strrchr(filename, '.');
449 if(ptr && !strcasecmp(ptr, ".exe"))
454 size_t strnlen(const char *str, size_t l)
463 #endif /* HAVE_STRNLEN */
467 size_t wcsnlen(const wchar_t *wcs, size_t l)
476 #endif /* HAVE_WCSNLEN */