xref: /aosp_15_r20/external/mtools/missFuncs.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 1991 Free Software Foundation, Inc.
2*d5c9a868SElliott Hughes  *  Copyright 1997,1999-2002,2007-2009 Alain Knaff.
3*d5c9a868SElliott Hughes  *  This file is part of mtools.
4*d5c9a868SElliott Hughes  *
5*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
6*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
7*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
8*d5c9a868SElliott Hughes  *  (at your option) any later version.
9*d5c9a868SElliott Hughes  *
10*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
11*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
14*d5c9a868SElliott Hughes  *
15*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
16*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
17*d5c9a868SElliott Hughes  */
18*d5c9a868SElliott Hughes #include "sysincludes.h"
19*d5c9a868SElliott Hughes #include "mtools.h"
20*d5c9a868SElliott Hughes 
21*d5c9a868SElliott Hughes #ifndef HAVE_STRDUP
22*d5c9a868SElliott Hughes 
23*d5c9a868SElliott Hughes 
strdup(const char * str)24*d5c9a868SElliott Hughes char *strdup(const char *str)
25*d5c9a868SElliott Hughes {
26*d5c9a868SElliott Hughes     char *nstr;
27*d5c9a868SElliott Hughes 
28*d5c9a868SElliott Hughes     if (str == (char*)0)
29*d5c9a868SElliott Hughes         return 0;
30*d5c9a868SElliott Hughes 
31*d5c9a868SElliott Hughes     nstr = (char*)malloc((strlen(str) + 1));
32*d5c9a868SElliott Hughes 
33*d5c9a868SElliott Hughes     if (nstr == (char*)0)
34*d5c9a868SElliott Hughes     {
35*d5c9a868SElliott Hughes         (void)fprintf(stderr, "strdup(): not enough memory to duplicate `%s'\n",
36*d5c9a868SElliott Hughes 		      str);
37*d5c9a868SElliott Hughes 	exit(1);
38*d5c9a868SElliott Hughes     }
39*d5c9a868SElliott Hughes 
40*d5c9a868SElliott Hughes     (void)strcpy(nstr, str);
41*d5c9a868SElliott Hughes 
42*d5c9a868SElliott Hughes     return nstr;
43*d5c9a868SElliott Hughes }
44*d5c9a868SElliott Hughes #endif /* HAVE_STRDUP */
45*d5c9a868SElliott Hughes 
46*d5c9a868SElliott Hughes #ifndef HAVE_STRNDUP
strndup(const char * s,size_t n)47*d5c9a868SElliott Hughes char *strndup( const char *s, size_t n )
48*d5c9a868SElliott Hughes {
49*d5c9a868SElliott Hughes     size_t nAvail;
50*d5c9a868SElliott Hughes     char *p;
51*d5c9a868SElliott Hughes 
52*d5c9a868SElliott Hughes     if ( !s )
53*d5c9a868SElliott Hughes         return 0;
54*d5c9a868SElliott Hughes 
55*d5c9a868SElliott Hughes     nAvail = min( strlen(s) + 1, n + 1 );
56*d5c9a868SElliott Hughes     p      = malloc( nAvail );
57*d5c9a868SElliott Hughes     if ( !p )
58*d5c9a868SElliott Hughes 	    return 0;
59*d5c9a868SElliott Hughes     memcpy( p, s, nAvail );
60*d5c9a868SElliott Hughes     p[nAvail - 1] = '\0';
61*d5c9a868SElliott Hughes 
62*d5c9a868SElliott Hughes     return p;
63*d5c9a868SElliott Hughes }
64*d5c9a868SElliott Hughes #endif /* HAVE_STRNDUP */
65*d5c9a868SElliott Hughes 
66*d5c9a868SElliott Hughes 
67*d5c9a868SElliott Hughes #ifdef HAVE_WCHAR_H
68*d5c9a868SElliott Hughes #ifndef HAVE_WCSDUP
wcsdup(const wchar_t * wcs)69*d5c9a868SElliott Hughes wchar_t *wcsdup(const wchar_t *wcs)
70*d5c9a868SElliott Hughes {
71*d5c9a868SElliott Hughes     wchar_t *nwcs;
72*d5c9a868SElliott Hughes 
73*d5c9a868SElliott Hughes     if (wcs == (wchar_t*)0)
74*d5c9a868SElliott Hughes         return 0;
75*d5c9a868SElliott Hughes 
76*d5c9a868SElliott Hughes     nwcs = (wchar_t*)calloc(wcslen(wcs) + 1, sizeof(wchar_t));
77*d5c9a868SElliott Hughes 
78*d5c9a868SElliott Hughes     if (nwcs == (wchar_t*)0)
79*d5c9a868SElliott Hughes     {
80*d5c9a868SElliott Hughes         (void)fprintf(stderr, "wcsdup(): not enough memory to duplicate `%ls'\n",
81*d5c9a868SElliott Hughes 		      wcs);
82*d5c9a868SElliott Hughes 	exit(1);
83*d5c9a868SElliott Hughes     }
84*d5c9a868SElliott Hughes 
85*d5c9a868SElliott Hughes     (void)wcscpy(nwcs, wcs);
86*d5c9a868SElliott Hughes 
87*d5c9a868SElliott Hughes     return nwcs;
88*d5c9a868SElliott Hughes }
89*d5c9a868SElliott Hughes #endif /* HAVE_WCSDUP */
90*d5c9a868SElliott Hughes #endif
91*d5c9a868SElliott Hughes 
92*d5c9a868SElliott Hughes #ifndef HAVE_MEMCPY
93*d5c9a868SElliott Hughes /*
94*d5c9a868SElliott Hughes  * Copy contents of memory (with possible overlapping).
95*d5c9a868SElliott Hughes  */
memcpy(char * s1,const char * s2,size_t n)96*d5c9a868SElliott Hughes char *memcpy(char *s1, const char *s2, size_t n)
97*d5c9a868SElliott Hughes {
98*d5c9a868SElliott Hughes 	bcopy(s2, s1, n);
99*d5c9a868SElliott Hughes 	return(s1);
100*d5c9a868SElliott Hughes }
101*d5c9a868SElliott Hughes #endif
102*d5c9a868SElliott Hughes 
103*d5c9a868SElliott Hughes #ifndef HAVE_MEMSET
104*d5c9a868SElliott Hughes /*
105*d5c9a868SElliott Hughes  * Copies the character c, n times to string s
106*d5c9a868SElliott Hughes  */
memset(char * s,char c,size_t n)107*d5c9a868SElliott Hughes char *memset(char *s, char c, size_t n)
108*d5c9a868SElliott Hughes {
109*d5c9a868SElliott Hughes 	char *s1 = s;
110*d5c9a868SElliott Hughes 
111*d5c9a868SElliott Hughes 	while (n > 0) {
112*d5c9a868SElliott Hughes 		--n;
113*d5c9a868SElliott Hughes 		*s++ = c;
114*d5c9a868SElliott Hughes 	}
115*d5c9a868SElliott Hughes 	return(s1);
116*d5c9a868SElliott Hughes }
117*d5c9a868SElliott Hughes #endif /* HAVE_MEMSET */
118*d5c9a868SElliott Hughes 
119*d5c9a868SElliott Hughes 
120*d5c9a868SElliott Hughes #ifndef HAVE_STRCHR
121*d5c9a868SElliott Hughes 
strchr(const char * s,int c)122*d5c9a868SElliott Hughes char * strchr (const char* s, int c)
123*d5c9a868SElliott Hughes {
124*d5c9a868SElliott Hughes 	if (!s) return NULL;
125*d5c9a868SElliott Hughes 	while (*s && *s != c) s++;
126*d5c9a868SElliott Hughes 	if (*s)
127*d5c9a868SElliott Hughes 		return (char*) s;
128*d5c9a868SElliott Hughes 	else
129*d5c9a868SElliott Hughes 		return NULL;
130*d5c9a868SElliott Hughes }
131*d5c9a868SElliott Hughes 
132*d5c9a868SElliott Hughes #endif
133*d5c9a868SElliott Hughes 
134*d5c9a868SElliott Hughes #ifndef HAVE_STRRCHR
135*d5c9a868SElliott Hughes 
strrchr(const char * s1,int c)136*d5c9a868SElliott Hughes char * strrchr (const char* s1, int c)
137*d5c9a868SElliott Hughes {
138*d5c9a868SElliott Hughes 	char* s = (char*) s1;
139*d5c9a868SElliott Hughes 	char* start = (char*) s;
140*d5c9a868SElliott Hughes 	if (!s) return NULL;
141*d5c9a868SElliott Hughes 	s += strlen(s)-1;
142*d5c9a868SElliott Hughes 	while (*s != c && (unsigned long) s != (unsigned long) start) s--;
143*d5c9a868SElliott Hughes 	if ((unsigned long) s == (unsigned long) start && *s != c)
144*d5c9a868SElliott Hughes 		return NULL;
145*d5c9a868SElliott Hughes 	else
146*d5c9a868SElliott Hughes 		return s;
147*d5c9a868SElliott Hughes }
148*d5c9a868SElliott Hughes 
149*d5c9a868SElliott Hughes #endif
150*d5c9a868SElliott Hughes 
151*d5c9a868SElliott Hughes #ifndef HAVE_STRPBRK
152*d5c9a868SElliott Hughes /*
153*d5c9a868SElliott Hughes  * Return ptr to first occurrence of any character from `brkset'
154*d5c9a868SElliott Hughes  * in the character string `string'; NULL if none exists.
155*d5c9a868SElliott Hughes  */
strpbrk(const char * string,const char * brkset)156*d5c9a868SElliott Hughes char *strpbrk(const char *string, const char *brkset)
157*d5c9a868SElliott Hughes {
158*d5c9a868SElliott Hughes 	register char *p;
159*d5c9a868SElliott Hughes 
160*d5c9a868SElliott Hughes 	if (!string || !brkset)
161*d5c9a868SElliott Hughes 		return(0);
162*d5c9a868SElliott Hughes 	do {
163*d5c9a868SElliott Hughes 		for (p = brkset; *p != '\0' && *p != *string; ++p)
164*d5c9a868SElliott Hughes 			;
165*d5c9a868SElliott Hughes 		if (*p != '\0')
166*d5c9a868SElliott Hughes 			return(string);
167*d5c9a868SElliott Hughes 	}
168*d5c9a868SElliott Hughes 	while (*string++);
169*d5c9a868SElliott Hughes 	return(0);
170*d5c9a868SElliott Hughes }
171*d5c9a868SElliott Hughes #endif /* HAVE_STRPBRK */
172*d5c9a868SElliott Hughes 
173*d5c9a868SElliott Hughes 
174*d5c9a868SElliott Hughes #ifndef HAVE_STRTOUL
getdigit(char a,int max)175*d5c9a868SElliott Hughes static int getdigit(char a, int max)
176*d5c9a868SElliott Hughes {
177*d5c9a868SElliott Hughes 	int dig;
178*d5c9a868SElliott Hughes 
179*d5c9a868SElliott Hughes 	if(a < '0')
180*d5c9a868SElliott Hughes 		return -1;
181*d5c9a868SElliott Hughes 	if(a <= '9') {
182*d5c9a868SElliott Hughes 		dig = a - '0';
183*d5c9a868SElliott Hughes 	} else if(a >= 'a')
184*d5c9a868SElliott Hughes 		dig = a - 'a' + 10;
185*d5c9a868SElliott Hughes 	else if(a >= 'A')
186*d5c9a868SElliott Hughes 		dig = a - 'A' + 10;
187*d5c9a868SElliott Hughes 	if(dig >= max)
188*d5c9a868SElliott Hughes 		return -1;
189*d5c9a868SElliott Hughes 	else
190*d5c9a868SElliott Hughes 		return dig;
191*d5c9a868SElliott Hughes }
192*d5c9a868SElliott Hughes 
strtoul(const char * string,char ** eptr,int base)193*d5c9a868SElliott Hughes unsigned long strtoul(const char *string, char **eptr, int base)
194*d5c9a868SElliott Hughes {
195*d5c9a868SElliott Hughes 	int accu, dig;
196*d5c9a868SElliott Hughes 
197*d5c9a868SElliott Hughes 	if(base < 1 || base > 36) {
198*d5c9a868SElliott Hughes 		if(string[0] == '0') {
199*d5c9a868SElliott Hughes 			switch(string[1]) {
200*d5c9a868SElliott Hughes 			       	case 'x':
201*d5c9a868SElliott Hughes 				case 'X':
202*d5c9a868SElliott Hughes 					return strtoul(string+2, eptr, 16);
203*d5c9a868SElliott Hughes 				case 'b':
204*d5c9a868SElliott Hughes 			       	case 'B':
205*d5c9a868SElliott Hughes 					return strtoul(string+2, eptr, 2);
206*d5c9a868SElliott Hughes 				default:
207*d5c9a868SElliott Hughes 					return strtoul(string, eptr, 8);
208*d5c9a868SElliott Hughes 			}
209*d5c9a868SElliott Hughes 		}
210*d5c9a868SElliott Hughes 	       	return strtoul(string, eptr, 10);
211*d5c9a868SElliott Hughes 	}
212*d5c9a868SElliott Hughes 	if(base == 16 && string[0] == '0' &&
213*d5c9a868SElliott Hughes 	   (string[1] == 'x' || string[1] == 'X'))
214*d5c9a868SElliott Hughes 		string += 2;
215*d5c9a868SElliott Hughes 
216*d5c9a868SElliott Hughes 	if(base == 2 && string[0] == '0' &&
217*d5c9a868SElliott Hughes 	   (string[1] == 'b' || string[1] == 'B'))
218*d5c9a868SElliott Hughes 		string += 2;
219*d5c9a868SElliott Hughes 	accu = 0;
220*d5c9a868SElliott Hughes 	while( (dig = getdigit(*string, base)) != -1 ) {
221*d5c9a868SElliott Hughes 		accu = accu * base + dig;
222*d5c9a868SElliott Hughes 		string++;
223*d5c9a868SElliott Hughes 	}
224*d5c9a868SElliott Hughes 	if(eptr)
225*d5c9a868SElliott Hughes 		*eptr = (char *) string;
226*d5c9a868SElliott Hughes 	return accu;
227*d5c9a868SElliott Hughes }
228*d5c9a868SElliott Hughes #endif /* HAVE_STRTOUL */
229*d5c9a868SElliott Hughes 
230*d5c9a868SElliott Hughes #ifndef HAVE_STRTOL
strtol(const char * string,char ** eptr,int base)231*d5c9a868SElliott Hughes long strtol(const char *string, char **eptr, int base)
232*d5c9a868SElliott Hughes {
233*d5c9a868SElliott Hughes 	long l;
234*d5c9a868SElliott Hughes 
235*d5c9a868SElliott Hughes 	if(*string == '-') {
236*d5c9a868SElliott Hughes 		return -(long) strtoul(string+1, eptr, base);
237*d5c9a868SElliott Hughes 	} else {
238*d5c9a868SElliott Hughes 		if (*string == '+')
239*d5c9a868SElliott Hughes 			string ++;
240*d5c9a868SElliott Hughes 		return (long) strtoul(string, eptr, base);
241*d5c9a868SElliott Hughes 	}
242*d5c9a868SElliott Hughes }
243*d5c9a868SElliott Hughes #endif
244*d5c9a868SElliott Hughes 
245*d5c9a868SElliott Hughes 
246*d5c9a868SElliott Hughes 
247*d5c9a868SElliott Hughes #ifndef HAVE_STRSPN
248*d5c9a868SElliott Hughes /* Return the length of the maximum initial segment
249*d5c9a868SElliott Hughes    of S which contains only characters in ACCEPT.  */
strspn(const char * s,const char * accept)250*d5c9a868SElliott Hughes size_t strspn(const char *s, const char *accept)
251*d5c9a868SElliott Hughes {
252*d5c9a868SElliott Hughes   register char *p;
253*d5c9a868SElliott Hughes   register char *a;
254*d5c9a868SElliott Hughes   register size_t count = 0;
255*d5c9a868SElliott Hughes 
256*d5c9a868SElliott Hughes   for (p = s; *p != '\0'; ++p)
257*d5c9a868SElliott Hughes     {
258*d5c9a868SElliott Hughes       for (a = accept; *a != '\0'; ++a)
259*d5c9a868SElliott Hughes 	if (*p == *a)
260*d5c9a868SElliott Hughes 	  break;
261*d5c9a868SElliott Hughes       if (*a == '\0')
262*d5c9a868SElliott Hughes 	return count;
263*d5c9a868SElliott Hughes       else
264*d5c9a868SElliott Hughes 	++count;
265*d5c9a868SElliott Hughes     }
266*d5c9a868SElliott Hughes 
267*d5c9a868SElliott Hughes   return count;
268*d5c9a868SElliott Hughes }
269*d5c9a868SElliott Hughes #endif /* HAVE_STRSPN */
270*d5c9a868SElliott Hughes 
271*d5c9a868SElliott Hughes #ifndef HAVE_STRCSPN
272*d5c9a868SElliott Hughes /* Return the length of the maximum initial segment of S
273*d5c9a868SElliott Hughes    which contains no characters from REJECT.  */
strcspn(const char * s,const char * reject)274*d5c9a868SElliott Hughes size_t strcspn (const char *s, const char *reject)
275*d5c9a868SElliott Hughes {
276*d5c9a868SElliott Hughes   register size_t count = 0;
277*d5c9a868SElliott Hughes 
278*d5c9a868SElliott Hughes   while (*s != '\0')
279*d5c9a868SElliott Hughes     if (strchr (reject, *s++) == NULL)
280*d5c9a868SElliott Hughes       ++count;
281*d5c9a868SElliott Hughes     else
282*d5c9a868SElliott Hughes       return count;
283*d5c9a868SElliott Hughes 
284*d5c9a868SElliott Hughes   return count;
285*d5c9a868SElliott Hughes }
286*d5c9a868SElliott Hughes 
287*d5c9a868SElliott Hughes #endif /* HAVE_STRCSPN */
288*d5c9a868SElliott Hughes 
289*d5c9a868SElliott Hughes #ifndef HAVE_STRERROR
290*d5c9a868SElliott Hughes 
291*d5c9a868SElliott Hughes #ifndef DECL_SYS_ERRLIST
292*d5c9a868SElliott Hughes extern char *sys_errlist[];
293*d5c9a868SElliott Hughes #endif
294*d5c9a868SElliott Hughes 
strerror(int errno)295*d5c9a868SElliott Hughes char *strerror(int errno)
296*d5c9a868SElliott Hughes {
297*d5c9a868SElliott Hughes   return sys_errlist[errno];
298*d5c9a868SElliott Hughes }
299*d5c9a868SElliott Hughes #endif
300*d5c9a868SElliott Hughes 
301*d5c9a868SElliott Hughes #ifndef HAVE_STRCASECMP
302*d5c9a868SElliott Hughes /* Compare S1 and S2, ignoring case, returning less than, equal to or
303*d5c9a868SElliott Hughes    greater than zero if S1 is lexiographically less than,
304*d5c9a868SElliott Hughes    equal to or greater than S2.  */
strcasecmp(const char * s1,const char * s2)305*d5c9a868SElliott Hughes int strcasecmp(const char *s1, const char *s2)
306*d5c9a868SElliott Hughes {
307*d5c9a868SElliott Hughes   register const unsigned char *p1 = (const unsigned char *) s1;
308*d5c9a868SElliott Hughes   register const unsigned char *p2 = (const unsigned char *) s2;
309*d5c9a868SElliott Hughes   unsigned char c1, c2;
310*d5c9a868SElliott Hughes 
311*d5c9a868SElliott Hughes   if (p1 == p2)
312*d5c9a868SElliott Hughes     return 0;
313*d5c9a868SElliott Hughes 
314*d5c9a868SElliott Hughes   do
315*d5c9a868SElliott Hughes     {
316*d5c9a868SElliott Hughes       c1 = tolower (*p1++);
317*d5c9a868SElliott Hughes       c2 = tolower (*p2++);
318*d5c9a868SElliott Hughes       if (c1 == '\0')
319*d5c9a868SElliott Hughes 	break;
320*d5c9a868SElliott Hughes     }
321*d5c9a868SElliott Hughes   while (c1 == c2);
322*d5c9a868SElliott Hughes 
323*d5c9a868SElliott Hughes   return c1 - c2;
324*d5c9a868SElliott Hughes }
325*d5c9a868SElliott Hughes #endif
326*d5c9a868SElliott Hughes 
327*d5c9a868SElliott Hughes #ifdef HAVE_WCHAR_H
328*d5c9a868SElliott Hughes #ifndef HAVE_WCSCASECMP
329*d5c9a868SElliott Hughes /* Compare S1 and S2, ignoring case, returning less than, equal to or
330*d5c9a868SElliott Hughes    greater than zero if S1 is lexiographically less than,
331*d5c9a868SElliott Hughes    equal to or greater than S2.  */
wcscasecmp(const wchar_t * s1,const wchar_t * s2)332*d5c9a868SElliott Hughes int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
333*d5c9a868SElliott Hughes {
334*d5c9a868SElliott Hughes   register const wchar_t *p1 = s1;
335*d5c9a868SElliott Hughes   register const wchar_t *p2 = s2;
336*d5c9a868SElliott Hughes   wchar_t c1, c2;
337*d5c9a868SElliott Hughes 
338*d5c9a868SElliott Hughes   if (p1 == p2)
339*d5c9a868SElliott Hughes     return 0;
340*d5c9a868SElliott Hughes 
341*d5c9a868SElliott Hughes   do
342*d5c9a868SElliott Hughes     {
343*d5c9a868SElliott Hughes       c1 = towlower (*p1++);
344*d5c9a868SElliott Hughes       c2 = towlower (*p2++);
345*d5c9a868SElliott Hughes       if (c1 == '\0')
346*d5c9a868SElliott Hughes 	break;
347*d5c9a868SElliott Hughes     }
348*d5c9a868SElliott Hughes   while (c1 == c2);
349*d5c9a868SElliott Hughes 
350*d5c9a868SElliott Hughes   return c1 - c2;
351*d5c9a868SElliott Hughes }
352*d5c9a868SElliott Hughes #endif
353*d5c9a868SElliott Hughes #endif
354*d5c9a868SElliott Hughes 
355*d5c9a868SElliott Hughes 
356*d5c9a868SElliott Hughes #ifndef HAVE_STRCASECMP
357*d5c9a868SElliott Hughes /* Compare S1 and S2, ignoring case, returning less than, equal to or
358*d5c9a868SElliott Hughes    greater than zero if S1 is lexiographically less than,
359*d5c9a868SElliott Hughes    equal to or greater than S2.  */
strncasecmp(const char * s1,const char * s2,size_t n)360*d5c9a868SElliott Hughes int strncasecmp(const char *s1, const char *s2, size_t n)
361*d5c9a868SElliott Hughes {
362*d5c9a868SElliott Hughes   register const unsigned char *p1 = (const unsigned char *) s1;
363*d5c9a868SElliott Hughes   register const unsigned char *p2 = (const unsigned char *) s2;
364*d5c9a868SElliott Hughes   unsigned char c1, c2;
365*d5c9a868SElliott Hughes 
366*d5c9a868SElliott Hughes   if (p1 == p2)
367*d5c9a868SElliott Hughes     return 0;
368*d5c9a868SElliott Hughes 
369*d5c9a868SElliott Hughes   c1 = c2 = 1;
370*d5c9a868SElliott Hughes   while (c1 && c1 == c2 && n-- > 0)
371*d5c9a868SElliott Hughes     {
372*d5c9a868SElliott Hughes       c1 = tolower (*p1++);
373*d5c9a868SElliott Hughes       c2 = tolower (*p2++);
374*d5c9a868SElliott Hughes     }
375*d5c9a868SElliott Hughes 
376*d5c9a868SElliott Hughes   return c1 - c2;
377*d5c9a868SElliott Hughes }
378*d5c9a868SElliott Hughes #endif
379*d5c9a868SElliott Hughes 
380*d5c9a868SElliott Hughes #ifndef HAVE_GETPASS
getpass(const char * prompt)381*d5c9a868SElliott Hughes char *getpass(const char *prompt)
382*d5c9a868SElliott Hughes {
383*d5c9a868SElliott Hughes 	static char password[129];
384*d5c9a868SElliott Hughes 	int l;
385*d5c9a868SElliott Hughes 
386*d5c9a868SElliott Hughes 	fprintf(stderr,"%s",prompt);
387*d5c9a868SElliott Hughes 	fgets(password, 128, stdin);
388*d5c9a868SElliott Hughes 	l = strlen(password);
389*d5c9a868SElliott Hughes 	if(l && password[l-1] == '\n')
390*d5c9a868SElliott Hughes 		password[l-1] = '\0';
391*d5c9a868SElliott Hughes 	return password;
392*d5c9a868SElliott Hughes 
393*d5c9a868SElliott Hughes }
394*d5c9a868SElliott Hughes #endif
395*d5c9a868SElliott Hughes 
396*d5c9a868SElliott Hughes #ifndef HAVE_ATEXIT
397*d5c9a868SElliott Hughes 
398*d5c9a868SElliott Hughes #ifdef HAVE_ON_EXIT
atexit(void (* function)(void))399*d5c9a868SElliott Hughes int atexit(void (*function)(void))
400*d5c9a868SElliott Hughes {
401*d5c9a868SElliott Hughes 	return on_exit( (void(*)(int,void*)) function, 0);
402*d5c9a868SElliott Hughes }
403*d5c9a868SElliott Hughes #else
404*d5c9a868SElliott Hughes 
405*d5c9a868SElliott Hughes typedef struct exitCallback {
406*d5c9a868SElliott Hughes 	void (*function) (void);
407*d5c9a868SElliott Hughes 	struct exitCallback *next;
408*d5c9a868SElliott Hughes } exitCallback_t;
409*d5c9a868SElliott Hughes 
410*d5c9a868SElliott Hughes static exitCallback_t *callback = 0;
411*d5c9a868SElliott Hughes 
atexit(void (* function)(void))412*d5c9a868SElliott Hughes int atexit(void (*function) (void))
413*d5c9a868SElliott Hughes {
414*d5c9a868SElliott Hughes 	exitCallback_t *newCallback;
415*d5c9a868SElliott Hughes 
416*d5c9a868SElliott Hughes 	newCallback = New(exitCallback_t);
417*d5c9a868SElliott Hughes 	if(!newCallback) {
418*d5c9a868SElliott Hughes 		printOom();
419*d5c9a868SElliott Hughes 		exit(1);
420*d5c9a868SElliott Hughes 	}
421*d5c9a868SElliott Hughes 	newCallback->function = function;
422*d5c9a868SElliott Hughes 	newCallback->next = callback;
423*d5c9a868SElliott Hughes 	callback = newCallback;
424*d5c9a868SElliott Hughes 	return 0;
425*d5c9a868SElliott Hughes }
426*d5c9a868SElliott Hughes #undef exit
427*d5c9a868SElliott Hughes 
myexit(int code)428*d5c9a868SElliott Hughes void myexit(int code)
429*d5c9a868SElliott Hughes {
430*d5c9a868SElliott Hughes   void (*function)(void);
431*d5c9a868SElliott Hughes 
432*d5c9a868SElliott Hughes   while(callback) {
433*d5c9a868SElliott Hughes     function = callback->function;
434*d5c9a868SElliott Hughes     callback = callback->next;
435*d5c9a868SElliott Hughes     function();
436*d5c9a868SElliott Hughes   }
437*d5c9a868SElliott Hughes   exit(code);
438*d5c9a868SElliott Hughes }
439*d5c9a868SElliott Hughes 
440*d5c9a868SElliott Hughes #endif
441*d5c9a868SElliott Hughes 
442*d5c9a868SElliott Hughes #endif
443*d5c9a868SElliott Hughes 
444*d5c9a868SElliott Hughes static const char PATH_SEP = '/';
445*d5c9a868SElliott Hughes 
446*d5c9a868SElliott Hughes /*#ifndef HAVE_BASENAME*/
_basename(const char * filename)447*d5c9a868SElliott Hughes const char *_basename(const char *filename)
448*d5c9a868SElliott Hughes {
449*d5c9a868SElliott Hughes 	char *ptr;
450*d5c9a868SElliott Hughes 
451*d5c9a868SElliott Hughes 	ptr = strrchr(filename, PATH_SEP);
452*d5c9a868SElliott Hughes 	if(ptr)
453*d5c9a868SElliott Hughes 		filename = ptr + 1;
454*d5c9a868SElliott Hughes 
455*d5c9a868SElliott Hughes #ifdef OS_mingw32msvc
456*d5c9a868SElliott Hughes 	ptr = strrchr(filename, '\\');
457*d5c9a868SElliott Hughes 	if(ptr)
458*d5c9a868SElliott Hughes 		filename = ptr + 1;
459*d5c9a868SElliott Hughes #endif
460*d5c9a868SElliott Hughes 
461*d5c9a868SElliott Hughes 	return filename;
462*d5c9a868SElliott Hughes }
463*d5c9a868SElliott Hughes /*#endif*/
464*d5c9a868SElliott Hughes 
465*d5c9a868SElliott Hughes /* Strip the suffix ".exe" from the argument, if present. */
_stripexe(char * filename)466*d5c9a868SElliott Hughes void _stripexe(char *filename)
467*d5c9a868SElliott Hughes {
468*d5c9a868SElliott Hughes 	char *ptr;
469*d5c9a868SElliott Hughes 	ptr = strrchr(filename, '.');
470*d5c9a868SElliott Hughes 	if(ptr && !strcasecmp(ptr, ".exe"))
471*d5c9a868SElliott Hughes 		*ptr = '\0';
472*d5c9a868SElliott Hughes }
473*d5c9a868SElliott Hughes 
474*d5c9a868SElliott Hughes #ifndef HAVE_STRNLEN
strnlen(const char * str,size_t l)475*d5c9a868SElliott Hughes size_t strnlen(const char *str, size_t l)
476*d5c9a868SElliott Hughes {
477*d5c9a868SElliott Hughes   size_t i;
478*d5c9a868SElliott Hughes   for(i=0; i<l; i++) {
479*d5c9a868SElliott Hughes     if(str[i] == 0)
480*d5c9a868SElliott Hughes       break;
481*d5c9a868SElliott Hughes   }
482*d5c9a868SElliott Hughes   return i;
483*d5c9a868SElliott Hughes }
484*d5c9a868SElliott Hughes #endif /* HAVE_STRNLEN */
485*d5c9a868SElliott Hughes 
486*d5c9a868SElliott Hughes #ifdef HAVE_WCHAR_H
487*d5c9a868SElliott Hughes #ifndef HAVE_WCSNLEN
wcsnlen(const wchar_t * wcs,size_t l)488*d5c9a868SElliott Hughes size_t wcsnlen(const wchar_t *wcs, size_t l)
489*d5c9a868SElliott Hughes {
490*d5c9a868SElliott Hughes   size_t i;
491*d5c9a868SElliott Hughes   for(i=0; i<l; i++) {
492*d5c9a868SElliott Hughes     if(wcs[i] == 0)
493*d5c9a868SElliott Hughes       break;
494*d5c9a868SElliott Hughes   }
495*d5c9a868SElliott Hughes   return i;
496*d5c9a868SElliott Hughes }
497*d5c9a868SElliott Hughes #endif /* HAVE_WCSNLEN */
498*d5c9a868SElliott Hughes #endif
499