1 /* Missing Functions
2  *
3  * The functions included here are provided as replacements for system
4  * library functions that are missing (or broken).
5  *
6  * Yes, configure usually tries to get you to have a single file for
7  * each function, but pppbbbttthhhhh.
8  *
9  * To my knowledge, each of these replacement functions are available as
10  * free code... if not, I'll have to steal them from somewhere else that
11  * is free (hmm, FreeBSD libc?)
12  */
13 
14 #include "cs_config.h"
15 
16 #ifndef HAVE_STRTOK_R
17 #include <string.h>
18 
19 /* from glibc */
20 /* Parse S into tokens separated by characters in DELIM.
21    If S is NULL, the saved pointer in SAVE_PTR is used as
22    the next starting point.  For example:
23      char s[] = "-abc-=-def";
24      char *sp;
25      x = strtok_r(s, "-", &sp);  // x = "abc", sp = "=-def"
26      x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
27      x = strtok_r(NULL, "=", &sp);   // x = NULL
28           // s = "abc\0-def\0"
29 */
30 
strtok_r(char * s,const char * delim,char ** save_ptr)31 char * strtok_r (char *s,const char * delim, char **save_ptr)
32 {
33   char *token;
34 
35   if (s == NULL)
36     s = *save_ptr;
37 
38   /* Scan leading delimiters.  */
39   s += strspn (s, delim);
40   if (*s == '\0')
41   {
42     *save_ptr = s;
43     return NULL;
44   }
45 
46   /* Find the end of the token.  */
47   token = s;
48   s = strpbrk (token, delim);
49   if (s == NULL)
50     /* This token finishes the string.  */
51     /**save_ptr = __rawmemchr (token, '\0');*/
52     *save_ptr = strchr (token, '\0');
53   else
54   {
55     /* Terminate the token and make
56      * *SAVE_PTR point past it.  */
57     *s = '\0';
58     *save_ptr = s + 1;
59   }
60   return token;
61 }
62 #endif
63 
64 #include <time.h>
65 
66 #ifndef HAVE_LOCALTIME_R
67 
localtime_r(const time_t * timep,struct tm * ttm)68 struct tm *localtime_r (const time_t *timep, struct tm *ttm)
69 {
70   ttm = localtime(timep);
71   return ttm;
72 }
73 #endif
74 
75 #ifndef HAVE_GMTIME_R
gmtime_r(const time_t * timep,struct tm * ttm)76 struct tm *gmtime_r(const time_t *timep, struct tm *ttm)
77 {
78   ttm = gmtime(timep);
79   return ttm;
80 }
81 
82 #endif
83 
84 #ifndef HAVE_MKSTEMP
85 #include <fcntl.h>
86 
mkstemp(char * path)87 int mkstemp(char *path)
88 {
89   return open(mktemp(path),O_RDWR);
90 }
91 #endif
92