Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/liblcdf/fixlibc.c

raw
1 /* Provide definitions for missing or incorrect libc functions. */
2
3 #ifdef HAVE_CONFIG_H
4 # include <config.h>
5 #endif
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 #if !HAVE_STRDUP
14 char *
15 strdup(const char *s)
16 {
17 char *t;
18 int l;
19 if (!s)
20 return 0;
21 l = strlen(s) + 1;
22 t = (char *)malloc(l);
23 if (!t)
24 return 0;
25 memcpy(t, s, l);
26 return t;
27 }
28 #endif
29
30 #if !HAVE_STRERROR
31 /* David Mazieres <[email protected]> assures me that this definition works. */
32 char *
33 strerror(int errno)
34 {
35 extern int sys_nerr;
36 extern char *sys_errlist[];
37 if (errno < 0 || errno >= sys_nerr)
38 return (char *)"bad error number";
39 else
40 return sys_errlist[errno];
41 }
42 #endif
43
44 #if HAVE_BROKEN_STRTOD
45 /* On NeXTSTEP, Melissa O'Neill <[email protected]> reports that strtod
46 consumes whitespace after its argument, which makes mminstance (among other
47 programs) not work. This wrapper gets rid of that whitespace again.
48 (Originally, we suspected strtol too, but it seems to work, at least in
49 NeXTSTEP 3.3 patch 2.) */
50
51 double
52 good_strtod(const char *nptr, char **endptr)
53 {
54 double d = strtod(nptr, endptr);
55 if (endptr)
56 while (*endptr > nptr && isspace((unsigned char) (*endptr)[-1]))
57 (*endptr)--;
58 return d;
59 }
60 #endif
61
62 #if !HAVE_STRNLEN || HAVE_BROKEN_STRNLEN
63 size_t
64 strnlen(const char *s, size_t maxlen)
65 {
66 const char *p = (const char *) memchr(s, 0, maxlen);
67 return p ? p - s : maxlen;
68 }
69 #endif
70
71 #ifdef __cplusplus
72 }
73 #endif