source: trunk/minix/commands/i386/mtools-3.9.7/missFuncs.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

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