backout all the changes to nmbd.
[kai/samba.git] / source3 / lib / system.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba system utilities
5    Copyright (C) Andrew Tridgell 1992-1995
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    The idea is that this file will eventually have wrappers around all
28    important system calls in samba. The aims are:
29
30    - to enable easier porting by putting OS dependent stuff in here
31
32    - to allow for hooks into other "pseudo-filesystems"
33
34    - to allow easier integration of things like the japanese extensions
35
36    - to support the philosophy of Samba to expose the features of
37      the OS within the SMB model. In general whatever file/printer/variable
38      expansions/etc make sense to the OS should be acceptable to Samba.
39 */
40
41
42 /*******************************************************************
43 this replaces the normal select() system call
44 return if some data has arrived on one of the file descriptors
45 return -1 means error
46 ********************************************************************/
47 #ifdef NO_SELECT
48 static int pollfd(int fd)
49 {
50   int     r=0;
51
52 #ifdef HAS_RDCHK
53   r = rdchk(fd);
54 #elif defined(TCRDCHK)
55   (void)ioctl(fd, TCRDCHK, &r);
56 #else
57   (void)ioctl(fd, FIONREAD, &r);
58 #endif
59
60   return(r);
61 }
62
63 int sys_select(fd_set *fds,struct timeval *tval)
64 {
65   fd_set fds2;
66   int counter=0;
67   int found=0;
68
69   FD_ZERO(&fds2);
70
71   while (1) 
72     {
73       int i;
74       for (i=0;i<255;i++) {
75         if (FD_ISSET(i,fds) && pollfd(i)>0) {
76           found++;
77           FD_SET(i,&fds2);
78         }
79       }
80
81       if (found) {
82         memcpy((void *)fds,(void *)&fds2,sizeof(fds2));
83         return(found);
84       }
85       
86       if (tval && tval->tv_sec < counter) return(0);
87       sleep(1);
88       counter++;
89     }
90 }
91
92 #else
93 int sys_select(fd_set *fds,struct timeval *tval)
94 {
95   struct timeval t2;
96   int selrtn;
97
98   do {
99     if (tval) memcpy((void *)&t2,(void *)tval,sizeof(t2));
100     errno = 0;
101     selrtn = select(255,SELECT_CAST fds,NULL,NULL,tval?&t2:NULL);
102   } while (selrtn<0 && errno == EINTR);
103
104   return(selrtn);
105 }
106 #endif
107
108
109 /*******************************************************************
110 just a unlink wrapper
111 ********************************************************************/
112 int sys_unlink(char *fname)
113 {
114   return(unlink(dos_to_unix(fname,False)));
115 }
116
117
118 /*******************************************************************
119 a simple open() wrapper
120 ********************************************************************/
121 int sys_open(char *fname,int flags,int mode)
122 {
123   return(open(dos_to_unix(fname,False),flags,mode));
124 }
125
126
127 /*******************************************************************
128 a simple opendir() wrapper
129 ********************************************************************/
130 DIR *sys_opendir(char *dname)
131 {
132   return(opendir(dos_to_unix(dname,False)));
133 }
134
135
136 /*******************************************************************
137 and a stat() wrapper
138 ********************************************************************/
139 int sys_stat(char *fname,struct stat *sbuf)
140 {
141   return(stat(dos_to_unix(fname,False),sbuf));
142 }
143
144 /*******************************************************************
145 don't forget lstat()
146 ********************************************************************/
147 int sys_lstat(char *fname,struct stat *sbuf)
148 {
149   return(lstat(dos_to_unix(fname,False),sbuf));
150 }
151
152
153 /*******************************************************************
154 mkdir() gets a wrapper
155 ********************************************************************/
156 int sys_mkdir(char *dname,int mode)
157 {
158   return(mkdir(dos_to_unix(dname,False),mode));
159 }
160
161
162 /*******************************************************************
163 do does rmdir()
164 ********************************************************************/
165 int sys_rmdir(char *dname)
166 {
167   return(rmdir(dos_to_unix(dname,False)));
168 }
169
170
171 /*******************************************************************
172 I almost forgot chdir()
173 ********************************************************************/
174 int sys_chdir(char *dname)
175 {
176   return(chdir(dos_to_unix(dname,False)));
177 }
178
179
180 /*******************************************************************
181 now for utime()
182 ********************************************************************/
183 int sys_utime(char *fname,struct utimbuf *times)
184 {
185   return(utime(dos_to_unix(fname,False),times));
186 }
187
188 /*******************************************************************
189 for rename()
190 ********************************************************************/
191 int sys_rename(char *from, char *to)
192 {
193 #ifdef KANJI
194     pstring zfrom, zto;
195     strcpy (zfrom, dos_to_unix (from, False));
196     strcpy (zto, dos_to_unix (to, False));
197     return rename (zfrom, zto);
198 #else 
199     return rename (from, to);
200 #endif /* KANJI */
201 }
202
203 /*******************************************************************
204 for chmod
205 ********************************************************************/
206 int sys_chmod(char *fname,int mode)
207 {
208   return(chmod(dos_to_unix(fname,False),mode));
209 }
210
211 /*******************************************************************
212 chown isn't used much but OS/2 doesn't have it
213 ********************************************************************/
214 int sys_chown(char *fname,int uid,int gid)
215 {
216 #ifdef NO_CHOWN
217   DEBUG(1,("Warning - chown(%s,%d,%d) not done\n",fname,uid,gid));
218 #else
219   return(chown(fname,uid,gid));
220 #endif
221 }
222
223 /*******************************************************************
224 os/2 also doesn't have chroot
225 ********************************************************************/
226 int sys_chroot(char *dname)
227 {
228 #ifdef NO_CHROOT
229   DEBUG(1,("Warning - chroot(%s) not done\n",dname));
230 #else
231   return(chroot(dname));
232 #endif
233 }