ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-UUlib/uulib/fptools.c
Revision: 1.15
Committed: Fri Dec 11 21:59:30 2020 UTC (3 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +6 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     * fptools.c, some helper functions for getcgi.c and uu(en|de)view
3     *
4 root 1.2 * Distributed under the terms of the GNU General Public License.
5     * Use and be happy.
6 root 1.1 */
7    
8     #ifdef HAVE_CONFIG_H
9     #include "config.h"
10     #endif
11    
12     #ifdef SYSTEM_WINDLL
13     #include <windows.h>
14     #endif
15     #ifdef SYSTEM_OS2
16     #include <os2.h>
17     #endif
18    
19     /*
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
22     * should behave exactly as their counterparts. There are also extensions
23     * that aren't portable at all (like strirstr etc.).
24     * The proper behaviour in a configure script is as follows:
25 root 1.13 * AC_CHECK_FUNC(strrchr,AC_DEFINE(strrchr,FP_strrchr))
26 root 1.1 * 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
28     * does not work with replacements that just shadow wrong behaviour (like
29 root 1.13 * FP_free) or provide extended functionality (FP_gets).
30 root 1.1 * The above is not used in the uuenview/uudeview configuration script,
31     * since both only use the replacement functions in non-performance-cri-
32 root 1.13 * tical sections (except for FP_tempnam and FP_strerror, where some
33 root 1.1 * functionality of the original would be lost).
34     */
35    
36     #include <stdio.h>
37     #include <ctype.h>
38    
39     #ifdef STDC_HEADERS
40     #include <stdlib.h>
41     #include <string.h>
42     #endif
43     #ifdef HAVE_MALLOC_H
44     #include <malloc.h>
45     #endif
46     #ifdef HAVE_UNISTD_H
47     #include <unistd.h>
48     #endif
49     #ifdef HAVE_MEMORY_H
50     #include <memory.h>
51     #endif
52    
53     #include <fptools.h>
54    
55     #if 0
56     #ifdef SYSTEM_WINDLL
57     BOOL _export WINAPI
58     DllEntryPoint (HINSTANCE hInstance, DWORD seginfo,
59     LPVOID lpCmdLine)
60     {
61     /* Don't do anything, so just return true */
62     return TRUE;
63     }
64     #endif
65     #endif
66    
67 root 1.7 char * fptools_id = "$Id$";
68 root 1.1
69     /*
70     * some versions of free can't handle a NULL pointer properly
71     * (ANSI says, free ignores a NULL pointer, but some machines
72     * prefer to SIGSEGV on it)
73     */
74    
75     void TOOLEXPORT
76 root 1.13 FP_free (void *ptr)
77 root 1.1 {
78     if (ptr) free (ptr);
79     }
80    
81     /*
82     * This is non-standard, so I'm defining my own
83     */
84    
85     char * TOOLEXPORT
86 root 1.13 FP_strdup (char *string)
87 root 1.1 {
88     char *result;
89    
90     if (string == NULL)
91     return NULL;
92    
93     if ((result = (char *) malloc (strlen (string) + 1)) == NULL)
94     return NULL;
95    
96     strcpy (result, string);
97     return result;
98     }
99    
100     /*
101     * limited-length string copy. this function behaves differently from
102     * the original in that the dest string is always terminated with a
103     * NULL character.
104     */
105    
106     char * TOOLEXPORT
107 root 1.13 FP_strncpy (char *dest, char *src, int length)
108 root 1.1 {
109     char *odest=dest;
110     if (src == NULL || dest == NULL || length-- <= 0)
111     return dest;
112    
113     while (length-- && *src)
114     *dest++ = *src++;
115    
116     *dest++ = '\0';
117     return odest;
118     }
119    
120     /*
121     * duplicate a memory area
122     */
123    
124     void * TOOLEXPORT
125 root 1.14 FP_memdup (const void *ptr, int len)
126 root 1.1 {
127     void *result;
128    
129     if (ptr == NULL)
130     return NULL;
131    
132     if ((result = malloc (len)) == NULL)
133     return NULL;
134    
135     memcpy (result, ptr, len);
136     return result;
137     }
138    
139     /*
140     * case-insensitive compare
141     */
142    
143 root 1.10 #ifndef FP_stricmp
144 root 1.1 int TOOLEXPORT
145 root 1.13 FP_stricmp (const char *str1, const char *str2)
146 root 1.1 {
147     if (str1==NULL || str2==NULL)
148     return -1;
149    
150     while (*str1) {
151     if (tolower(*str1) != tolower(*str2))
152     break;
153     str1++;
154     str2++;
155     }
156     return (tolower (*str1) - tolower (*str2));
157     }
158 root 1.10 #endif
159 root 1.1
160 root 1.10 #ifndef FP_strnicmp
161 root 1.1 int TOOLEXPORT
162 root 1.13 FP_strnicmp (const char *str1, const char *str2, int count)
163 root 1.1 {
164     if (str1==NULL || str2==NULL)
165     return -1;
166    
167     while (*str1 && count) {
168     if (tolower(*str1) != tolower(*str2))
169     break;
170     str1++;
171     str2++;
172     count--;
173     }
174     return count ? (tolower (*str1) - tolower (*str2)) : 0;
175     }
176 root 1.10 #endif
177 root 1.1
178 root 1.14 int TOOLEXPORT
179     FP_strnicmp_fast (const char *str1, const char *str2, int count)
180     {
181 root 1.15 if (!str1 || !str2)
182 root 1.14 return -1;
183    
184     while (*str1 && count) {
185     if ((*str1 ^ *str2) & 0xdf)
186     break;
187    
188     str1++;
189     str2++;
190     count--;
191     }
192    
193     return (*str1 & 0xdf) - (*str2 & 0xdf);
194     }
195    
196 root 1.1 char * TOOLEXPORT
197 root 1.13 FP_strpbrk (char *str, char *accept)
198 root 1.1 {
199     char *ptr;
200    
201     if (str == NULL)
202     return NULL;
203     if (accept == NULL || *accept == '\0')
204     return str;
205    
206     for (; *str; str++)
207     for (ptr=accept; *ptr; ptr++)
208     if (*str == *ptr)
209     return str;
210    
211     return NULL;
212     }
213    
214     /*
215     * autoconf also complains about this one
216     */
217    
218     char * TOOLEXPORT
219 root 1.13 FP_strtok (char *str1, char *str2)
220 root 1.1 {
221     static char *optr;
222     char *ptr;
223    
224     if (str2 == NULL)
225     return NULL;
226    
227     if (str1) {
228     optr = str1;
229     }
230     else {
231     if (*optr == '\0')
232     return NULL;
233     }
234    
235     while (*optr && strchr (str2, *optr)) /* look for beginning of token */
236     optr++;
237    
238     if (*optr == '\0') /* no token found */
239     return NULL;
240    
241     ptr = optr;
242     while (*optr && strchr (str2, *optr) == NULL) /* look for end of token */
243     optr++;
244    
245     if (*optr) {
246     *optr++ = '\0';
247     }
248     return ptr;
249     }
250    
251     /*
252     * case insensitive strstr.
253     */
254    
255 root 1.10 #ifndef FP_stristr
256 root 1.1 char * TOOLEXPORT
257 root 1.13 FP_stristr (char *str1, char *str2)
258 root 1.1 {
259     char *ptr1, *ptr2;
260    
261     if (str1==NULL)
262     return NULL;
263     if (str2==NULL)
264     return str1;
265    
266     while (*(ptr1=str1)) {
267     for (ptr2=str2;
268     *ptr1 && *ptr2 && tolower(*ptr1)==tolower(*ptr2);
269     ptr1++, ptr2++)
270     /* empty loop */ ;
271    
272     if (*ptr2 == '\0')
273     return str1;
274     str1++;
275     }
276     return NULL;
277     }
278 root 1.10 #endif
279 root 1.1
280     /*
281     * Nice fake of the real (non-standard) one
282     */
283    
284     char * TOOLEXPORT
285 root 1.15 FP_strrstr (const char *ptr, const char *str)
286 root 1.1 {
287 root 1.15 const char *found=NULL, *new, *iter=ptr;
288 root 1.1
289     if (ptr==NULL || str==NULL)
290     return NULL;
291    
292     if (*str == '\0')
293 root 1.15 return (char *)ptr;
294 root 1.1
295 root 1.13 while ((new = FP_strstr (iter, str)) != NULL) {
296 root 1.1 found = new;
297     iter = new + 1;
298     }
299 root 1.15
300     return (char *)found;
301 root 1.1 }
302    
303     char * TOOLEXPORT
304 root 1.13 FP_strirstr (char *ptr, char *str)
305 root 1.1 {
306     char *found=NULL, *iter=ptr, *new;
307    
308     if (ptr==NULL || str==NULL)
309     return NULL;
310     if (*str == '\0')
311     return ptr;
312    
313 root 1.13 while ((new = FP_stristr (iter, str)) != NULL) {
314 root 1.1 found = new;
315     iter = new + 1;
316     }
317     return found;
318     }
319    
320     /*
321     * convert whole string to case
322     */
323    
324     char * TOOLEXPORT
325 root 1.13 FP_stoupper (char *input)
326 root 1.1 {
327     char *iter = input;
328    
329     if (input == NULL)
330     return NULL;
331    
332     while (*iter) {
333     *iter = toupper (*iter);
334     iter++;
335     }
336     return input;
337     }
338    
339     char * TOOLEXPORT
340 root 1.13 FP_stolower (char *input)
341 root 1.1 {
342     char *iter = input;
343    
344     if (input == NULL)
345     return NULL;
346    
347     while (*iter) {
348     *iter = tolower (*iter);
349     iter++;
350     }
351     return input;
352     }
353    
354     /*
355     * string matching with wildcards
356     */
357    
358     int TOOLEXPORT
359 root 1.13 FP_strmatch (char *string, char *pattern)
360 root 1.1 {
361     char *p1 = string, *p2 = pattern;
362    
363     if (pattern==NULL || string==NULL)
364     return 0;
365    
366     while (*p1 && *p2) {
367     if (*p2 == '?') {
368     p1++; p2++;
369     }
370     else if (*p2 == '*') {
371     if (*++p2 == '\0')
372     return 1;
373     while (*p1 && *p1 != *p2)
374     p1++;
375     }
376     else if (*p1 == *p2) {
377     p1++; p2++;
378     }
379     else
380     return 0;
381     }
382     if (*p1 || *p2)
383     return 0;
384    
385     return 1;
386     }
387    
388     char * TOOLEXPORT
389 root 1.14 FP_strrchr (const char *string, int tc)
390 root 1.1 {
391 root 1.14 const char *ptr;
392 root 1.1
393 root 1.7 if (string == NULL || !*string)
394 root 1.1 return NULL;
395    
396     ptr = string + strlen (string) - 1;
397    
398     while (ptr != string && *ptr != tc)
399     ptr--;
400    
401     if (*ptr == tc)
402 root 1.14 return (char *)ptr;
403 root 1.1
404     return NULL;
405     }
406    
407     /*
408     * strip directory information from a filename. Works only on DOS and
409     * Unix systems so far ...
410     */
411    
412     char * TOOLEXPORT
413 root 1.13 FP_cutdir (char *filename)
414 root 1.1 {
415     char *ptr;
416    
417     if (filename == NULL)
418     return NULL;
419    
420 root 1.13 if ((ptr = FP_strrchr (filename, '/')) != NULL)
421 root 1.1 ptr++;
422 root 1.13 else if ((ptr = FP_strrchr (filename, '\\')) != NULL)
423 root 1.1 ptr++;
424     else
425     ptr = filename;
426    
427     return ptr;
428     }
429    
430     /*
431     * My own fgets function. It handles all kinds of line terminators
432 root 1.9 * properly: LF (Unix), CRLF (DOS) and CR (Mac).
433 root 1.1 */
434 root 1.9 /* (schmorp) the buffer is always written to, and no LF is stored at the end */
435 root 1.12 /* also, if the buffer is too short, the remaining line is skipped */
436 root 1.1 char * TOOLEXPORT
437 root 1.13 FP_fgets (char *buf, int n, FILE *stream)
438 root 1.1 {
439 root 1.12 char *ptr = buf;
440     char *end = buf + n - 1;
441 root 1.6
442     /* shield against buffer overflows caused by "255 - bytes_left"-kind of bugs when bytes_left > 255 */
443     if (n <= 0)
444 root 1.12 return 0;
445 root 1.1
446 root 1.9 for (;;)
447     {
448 root 1.13 int c = FP_getc (stream);
449 root 1.9
450 root 1.12 if (ecb_expect_false (c <= '\015')) /* EOF is < 0x20, too */
451 root 1.9 {
452 root 1.12 /* ctlchar */
453 root 1.9
454 root 1.12 if (c == '\012')
455     /* LF, nothing following */
456     break;
457     else if (c == '\015')
458     {
459     /* CR, possibly CRLF, skip following LF */
460 root 1.13 c = FP_getc (stream);
461 root 1.12
462     if (c != '\012') /* CR LF? */
463     ungetc (c, stream);
464    
465     break;
466     }
467     else if (c == EOF)
468     {
469     *ptr = 0;
470     return 0;
471     }
472 root 1.9 }
473 root 1.1
474 root 1.12 *ptr = c;
475     ptr += ptr < end; /* this is hopefully branch-free, and fast */
476 root 1.1 }
477 root 1.12
478     *ptr = 0;
479     return buf;
480 root 1.1 }
481    
482     /*
483     * A replacement strerror function that just returns the error code
484     */
485    
486     char * TOOLEXPORT
487 root 1.13 FP_strerror (int errcode)
488 root 1.1 {
489     static char number[8];
490    
491     sprintf (number, "%03d", errcode);
492    
493     return number;
494     }
495 root 1.3 #ifndef HAVE_MKSTEMP
496 root 1.1 /*
497     * tempnam is not ANSI, but tmpnam is. Ignore the prefix here.
498     */
499    
500     char * TOOLEXPORT
501 root 1.13 FP_tempnam (char *dir, char *pfx)
502 root 1.1 {
503 root 1.13 return FP_strdup (tmpnam (NULL));
504 root 1.1 }
505 root 1.3 #endif /* HAVE_MKSTEMP */