ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-UUlib/uulib/fptools.c
Revision: 1.14
Committed: Fri Dec 11 20:27:00 2020 UTC (3 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +22 -4 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     if (str1==NULL || str2==NULL)
182     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.13 FP_strrstr (char *ptr, char *str)
286 root 1.1 {
287     char *found=NULL, *new, *iter=ptr;
288    
289     if (ptr==NULL || str==NULL)
290     return NULL;
291    
292     if (*str == '\0')
293     return ptr;
294    
295 root 1.13 while ((new = FP_strstr (iter, str)) != NULL) {
296 root 1.1 found = new;
297     iter = new + 1;
298     }
299     return found;
300     }
301    
302     char * TOOLEXPORT
303 root 1.13 FP_strirstr (char *ptr, char *str)
304 root 1.1 {
305     char *found=NULL, *iter=ptr, *new;
306    
307     if (ptr==NULL || str==NULL)
308     return NULL;
309     if (*str == '\0')
310     return ptr;
311    
312 root 1.13 while ((new = FP_stristr (iter, str)) != NULL) {
313 root 1.1 found = new;
314     iter = new + 1;
315     }
316     return found;
317     }
318    
319     /*
320     * convert whole string to case
321     */
322    
323     char * TOOLEXPORT
324 root 1.13 FP_stoupper (char *input)
325 root 1.1 {
326     char *iter = input;
327    
328     if (input == NULL)
329     return NULL;
330    
331     while (*iter) {
332     *iter = toupper (*iter);
333     iter++;
334     }
335     return input;
336     }
337    
338     char * TOOLEXPORT
339 root 1.13 FP_stolower (char *input)
340 root 1.1 {
341     char *iter = input;
342    
343     if (input == NULL)
344     return NULL;
345    
346     while (*iter) {
347     *iter = tolower (*iter);
348     iter++;
349     }
350     return input;
351     }
352    
353     /*
354     * string matching with wildcards
355     */
356    
357     int TOOLEXPORT
358 root 1.13 FP_strmatch (char *string, char *pattern)
359 root 1.1 {
360     char *p1 = string, *p2 = pattern;
361    
362     if (pattern==NULL || string==NULL)
363     return 0;
364    
365     while (*p1 && *p2) {
366     if (*p2 == '?') {
367     p1++; p2++;
368     }
369     else if (*p2 == '*') {
370     if (*++p2 == '\0')
371     return 1;
372     while (*p1 && *p1 != *p2)
373     p1++;
374     }
375     else if (*p1 == *p2) {
376     p1++; p2++;
377     }
378     else
379     return 0;
380     }
381     if (*p1 || *p2)
382     return 0;
383    
384     return 1;
385     }
386    
387     char * TOOLEXPORT
388 root 1.14 FP_strrchr (const char *string, int tc)
389 root 1.1 {
390 root 1.14 const char *ptr;
391 root 1.1
392 root 1.7 if (string == NULL || !*string)
393 root 1.1 return NULL;
394    
395     ptr = string + strlen (string) - 1;
396    
397     while (ptr != string && *ptr != tc)
398     ptr--;
399    
400     if (*ptr == tc)
401 root 1.14 return (char *)ptr;
402 root 1.1
403     return NULL;
404     }
405    
406     /*
407     * strip directory information from a filename. Works only on DOS and
408     * Unix systems so far ...
409     */
410    
411     char * TOOLEXPORT
412 root 1.13 FP_cutdir (char *filename)
413 root 1.1 {
414     char *ptr;
415    
416     if (filename == NULL)
417     return NULL;
418    
419 root 1.13 if ((ptr = FP_strrchr (filename, '/')) != NULL)
420 root 1.1 ptr++;
421 root 1.13 else if ((ptr = FP_strrchr (filename, '\\')) != NULL)
422 root 1.1 ptr++;
423     else
424     ptr = filename;
425    
426     return ptr;
427     }
428    
429     /*
430     * My own fgets function. It handles all kinds of line terminators
431 root 1.9 * properly: LF (Unix), CRLF (DOS) and CR (Mac).
432 root 1.1 */
433 root 1.9 /* (schmorp) the buffer is always written to, and no LF is stored at the end */
434 root 1.12 /* also, if the buffer is too short, the remaining line is skipped */
435 root 1.1 char * TOOLEXPORT
436 root 1.13 FP_fgets (char *buf, int n, FILE *stream)
437 root 1.1 {
438 root 1.12 char *ptr = buf;
439     char *end = buf + n - 1;
440 root 1.6
441     /* shield against buffer overflows caused by "255 - bytes_left"-kind of bugs when bytes_left > 255 */
442     if (n <= 0)
443 root 1.12 return 0;
444 root 1.1
445 root 1.9 for (;;)
446     {
447 root 1.13 int c = FP_getc (stream);
448 root 1.9
449 root 1.12 if (ecb_expect_false (c <= '\015')) /* EOF is < 0x20, too */
450 root 1.9 {
451 root 1.12 /* ctlchar */
452 root 1.9
453 root 1.12 if (c == '\012')
454     /* LF, nothing following */
455     break;
456     else if (c == '\015')
457     {
458     /* CR, possibly CRLF, skip following LF */
459 root 1.13 c = FP_getc (stream);
460 root 1.12
461     if (c != '\012') /* CR LF? */
462     ungetc (c, stream);
463    
464     break;
465     }
466     else if (c == EOF)
467     {
468     *ptr = 0;
469     return 0;
470     }
471 root 1.9 }
472 root 1.1
473 root 1.12 *ptr = c;
474     ptr += ptr < end; /* this is hopefully branch-free, and fast */
475 root 1.1 }
476 root 1.12
477     *ptr = 0;
478     return buf;
479 root 1.1 }
480    
481     /*
482     * A replacement strerror function that just returns the error code
483     */
484    
485     char * TOOLEXPORT
486 root 1.13 FP_strerror (int errcode)
487 root 1.1 {
488     static char number[8];
489    
490     sprintf (number, "%03d", errcode);
491    
492     return number;
493     }
494 root 1.3 #ifndef HAVE_MKSTEMP
495 root 1.1 /*
496     * tempnam is not ANSI, but tmpnam is. Ignore the prefix here.
497     */
498    
499     char * TOOLEXPORT
500 root 1.13 FP_tempnam (char *dir, char *pfx)
501 root 1.1 {
502 root 1.13 return FP_strdup (tmpnam (NULL));
503 root 1.1 }
504 root 1.3 #endif /* HAVE_MKSTEMP */