938746e9c9d7ff3696e409f44f067ad07567f953
[bbaumbach/samba-autobuild/.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 aim is twofold:
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
37
38 /*******************************************************************
39 this replaces the normal select() system call
40 return if some data has arrived on one of the file descriptors
41 return -1 means error
42 ********************************************************************/
43 #ifdef NO_SELECT
44 static int pollfd(int fd)
45 {
46   int     r=0;
47
48 #ifdef HAS_RDCHK
49   r = rdchk(fd);
50 #elif defined(TCRDCHK)
51   (void)ioctl(fd, TCRDCHK, &r);
52 #else
53   (void)ioctl(fd, FIONREAD, &r);
54 #endif
55
56   return(r);
57 }
58
59 int sys_select(fd_set *fds,struct timeval *tval)
60 {
61   fd_set fds2;
62   int counter=0;
63   int found=0;
64
65   FD_ZERO(&fds2);
66
67   while (1) 
68     {
69       int i;
70       for (i=0;i<255;i++) {
71         if (FD_ISSET(i,fds) && pollfd(i)>0) {
72           found++;
73           FD_SET(i,&fds2);
74         }
75       }
76
77       if (found) {
78         memcpy((void *)fds,(void *)&fds2,sizeof(fds2));
79         return(found);
80       }
81       
82       if (tval && tval.tv_sec < counter) return(0);
83       sleep(1);
84       counter++;
85     }
86 }
87
88 #else
89 int sys_select(fd_set *fds,struct timeval *tval)
90 {
91   struct timeval t2;
92   int selrtn;
93
94   do {
95     if (tval) memcpy((void *)&t2,(void *)tval,sizeof(t2));
96     errno = 0;
97     selrtn = select(16,SELECT_CAST fds,NULL,NULL,tval?&t2:NULL);
98   } while (selrtn<0 && errno == EINTR);
99
100   return(selrtn);
101 }
102 #endif
103
104
105 /*******************************************************************
106 just a unlink wrapper
107 ********************************************************************/
108 int sys_unlink(char *fname)
109 {
110   return(unlink(dos_to_unix(fname,False)));
111 }
112
113
114 /*******************************************************************
115 a simple open() wrapper
116 ********************************************************************/
117 int sys_open(char *fname,int flags,int mode)
118 {
119   return(open(dos_to_unix(fname,False),flags,mode));
120 }
121
122
123 /*******************************************************************
124 a simple opendir() wrapper
125 ********************************************************************/
126 DIR *sys_opendir(char *dname)
127 {
128   return(opendir(dos_to_unix(dname,False)));
129 }
130
131
132 /*******************************************************************
133 and a stat() wrapper
134 ********************************************************************/
135 int sys_stat(char *fname,struct stat *sbuf)
136 {
137   return(stat(dos_to_unix(fname,False),sbuf));
138 }
139
140 /*******************************************************************
141 don't forget lstat()
142 ********************************************************************/
143 int sys_lstat(char *fname,struct stat *sbuf)
144 {
145   return(lstat(dos_to_unix(fname,False),sbuf));
146 }
147
148
149 /*******************************************************************
150 mkdir() gets a wrapper
151 ********************************************************************/
152 int sys_mkdir(char *dname,int mode)
153 {
154   return(mkdir(dos_to_unix(dname,False),mode));
155 }
156
157
158 /*******************************************************************
159 do does rmdir()
160 ********************************************************************/
161 int sys_rmdir(char *dname)
162 {
163   return(rmdir(dos_to_unix(dname,False)));
164 }
165
166
167 /*******************************************************************
168 I almost forgot chdir()
169 ********************************************************************/
170 int sys_chdir(char *dname)
171 {
172   return(chdir(dos_to_unix(dname,False)));
173 }
174
175
176 /*******************************************************************
177 now for utime()
178 ********************************************************************/
179 int sys_utime(char *fname,struct utimbuf *times)
180 {
181   return(utime(dos_to_unix(fname,False),times));
182 }
183
184 /*******************************************************************
185 for rename()
186 ********************************************************************/
187 int sys_rename(char *from, char *to)
188 {
189 #ifdef KANJI
190     pstring zfrom, zto;
191     strcpy (zfrom, dos_to_unix (from, False));
192     strcpy (zto, dos_to_unix (to, False));
193     return rename (zfrom, zto);
194 #else 
195     return rename (from, to);
196 #endif /* KANJI */
197 }
198
199
200 /*******************************************************************
201 chown isn't used much but OS/2 doesn't have it
202 ********************************************************************/
203 int sys_chown(char *fname,int uid,int gid)
204 {
205 #ifdef NO_CHOWN
206   DEBUG(1,("Warning - chown(%s,%d,%d) not done\n",fname,uid,gid));
207 #else
208   return(chown(fname,uid,gid));
209 #endif
210 }
211
212 /*******************************************************************
213 os/2 also doesn't have chroot
214 ********************************************************************/
215 int sys_chroot(char *dname)
216 {
217 #ifdef NO_CHROOT
218   DEBUG(1,("Warning - chroot(%s) not done\n",dname));
219 #else
220   return(chroot(dname));
221 #endif
222 }