2 Unix SMB/Netbios implementation.
4 replacement routines for broken systems
5 Copyright (C) Andrew Tridgell 1992-1995
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 void replace_dummy(void)
28 /****************************************************************************
29 a replacement strlen() that returns int for solaris
30 ****************************************************************************/
41 /*******************************************************************
42 ftruncate for operating systems that don't have it
43 ********************************************************************/
44 int ftruncate(int f
,long l
)
52 return fcntl(f
, F_FREESP
, &fl
);
58 /****************************************************************************
59 Mips version of strstr doesn't seem to work correctly.
60 There is a #define in includes.h to redirect calls to this function.
61 ****************************************************************************/
62 char *Strstr(char *s
, char *p
)
66 while ( *s
!= '\0' ) {
67 if ( strncmp(s
, p
, len
) == 0 )
74 #endif /* REPLACE_STRSTR */
78 /*******************************************************************
79 a mktime() replacement for those who don't have it - contributed by
80 C.A. Lademann <cal@zls.com>
81 ********************************************************************/
83 #define HOUR 60*MINUTE
86 time_t Mktime(struct tm
*t
)
90 int mon
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
96 epoch
= (t
->tm_year
- 70) * YEAR
+
97 (t
->tm_year
/ 4 - 70 / 4 - t
->tm_year
/ 100) * DAY
;
102 for(i
= 0; i
< t
->tm_mon
; i
++) {
103 epoch
+= mon
[m
] * DAY
;
104 if(m
== 1 && y
% 4 == 0 && (y
% 100 != 0 || y
% 400 == 0))
113 epoch
+= (t
->tm_mday
- 1) * DAY
;
114 epoch
+= t
->tm_hour
* HOUR
+ t
->tm_min
* MINUTE
+ t
->tm_sec
;
116 if((u
= localtime(&epoch
)) != NULL
) {
117 t
->tm_sec
= u
->tm_sec
;
118 t
->tm_min
= u
->tm_min
;
119 t
->tm_hour
= u
->tm_hour
;
120 t
->tm_mday
= u
->tm_mday
;
121 t
->tm_mon
= u
->tm_mon
;
122 t
->tm_year
= u
->tm_year
;
123 t
->tm_wday
= u
->tm_wday
;
124 t
->tm_yday
= u
->tm_yday
;
125 t
->tm_isdst
= u
->tm_isdst
;
127 memcpy(t
->tm_name
, u
->tm_name
, LTZNMAX
);
133 #endif /* REPLACE_MKTIME */
137 #ifdef REPLACE_RENAME
138 /* Rename a file. (from libiberty in GNU binutils) */
139 int rename (zfrom
, zto
)
143 if (link (zfrom
, zto
) < 0)
148 || link (zfrom
, zto
) < 0)
151 return unlink (zfrom
);
156 #ifdef REPLACE_INNETGR
158 * Search for a match in a netgroup. This replaces it on broken systems.
160 int InNetGr(char *group
,char *host
,char *user
,char *dom
)
162 char *hst
, *usr
, *dm
;
165 while (getnetgrent(&hst
, &usr
, &dm
))
166 if (((host
== 0) || (hst
== 0) || !strcmp(host
, hst
)) &&
167 ((user
== 0) || (usr
== 0) || !strcmp(user
, usr
)) &&
168 ((dom
== 0) || (dm
== 0) || !strcmp(dom
, dm
))) {
180 #include <sys/types.h>
185 #define NULL (void *)0
188 /****************************************************************************
189 some systems don't have an initgroups call
190 ****************************************************************************/
191 int initgroups(char *name
,gid_t id
)
194 /* yikes! no SETGROUPS or INITGROUPS? how can this work? */
197 gid_t grouplst
[NGROUPS_MAX
];
204 while (i
< NGROUPS_MAX
&&
205 ((g
= (struct group
*)getgrent()) != (struct group
*)NULL
))
211 while (gr
&& (*gr
!= (char)NULL
)) {
212 if (strcmp(name
,gr
) == 0) {
213 grouplst
[i
] = g
->gr_gid
;
222 return(setgroups(i
,grouplst
));
228 #if (defined(SecureWare) && defined(SCO))
229 /* This is needed due to needing the nap() function but we don't want
230 to include the Xenix libraries since that will break other things...
231 BTW: system call # 0x0c28 is the same as calling nap() */
232 long nap(long milliseconds
) {
233 return syscall(0x0c28, milliseconds
);
241 /* undo the wrapping temporarily */
246 /****************************************************************************
247 wrapper for malloc() to catch memory errors
248 ****************************************************************************/
249 void *malloc_wrapped(int size
,char *file
,int line
)
252 void *res
= xx_old_malloc(size
);
254 void *res
= malloc(size
);
256 DEBUG(3,("Malloc called from %s(%d) with size=%d gave ptr=0x%X\n",
258 size
,(unsigned int)res
));
262 /****************************************************************************
263 wrapper for realloc() to catch memory errors
264 ****************************************************************************/
265 void *realloc_wrapped(void *ptr
,int size
,char *file
,int line
)
267 #ifdef xx_old_realloc
268 void *res
= xx_old_realloc(ptr
,size
);
270 void *res
= realloc(ptr
,size
);
272 DEBUG(3,("Realloc\n"));
273 DEBUG(3,("free called from %s(%d) with ptr=0x%X\n",
276 DEBUG(3,("Malloc called from %s(%d) with size=%d gave ptr=0x%X\n",
278 size
,(unsigned int)res
));
282 /****************************************************************************
283 wrapper for free() to catch memory errors
284 ****************************************************************************/
285 void free_wrapped(void *ptr
,char *file
,int line
)
292 DEBUG(3,("free called from %s(%d) with ptr=0x%X\n",
293 file
,line
,(unsigned int)ptr
));
297 /* and re-do the define for spots lower in this file */
298 #define malloc(size) malloc_wrapped(size,__FILE__,__LINE__)
299 #define realloc(ptr,size) realloc_wrapped(ptr,size,__FILE__,__LINE__)
300 #define free(ptr) free_wrapped(ptr,__FILE__,__LINE__)
307 /*******************************************************************
308 a wrapper around memcpy for diagnostic purposes
309 ********************************************************************/
310 void *memcpy_wrapped(void *d
,void *s
,int l
,char *fname
,int line
)
312 if (l
>64 && (((int)d
)%4) != (((int)s
)%4))
313 DEBUG(4,("Misaligned memcpy(0x%X,0x%X,%d) at %s(%d)\n",d
,s
,l
,fname
,line
));
315 return(xx_old_memcpy(d
,s
,l
));
317 return(memcpy(d
,s
,l
));
320 #define memcpy(d,s,l) memcpy_wrapped(d,s,l,__FILE__,__LINE__)