b27909db03495e4d39248138a1fc09181374d122
[ira/wip.git] / source3 / lib / replace.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    replacement routines for broken systems
5    Copyright (C) Andrew Tridgell 1992-1997
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25
26
27  void replace_dummy(void) 
28 {}
29
30 #ifdef REPLACE_STRLEN
31 /****************************************************************************
32 a replacement strlen() that returns int for solaris
33 ****************************************************************************/
34  int Strlen(char *s)
35 {
36   int ret=0;
37   if (!s) return(0);
38   while (*s++) ret++;
39   return(ret);
40 }
41 #endif
42
43 #ifdef NO_FTRUNCATE
44  /*******************************************************************
45 ftruncate for operating systems that don't have it
46 ********************************************************************/
47  int ftruncate(int f,long l)
48 {
49       struct  flock   fl;
50
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);
56 }
57 #endif
58
59
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)
66 {
67         int len = strlen(p);
68
69         while ( *s != '\0' ) {
70                 if ( strncmp(s, p, len) == 0 )
71                 return s;
72                 s++;
73         }
74
75         return NULL;
76 }
77 #endif /* REPLACE_STRSTR */
78
79
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)
90 {
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;
95
96   if(t->tm_year < 70)
97     return((time_t)-1);
98
99   epoch = (t->tm_year - 70) * YEAR + 
100     (t->tm_year / 4 - 70 / 4 - t->tm_year / 100) * DAY;
101
102   y = t->tm_year;
103   m = 0;
104
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;
109     
110     if(++m > 11) {
111       m = 0;
112       y++;
113     }
114   }
115
116   epoch += (t->tm_mday - 1) * DAY;
117   epoch += t->tm_hour * HOUR + t->tm_min * MINUTE + t->tm_sec;
118   
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
132   }
133
134   return(epoch);
135 }
136 #endif /* REPLACE_MKTIME */
137
138
139
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;
145 {
146   if (link (zfrom, zto) < 0)
147     {
148       if (errno != EEXIST)
149         return -1;
150       if (unlink (zto) < 0
151           || link (zfrom, zto) < 0)
152         return -1;
153     }
154   return unlink (zfrom);
155 }
156 #endif
157
158
159 #ifdef REPLACE_INNETGR
160 /*
161  * Search for a match in a netgroup. This replaces it on broken systems.
162  */
163 int InNetGr(char *group,char *host,char *user,char *dom)
164 {
165   char *hst, *usr, *dm;
166   
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);
174     }
175   endnetgrent();
176   return (0);
177 }
178 #endif
179
180
181
182 #ifdef NO_INITGROUPS
183 #include <sys/types.h>
184 #include <limits.h>
185 #include <grp.h>
186
187 #ifndef NULL
188 #define NULL (void *)0
189 #endif
190
191 /****************************************************************************
192  some systems don't have an initgroups call 
193 ****************************************************************************/
194  int initgroups(char *name,gid_t id)
195 {
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;
204
205   grouplst[0] = id;
206   i = 1;
207   while (i < NGROUPS_MAX && 
208          ((g = (struct group *)getgrent()) != (struct group *)NULL)) 
209     {
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;
220         }
221         gr = g->gr_mem[++j];
222       }
223     }
224   endgrent();
225   return(setgroups(i,grouplst));
226 #endif
227 }
228 #endif
229
230
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);
237 }
238 #endif
239
240
241 #if WRAP_MEMCPY
242 #undef memcpy
243 /*******************************************************************
244 a wrapper around memcpy for diagnostic purposes
245 ********************************************************************/
246 void *memcpy_wrapped(void *d,void *s,int l,char *fname,int line)
247 {
248   if (l>64 && (((int)d)%4) != (((int)s)%4))
249     DEBUG(4,("Misaligned memcpy(0x%X,0x%X,%d) at %s(%d)\n",d,s,l,fname,line));
250 #ifdef xx_old_memcpy  
251   return(xx_old_memcpy(d,s,l));
252 #else
253   return(memcpy(d,s,l));
254 #endif
255 }
256 #define memcpy(d,s,l) memcpy_wrapped(d,s,l,__FILE__,__LINE__)
257 #endif
258