2 Unix SMB/Netbios implementation.
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.
24 extern int DEBUGLEVEL
;
27 void replace_dummy(void)
31 /****************************************************************************
32 a replacement strlen() that returns int for solaris
33 ****************************************************************************/
44 /*******************************************************************
45 ftruncate for operating systems that don't have it
46 ********************************************************************/
47 int ftruncate(int f
,long l
)
55 return fcntl(f
, F_FREESP
, &fl
);
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
)
69 while ( *s
!= '\0' ) {
70 if ( strncmp(s
, p
, len
) == 0 )
77 #endif /* REPLACE_STRSTR */
81 /*******************************************************************
82 a mktime() replacement for those who don't have it - contributed by
83 C.A. Lademann <cal@zls.com>
84 ********************************************************************/
86 #define HOUR 60*MINUTE
89 time_t Mktime(struct tm
*t
)
93 int mon
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
99 epoch
= (t
->tm_year
- 70) * YEAR
+
100 (t
->tm_year
/ 4 - 70 / 4 - t
->tm_year
/ 100) * DAY
;
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))
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
;
130 memcpy(t
->tm_name
, u
->tm_name
, LTZNMAX
);
136 #endif /* REPLACE_MKTIME */
140 #ifdef REPLACE_RENAME
141 /* Rename a file. (from libiberty in GNU binutils) */
142 int rename (zfrom
, zto
)
146 if (link (zfrom
, zto
) < 0)
151 || link (zfrom
, zto
) < 0)
154 return unlink (zfrom
);
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
;
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
))) {
183 #include <sys/types.h>
188 #define NULL (void *)0
191 /****************************************************************************
192 some systems don't have an initgroups call
193 ****************************************************************************/
194 int initgroups(char *name
,gid_t id
)
197 /* yikes! no SETGROUPS or INITGROUPS? how can this work? */
200 gid_t grouplst
[NGROUPS_MAX
];
207 while (i
< NGROUPS_MAX
&&
208 ((g
= (struct group
*)getgrent()) != (struct group
*)NULL
))
214 while (gr
&& (*gr
!= (char)NULL
)) {
215 if (strcmp(name
,gr
) == 0) {
216 grouplst
[i
] = g
->gr_gid
;
225 return(setgroups(i
,grouplst
));
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
);
244 /* undo the wrapping temporarily */
249 /****************************************************************************
250 wrapper for malloc() to catch memory errors
251 ****************************************************************************/
252 void *malloc_wrapped(int size
,char *file
,int line
)
255 void *res
= xx_old_malloc(size
);
257 void *res
= malloc(size
);
259 DEBUG(3,("Malloc called from %s(%d) with size=%d gave ptr=0x%X\n",
261 size
,(unsigned int)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
);
273 void *res
= realloc(ptr
,size
);
275 DEBUG(3,("Realloc\n"));
276 DEBUG(3,("free called from %s(%d) with ptr=0x%X\n",
279 DEBUG(3,("Malloc called from %s(%d) with size=%d gave ptr=0x%X\n",
281 size
,(unsigned int)res
));
285 /****************************************************************************
286 wrapper for free() to catch memory errors
287 ****************************************************************************/
288 void free_wrapped(void *ptr
,char *file
,int line
)
295 DEBUG(3,("free called from %s(%d) with ptr=0x%X\n",
296 file
,line
,(unsigned int)ptr
));
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__)
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
));
318 return(xx_old_memcpy(d
,s
,l
));
320 return(memcpy(d
,s
,l
));
323 #define memcpy(d,s,l) memcpy_wrapped(d,s,l,__FILE__,__LINE__)