Massively cleaned up #include's, so they're in a consistent
[freeradius.git] / src / lib / missing.c
1 /*
2  * missing.c    Replacements for functions that are or can be
3  *              missing on some platforms.
4  *
5  * Version:     $Id$
6  *
7  *   This library is free software; you can redistribute it and/or
8  *   modify it under the terms of the GNU Lesser General Public
9  *   License as published by the Free Software Foundation; either
10  *   version 2.1 of the License, or (at your option) any later version.
11  *
12  *   This library is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  *   Lesser General Public License for more details.
16  *
17  *   You should have received a copy of the GNU Lesser General Public
18  *   License along with this library; if not, write to the Free Software
19  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * Copyright 2000,2006  The FreeRADIUS server project
22  */
23
24 #include        <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include        <freeradius-devel/libradius.h>
28
29 #include        <ctype.h>
30
31 #ifndef HAVE_CRYPT
32 char *crypt(char *key, char *salt)
33 {
34         /*log(L_ERR, "crypt() called but not implemented");*/
35         return "____fnord____";
36 }
37 #endif
38
39 #ifndef HAVE_STRNCASECMP
40 int strncasecmp(char *s1, char *s2, int n)
41 {
42         int             dif;
43         unsigned char   *p1, *p2;
44         int             c1, c2;
45
46         p1 = (unsigned char *)s1;
47         p2 = (unsigned char *)s2;
48         dif = 0;
49
50         while (n != 0) {
51                 if (*p1 == 0 && *p2 == 0)
52                         break;
53                 c1 = *p1;
54                 c2 = *p2;
55
56                 if (islower(c1)) c1 = toupper(c1);
57                 if (islower(c2)) c2 = toupper(c2);
58
59                 if ((dif = c1 - c2) != 0)
60                         break;
61                 p1++;
62                 p2++;
63                 n--;
64         }
65         return dif;
66 }
67 #endif
68
69 #ifndef HAVE_STRCASECMP
70 int strcasecmp(char *s1, char *s2)
71 {
72         int             l1, l2;
73
74         l1 = strlen(s1);
75         l2 = strlen(s2);
76         if (l2 > l1) l1 = l2;
77
78         return strncasecmp(s1, s2, l1);
79 }
80 #endif
81
82 #ifndef HAVE_INET_ATON
83 int inet_aton(char *cp, struct in_addr *inp)
84 {
85         int     a1, a2, a3, a4;
86
87         if (sscanf(cp, "%d.%d.%d.%d", &a1, &a2, &a3, &a4) != 4)
88                 return 0;
89
90         inp->s_addr = htonl((a1 << 24) + (a2 << 16) + (a3 << 8) + a4);
91         return 1;
92 }
93 #endif
94
95 #ifndef HAVE_GETHOSTNAME
96 int gethostname(char *name, int len)
97 {
98         char            *h;
99
100         h = getenv("HOSTNAME");
101         if (!h || (strlen(h) + 1 > len))
102                 return -1;
103         strcpy(name, h);
104         return 0;
105 }
106 #endif
107
108 #ifndef HAVE_STRSEP
109 /*
110  *      Get next token from string *stringp, where tokens are
111  *      possibly-empty strings separated by characters from delim.
112  *
113  *      Writes NULs into the string at *stringp to end tokens.
114  *      delim need not remain constant from call to call.  On
115  *      return, *stringp points past the last NUL written (if there
116  *      might be further tokens), or is NULL (if there are
117  *      definitely no more tokens).
118  *
119  *      If *stringp is NULL, strsep returns NULL.
120  */
121 char *
122 strsep(char **stringp, const char *delim)
123 {
124         char *s;
125         const char *spanp;
126         int c, sc;
127         char *tok;
128
129         if ((s = *stringp) == NULL)
130                 return (NULL);
131
132         for (tok = s;;) {
133                 c = *s++;
134                 spanp = delim;
135                 do {
136                         if ((sc = *spanp++) == c) {
137                                 if (c == 0)
138                                         s = NULL;
139                                 else
140                                         s[-1] = 0;
141                                 *stringp = s;
142                                 return (tok);
143                         }
144                 } while (sc != 0);
145         }
146
147         return NULL;            /* NOTREACHED, but the compiler complains */
148 }
149 #endif
150
151 #ifndef HAVE_LOCALTIME_R
152 /*
153  *      We use localtime_r() by default in the server.
154  *
155  *      For systems which do NOT have localtime_r(), we make the
156  *      assumption that localtime() is re-entrant, and returns a
157  *      per-thread data structure.
158  *
159  *      Even if localtime is NOT re-entrant, this function will
160  *      lower the possibility of race conditions.
161  */
162 struct tm *localtime_r(const time_t *l_clock, struct tm *result)
163 {
164   memcpy(result, localtime(l_clock), sizeof(*result));
165
166   return result;
167 }
168 #endif
169
170 #ifndef HAVE_CTIME_R
171 /*
172  *      We use ctime_r() by default in the server.
173  *
174  *      For systems which do NOT have ctime_r(), we make the
175  *      assumption that ctime() is re-entrant, and returns a
176  *      per-thread data structure.
177  *
178  *      Even if ctime is NOT re-entrant, this function will
179  *      lower the possibility of race conditions.
180  */
181 char *ctime_r(const time_t *l_clock, char *l_buf)
182 {
183   strcpy(l_buf, ctime(l_clock));
184
185   return l_buf;
186 }
187 #endif
188
189 #ifndef HAVE_GMTIME_R
190 /*
191  *      We use gmtime_r() by default in the server.
192  *
193  *      For systems which do NOT have gmtime_r(), we make the
194  *      assumption that gmtime() is re-entrant, and returns a
195  *      per-thread data structure.
196  *
197  *      Even if gmtime is NOT re-entrant, this function will
198  *      lower the possibility of race conditions.
199  */
200 struct tm *gmtime_r(const time_t *l_clock, struct tm *result)
201 {
202   memcpy(result, gmtime(l_clock), sizeof(*result));
203
204   return result;
205 }
206 #endif