… | |
… | |
20 | * This file provides replacements for some handy functions that aren't |
20 | * This file provides replacements for some handy functions that aren't |
21 | * available on all systems, like most of the <string.h> functions. They |
21 | * available on all systems, like most of the <string.h> functions. They |
22 | * should behave exactly as their counterparts. There are also extensions |
22 | * should behave exactly as their counterparts. There are also extensions |
23 | * that aren't portable at all (like strirstr etc.). |
23 | * that aren't portable at all (like strirstr etc.). |
24 | * The proper behaviour in a configure script is as follows: |
24 | * The proper behaviour in a configure script is as follows: |
25 | * AC_CHECK_FUNC(strrchr,AC_DEFINE(strrchr,FP_strrchr)) |
25 | * AC_CHECK_FUNC(strrchr,AC_DEFINE(strrchr,_FP_strrchr)) |
26 | * This way, the (probably less efficient) replacements will only be used |
26 | * This way, the (probably less efficient) replacements will only be used |
27 | * where it is not provided by the default libraries. Be aware that this |
27 | * where it is not provided by the default libraries. Be aware that this |
28 | * does not work with replacements that just shadow wrong behaviour (like |
28 | * does not work with replacements that just shadow wrong behaviour (like |
29 | * FP_free) or provide extended functionality (FP_gets). |
29 | * _FP_free) or provide extended functionality (FP_gets). |
30 | * The above is not used in the uuenview/uudeview configuration script, |
30 | * The above is not used in the uuenview/uudeview configuration script, |
31 | * since both only use the replacement functions in non-performance-cri- |
31 | * since both only use the replacement functions in non-performance-cri- |
32 | * tical sections (except for FP_tempnam and FP_strerror, where some |
32 | * tical sections (except for _FP_tempnam and FP_strerror, where some |
33 | * functionality of the original would be lost). |
33 | * functionality of the original would be lost). |
34 | */ |
34 | */ |
35 | |
35 | |
36 | #include <stdio.h> |
36 | #include <stdio.h> |
37 | #include <ctype.h> |
37 | #include <ctype.h> |
… | |
… | |
62 | return TRUE; |
62 | return TRUE; |
63 | } |
63 | } |
64 | #endif |
64 | #endif |
65 | #endif |
65 | #endif |
66 | |
66 | |
67 | char * fptools_id = "$Id: fptools.c,v 1.4 2002/03/31 20:04:29 root Exp $"; |
67 | char * fptools_id = "$Id: fptools.c,v 1.5 2002/03/31 20:08:42 root Exp $"; |
68 | |
68 | |
69 | /* |
69 | /* |
70 | * some versions of free can't handle a NULL pointer properly |
70 | * some versions of free can't handle a NULL pointer properly |
71 | * (ANSI says, free ignores a NULL pointer, but some machines |
71 | * (ANSI says, free ignores a NULL pointer, but some machines |
72 | * prefer to SIGSEGV on it) |
72 | * prefer to SIGSEGV on it) |
73 | */ |
73 | */ |
74 | |
74 | |
75 | void TOOLEXPORT |
75 | void TOOLEXPORT |
76 | FP_free (void *ptr) |
76 | _FP_free (void *ptr) |
77 | { |
77 | { |
78 | if (ptr) free (ptr); |
78 | if (ptr) free (ptr); |
79 | } |
79 | } |
80 | |
80 | |
81 | /* |
81 | /* |
82 | * This is non-standard, so I'm defining my own |
82 | * This is non-standard, so I'm defining my own |
83 | */ |
83 | */ |
84 | |
84 | |
85 | char * TOOLEXPORT |
85 | char * TOOLEXPORT |
86 | FP_strdup (char *string) |
86 | _FP_strdup (char *string) |
87 | { |
87 | { |
88 | char *result; |
88 | char *result; |
89 | |
89 | |
90 | if (string == NULL) |
90 | if (string == NULL) |
91 | return NULL; |
91 | return NULL; |
… | |
… | |
102 | * the original in that the dest string is always terminated with a |
102 | * the original in that the dest string is always terminated with a |
103 | * NULL character. |
103 | * NULL character. |
104 | */ |
104 | */ |
105 | |
105 | |
106 | char * TOOLEXPORT |
106 | char * TOOLEXPORT |
107 | FP_strncpy (char *dest, char *src, int length) |
107 | _FP_strncpy (char *dest, char *src, int length) |
108 | { |
108 | { |
109 | char *odest=dest; |
109 | char *odest=dest; |
110 | if (src == NULL || dest == NULL || length-- <= 0) |
110 | if (src == NULL || dest == NULL || length-- <= 0) |
111 | return dest; |
111 | return dest; |
112 | |
112 | |
… | |
… | |
120 | /* |
120 | /* |
121 | * duplicate a memory area |
121 | * duplicate a memory area |
122 | */ |
122 | */ |
123 | |
123 | |
124 | void * TOOLEXPORT |
124 | void * TOOLEXPORT |
125 | FP_memdup (void *ptr, int len) |
125 | _FP_memdup (void *ptr, int len) |
126 | { |
126 | { |
127 | void *result; |
127 | void *result; |
128 | |
128 | |
129 | if (ptr == NULL) |
129 | if (ptr == NULL) |
130 | return NULL; |
130 | return NULL; |
… | |
… | |
139 | /* |
139 | /* |
140 | * case-insensitive compare |
140 | * case-insensitive compare |
141 | */ |
141 | */ |
142 | |
142 | |
143 | int TOOLEXPORT |
143 | int TOOLEXPORT |
144 | FP_stricmp (char *str1, char *str2) |
144 | _FP_stricmp (char *str1, char *str2) |
145 | { |
145 | { |
146 | if (str1==NULL || str2==NULL) |
146 | if (str1==NULL || str2==NULL) |
147 | return -1; |
147 | return -1; |
148 | |
148 | |
149 | while (*str1) { |
149 | while (*str1) { |
… | |
… | |
154 | } |
154 | } |
155 | return (tolower (*str1) - tolower (*str2)); |
155 | return (tolower (*str1) - tolower (*str2)); |
156 | } |
156 | } |
157 | |
157 | |
158 | int TOOLEXPORT |
158 | int TOOLEXPORT |
159 | FP_strnicmp (char *str1, char *str2, int count) |
159 | _FP_strnicmp (char *str1, char *str2, int count) |
160 | { |
160 | { |
161 | if (str1==NULL || str2==NULL) |
161 | if (str1==NULL || str2==NULL) |
162 | return -1; |
162 | return -1; |
163 | |
163 | |
164 | while (*str1 && count) { |
164 | while (*str1 && count) { |
… | |
… | |
174 | /* |
174 | /* |
175 | * autoconf says this function might be a compatibility problem |
175 | * autoconf says this function might be a compatibility problem |
176 | */ |
176 | */ |
177 | |
177 | |
178 | char * TOOLEXPORT |
178 | char * TOOLEXPORT |
179 | FP_strstr (char *str1, char *str2) |
179 | _FP_strstr (char *str1, char *str2) |
180 | { |
180 | { |
181 | char *ptr1, *ptr2; |
181 | char *ptr1, *ptr2; |
182 | |
182 | |
183 | if (str1==NULL) |
183 | if (str1==NULL) |
184 | return NULL; |
184 | return NULL; |
… | |
… | |
197 | } |
197 | } |
198 | return NULL; |
198 | return NULL; |
199 | } |
199 | } |
200 | |
200 | |
201 | char * TOOLEXPORT |
201 | char * TOOLEXPORT |
202 | FP_strpbrk (char *str, char *accept) |
202 | _FP_strpbrk (char *str, char *accept) |
203 | { |
203 | { |
204 | char *ptr; |
204 | char *ptr; |
205 | |
205 | |
206 | if (str == NULL) |
206 | if (str == NULL) |
207 | return NULL; |
207 | return NULL; |
… | |
… | |
219 | /* |
219 | /* |
220 | * autoconf also complains about this one |
220 | * autoconf also complains about this one |
221 | */ |
221 | */ |
222 | |
222 | |
223 | char * TOOLEXPORT |
223 | char * TOOLEXPORT |
224 | FP_strtok (char *str1, char *str2) |
224 | _FP_strtok (char *str1, char *str2) |
225 | { |
225 | { |
226 | static char *optr; |
226 | static char *optr; |
227 | char *ptr; |
227 | char *ptr; |
228 | |
228 | |
229 | if (str2 == NULL) |
229 | if (str2 == NULL) |
… | |
… | |
256 | /* |
256 | /* |
257 | * case insensitive strstr. |
257 | * case insensitive strstr. |
258 | */ |
258 | */ |
259 | |
259 | |
260 | char * TOOLEXPORT |
260 | char * TOOLEXPORT |
261 | FP_stristr (char *str1, char *str2) |
261 | _FP_stristr (char *str1, char *str2) |
262 | { |
262 | { |
263 | char *ptr1, *ptr2; |
263 | char *ptr1, *ptr2; |
264 | |
264 | |
265 | if (str1==NULL) |
265 | if (str1==NULL) |
266 | return NULL; |
266 | return NULL; |
… | |
… | |
283 | /* |
283 | /* |
284 | * Nice fake of the real (non-standard) one |
284 | * Nice fake of the real (non-standard) one |
285 | */ |
285 | */ |
286 | |
286 | |
287 | char * TOOLEXPORT |
287 | char * TOOLEXPORT |
288 | FP_strrstr (char *ptr, char *str) |
288 | _FP_strrstr (char *ptr, char *str) |
289 | { |
289 | { |
290 | char *found=NULL, *new, *iter=ptr; |
290 | char *found=NULL, *new, *iter=ptr; |
291 | |
291 | |
292 | if (ptr==NULL || str==NULL) |
292 | if (ptr==NULL || str==NULL) |
293 | return NULL; |
293 | return NULL; |
294 | |
294 | |
295 | if (*str == '\0') |
295 | if (*str == '\0') |
296 | return ptr; |
296 | return ptr; |
297 | |
297 | |
298 | while ((new = FP_strstr (iter, str)) != NULL) { |
298 | while ((new = _FP_strstr (iter, str)) != NULL) { |
299 | found = new; |
299 | found = new; |
300 | iter = new + 1; |
300 | iter = new + 1; |
301 | } |
301 | } |
302 | return found; |
302 | return found; |
303 | } |
303 | } |
304 | |
304 | |
305 | char * TOOLEXPORT |
305 | char * TOOLEXPORT |
306 | FP_strirstr (char *ptr, char *str) |
306 | _FP_strirstr (char *ptr, char *str) |
307 | { |
307 | { |
308 | char *found=NULL, *iter=ptr, *new; |
308 | char *found=NULL, *iter=ptr, *new; |
309 | |
309 | |
310 | if (ptr==NULL || str==NULL) |
310 | if (ptr==NULL || str==NULL) |
311 | return NULL; |
311 | return NULL; |
312 | if (*str == '\0') |
312 | if (*str == '\0') |
313 | return ptr; |
313 | return ptr; |
314 | |
314 | |
315 | while ((new = FP_stristr (iter, str)) != NULL) { |
315 | while ((new = _FP_stristr (iter, str)) != NULL) { |
316 | found = new; |
316 | found = new; |
317 | iter = new + 1; |
317 | iter = new + 1; |
318 | } |
318 | } |
319 | return found; |
319 | return found; |
320 | } |
320 | } |
… | |
… | |
322 | /* |
322 | /* |
323 | * convert whole string to case |
323 | * convert whole string to case |
324 | */ |
324 | */ |
325 | |
325 | |
326 | char * TOOLEXPORT |
326 | char * TOOLEXPORT |
327 | FP_stoupper (char *input) |
327 | _FP_stoupper (char *input) |
328 | { |
328 | { |
329 | char *iter = input; |
329 | char *iter = input; |
330 | |
330 | |
331 | if (input == NULL) |
331 | if (input == NULL) |
332 | return NULL; |
332 | return NULL; |
… | |
… | |
337 | } |
337 | } |
338 | return input; |
338 | return input; |
339 | } |
339 | } |
340 | |
340 | |
341 | char * TOOLEXPORT |
341 | char * TOOLEXPORT |
342 | FP_stolower (char *input) |
342 | _FP_stolower (char *input) |
343 | { |
343 | { |
344 | char *iter = input; |
344 | char *iter = input; |
345 | |
345 | |
346 | if (input == NULL) |
346 | if (input == NULL) |
347 | return NULL; |
347 | return NULL; |
… | |
… | |
356 | /* |
356 | /* |
357 | * string matching with wildcards |
357 | * string matching with wildcards |
358 | */ |
358 | */ |
359 | |
359 | |
360 | int TOOLEXPORT |
360 | int TOOLEXPORT |
361 | FP_strmatch (char *string, char *pattern) |
361 | _FP_strmatch (char *string, char *pattern) |
362 | { |
362 | { |
363 | char *p1 = string, *p2 = pattern; |
363 | char *p1 = string, *p2 = pattern; |
364 | |
364 | |
365 | if (pattern==NULL || string==NULL) |
365 | if (pattern==NULL || string==NULL) |
366 | return 0; |
366 | return 0; |
… | |
… | |
386 | |
386 | |
387 | return 1; |
387 | return 1; |
388 | } |
388 | } |
389 | |
389 | |
390 | char * TOOLEXPORT |
390 | char * TOOLEXPORT |
391 | FP_strrchr (char *string, int tc) |
391 | _FP_strrchr (char *string, int tc) |
392 | { |
392 | { |
393 | char *ptr; |
393 | char *ptr; |
394 | |
394 | |
395 | if (string == NULL) |
395 | if (string == NULL) |
396 | return NULL; |
396 | return NULL; |
… | |
… | |
410 | * strip directory information from a filename. Works only on DOS and |
410 | * strip directory information from a filename. Works only on DOS and |
411 | * Unix systems so far ... |
411 | * Unix systems so far ... |
412 | */ |
412 | */ |
413 | |
413 | |
414 | char * TOOLEXPORT |
414 | char * TOOLEXPORT |
415 | FP_cutdir (char *filename) |
415 | _FP_cutdir (char *filename) |
416 | { |
416 | { |
417 | char *ptr; |
417 | char *ptr; |
418 | |
418 | |
419 | if (filename == NULL) |
419 | if (filename == NULL) |
420 | return NULL; |
420 | return NULL; |
421 | |
421 | |
422 | if ((ptr = FP_strrchr (filename, '/')) != NULL) |
422 | if ((ptr = _FP_strrchr (filename, '/')) != NULL) |
423 | ptr++; |
423 | ptr++; |
424 | else if ((ptr = FP_strrchr (filename, '\\')) != NULL) |
424 | else if ((ptr = _FP_strrchr (filename, '\\')) != NULL) |
425 | ptr++; |
425 | ptr++; |
426 | else |
426 | else |
427 | ptr = filename; |
427 | ptr = filename; |
428 | |
428 | |
429 | return ptr; |
429 | return ptr; |
… | |
… | |
434 | * properly: LF (Unix), CRLF (DOS) and CR (Mac). In all cases, the |
434 | * properly: LF (Unix), CRLF (DOS) and CR (Mac). In all cases, the |
435 | * terminator is replaced by a single LF |
435 | * terminator is replaced by a single LF |
436 | */ |
436 | */ |
437 | |
437 | |
438 | char * TOOLEXPORT |
438 | char * TOOLEXPORT |
439 | FP_fgets (char *buf, int n, FILE *stream) |
439 | _FP_fgets (char *buf, int n, FILE *stream) |
440 | { |
440 | { |
441 | char *obp = buf; |
441 | char *obp = buf; |
442 | int c; |
442 | int c; |
443 | |
443 | |
444 | if (feof (stream)) |
444 | if (feof (stream)) |
… | |
… | |
489 | /* |
489 | /* |
490 | * A replacement strerror function that just returns the error code |
490 | * A replacement strerror function that just returns the error code |
491 | */ |
491 | */ |
492 | |
492 | |
493 | char * TOOLEXPORT |
493 | char * TOOLEXPORT |
494 | FP_strerror (int errcode) |
494 | _FP_strerror (int errcode) |
495 | { |
495 | { |
496 | static char number[8]; |
496 | static char number[8]; |
497 | |
497 | |
498 | sprintf (number, "%03d", errcode); |
498 | sprintf (number, "%03d", errcode); |
499 | |
499 | |
… | |
… | |
503 | /* |
503 | /* |
504 | * tempnam is not ANSI, but tmpnam is. Ignore the prefix here. |
504 | * tempnam is not ANSI, but tmpnam is. Ignore the prefix here. |
505 | */ |
505 | */ |
506 | |
506 | |
507 | char * TOOLEXPORT |
507 | char * TOOLEXPORT |
508 | FP_tempnam (char *dir, char *pfx) |
508 | _FP_tempnam (char *dir, char *pfx) |
509 | { |
509 | { |
510 | return FP_strdup (tmpnam (NULL)); |
510 | return _FP_strdup (tmpnam (NULL)); |
511 | } |
511 | } |
512 | #endif /* HAVE_MKSTEMP */ |
512 | #endif /* HAVE_MKSTEMP */ |