This is the ubiqx binary tree and linked list library.
[Samba.git] / source / lib / replace.c
blob67c18a15237b52e398ecb0076c96abc1682445cb
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 replacement routines for broken systems
5 Copyright (C) Andrew Tridgell 1992-1997
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program 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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 extern int DEBUGLEVEL;
27 void replace_dummy(void)
30 #ifdef REPLACE_STRLEN
31 /****************************************************************************
32 a replacement strlen() that returns int for solaris
33 ****************************************************************************/
34 int Strlen(char *s)
36 int ret=0;
37 if (!s) return(0);
38 while (*s++) ret++;
39 return(ret);
41 #endif
43 #ifdef NO_FTRUNCATE
44 /*******************************************************************
45 ftruncate for operating systems that don't have it
46 ********************************************************************/
47 int ftruncate(int f,long l)
49 struct flock fl;
51 fl.l_whence = 0;
52 fl.l_len = 0;
53 fl.l_start = l;
54 fl.l_type = F_WRLCK;
55 return fcntl(f, F_FREESP, &fl);
57 #endif
60 #ifdef REPLACE_STRSTR
61 /****************************************************************************
62 Mips version of strstr doesn't seem to work correctly.
63 There is a #define in includes.h to redirect calls to this function.
64 ****************************************************************************/
65 char *Strstr(char *s, char *p)
67 int len = strlen(p);
69 while ( *s != '\0' ) {
70 if ( strncmp(s, p, len) == 0 )
71 return s;
72 s++;
75 return NULL;
77 #endif /* REPLACE_STRSTR */
80 #ifdef REPLACE_MKTIME
81 /*******************************************************************
82 a mktime() replacement for those who don't have it - contributed by
83 C.A. Lademann <cal@zls.com>
84 ********************************************************************/
85 #define MINUTE 60
86 #define HOUR 60*MINUTE
87 #define DAY 24*HOUR
88 #define YEAR 365*DAY
89 time_t Mktime(struct tm *t)
91 struct tm *u;
92 time_t epoch = 0;
93 int mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
94 y, m, i;
96 if(t->tm_year < 70)
97 return((time_t)-1);
99 epoch = (t->tm_year - 70) * YEAR +
100 (t->tm_year / 4 - 70 / 4 - t->tm_year / 100) * DAY;
102 y = t->tm_year;
103 m = 0;
105 for(i = 0; i < t->tm_mon; i++) {
106 epoch += mon [m] * DAY;
107 if(m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
108 epoch += DAY;
110 if(++m > 11) {
111 m = 0;
112 y++;
116 epoch += (t->tm_mday - 1) * DAY;
117 epoch += t->tm_hour * HOUR + t->tm_min * MINUTE + t->tm_sec;
119 if((u = localtime(&epoch)) != NULL) {
120 t->tm_sec = u->tm_sec;
121 t->tm_min = u->tm_min;
122 t->tm_hour = u->tm_hour;
123 t->tm_mday = u->tm_mday;
124 t->tm_mon = u->tm_mon;
125 t->tm_year = u->tm_year;
126 t->tm_wday = u->tm_wday;
127 t->tm_yday = u->tm_yday;
128 t->tm_isdst = u->tm_isdst;
129 #ifndef NO_TM_NAME
130 memcpy(t->tm_name, u->tm_name, LTZNMAX);
131 #endif
134 return(epoch);
136 #endif /* REPLACE_MKTIME */
140 #ifdef REPLACE_RENAME
141 /* Rename a file. (from libiberty in GNU binutils) */
142 int rename (zfrom, zto)
143 const char *zfrom;
144 const char *zto;
146 if (link (zfrom, zto) < 0)
148 if (errno != EEXIST)
149 return -1;
150 if (unlink (zto) < 0
151 || link (zfrom, zto) < 0)
152 return -1;
154 return unlink (zfrom);
156 #endif
159 #ifdef REPLACE_INNETGR
161 * Search for a match in a netgroup. This replaces it on broken systems.
163 int InNetGr(char *group,char *host,char *user,char *dom)
165 char *hst, *usr, *dm;
167 setnetgrent(group);
168 while (getnetgrent(&hst, &usr, &dm))
169 if (((host == 0) || (hst == 0) || !strcmp(host, hst)) &&
170 ((user == 0) || (usr == 0) || !strcmp(user, usr)) &&
171 ((dom == 0) || (dm == 0) || !strcmp(dom, dm))) {
172 endnetgrent();
173 return (1);
175 endnetgrent();
176 return (0);
178 #endif
182 #ifdef NO_INITGROUPS
183 #include <sys/types.h>
184 #include <limits.h>
185 #include <grp.h>
187 #ifndef NULL
188 #define NULL (void *)0
189 #endif
191 /****************************************************************************
192 some systems don't have an initgroups call
193 ****************************************************************************/
194 int initgroups(char *name,gid_t id)
196 #ifdef NO_SETGROUPS
197 /* yikes! no SETGROUPS or INITGROUPS? how can this work? */
198 return(0);
199 #else
200 gid_t grouplst[NGROUPS_MAX];
201 int i,j;
202 struct group *g;
203 char *gr;
205 grouplst[0] = id;
206 i = 1;
207 while (i < NGROUPS_MAX &&
208 ((g = (struct group *)getgrent()) != (struct group *)NULL))
210 if (g->gr_gid == id)
211 continue;
212 j = 0;
213 gr = g->gr_mem[0];
214 while (gr && (*gr != (char)NULL)) {
215 if (strcmp(name,gr) == 0) {
216 grouplst[i] = g->gr_gid;
217 i++;
218 gr = (char *)NULL;
219 break;
221 gr = g->gr_mem[++j];
224 endgrent();
225 return(setgroups(i,grouplst));
226 #endif
228 #endif
231 #if (defined(SecureWare) && defined(SCO))
232 /* This is needed due to needing the nap() function but we don't want
233 to include the Xenix libraries since that will break other things...
234 BTW: system call # 0x0c28 is the same as calling nap() */
235 long nap(long milliseconds) {
236 return syscall(0x0c28, milliseconds);
238 #endif
242 #if WRAP_MALLOC
244 /* undo the wrapping temporarily */
245 #undef malloc
246 #undef realloc
247 #undef free
249 /****************************************************************************
250 wrapper for malloc() to catch memory errors
251 ****************************************************************************/
252 void *malloc_wrapped(int size,char *file,int line)
254 #ifdef xx_old_malloc
255 void *res = xx_old_malloc(size);
256 #else
257 void *res = malloc(size);
258 #endif
259 DEBUG(3,("Malloc called from %s(%d) with size=%d gave ptr=0x%X\n",
260 file,line,
261 size,(unsigned int)res));
262 return(res);
265 /****************************************************************************
266 wrapper for realloc() to catch memory errors
267 ****************************************************************************/
268 void *realloc_wrapped(void *ptr,int size,char *file,int line)
270 #ifdef xx_old_realloc
271 void *res = xx_old_realloc(ptr,size);
272 #else
273 void *res = realloc(ptr,size);
274 #endif
275 DEBUG(3,("Realloc\n"));
276 DEBUG(3,("free called from %s(%d) with ptr=0x%X\n",
277 file,line,
278 (unsigned int)ptr));
279 DEBUG(3,("Malloc called from %s(%d) with size=%d gave ptr=0x%X\n",
280 file,line,
281 size,(unsigned int)res));
282 return(res);
285 /****************************************************************************
286 wrapper for free() to catch memory errors
287 ****************************************************************************/
288 void free_wrapped(void *ptr,char *file,int line)
290 #ifdef xx_old_free
291 xx_old_free(ptr);
292 #else
293 free(ptr);
294 #endif
295 DEBUG(3,("free called from %s(%d) with ptr=0x%X\n",
296 file,line,(unsigned int)ptr));
297 return;
300 /* and re-do the define for spots lower in this file */
301 #define malloc(size) malloc_wrapped(size,__FILE__,__LINE__)
302 #define realloc(ptr,size) realloc_wrapped(ptr,size,__FILE__,__LINE__)
303 #define free(ptr) free_wrapped(ptr,__FILE__,__LINE__)
305 #endif
308 #if WRAP_MEMCPY
309 #undef memcpy
310 /*******************************************************************
311 a wrapper around memcpy for diagnostic purposes
312 ********************************************************************/
313 void *memcpy_wrapped(void *d,void *s,int l,char *fname,int line)
315 if (l>64 && (((int)d)%4) != (((int)s)%4))
316 DEBUG(4,("Misaligned memcpy(0x%X,0x%X,%d) at %s(%d)\n",d,s,l,fname,line));
317 #ifdef xx_old_memcpy
318 return(xx_old_memcpy(d,s,l));
319 #else
320 return(memcpy(d,s,l));
321 #endif
323 #define memcpy(d,s,l) memcpy_wrapped(d,s,l,__FILE__,__LINE__)
324 #endif