Code indentation.
[midnight-commander.git] / src / vfs / smbfs / helpers / lib / system.c
blob70d88ecf0095322405fda205103343f91e5f93ab
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba system utilities
6 Copyright (C) Andrew Tridgell 1992-1998
8 Copyright (C) 2011
9 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "includes.h"
29 extern int DEBUGLEVEL;
32 The idea is that this file will eventually have wrappers around all
33 important system calls in samba. The aims are:
35 - to enable easier porting by putting OS dependent stuff in here
37 - to allow for hooks into other "pseudo-filesystems"
39 - to allow easier integration of things like the japanese extensions
41 - to support the philosophy of Samba to expose the features of
42 the OS within the SMB model. In general whatever file/printer/variable
43 expansions/etc make sense to the OS should be acceptable to Samba.
47 /*******************************************************************
48 this replaces the normal select() system call
49 return if some data has arrived on one of the file descriptors
50 return -1 means error
51 ********************************************************************/
52 #ifndef HAVE_SELECT
53 static int
54 pollfd (int fd)
56 int r = 0;
58 #ifdef HAS_RDCHK
59 r = rdchk (fd);
60 #elif defined(TCRDCHK)
61 (void) ioctl (fd, TCRDCHK, &r);
62 #else
63 (void) ioctl (fd, FIONREAD, &r);
64 #endif
66 return (r);
69 int
70 sys_select (int maxfd, fd_set * fds, struct timeval *tval)
72 fd_set fds2;
73 int counter = 0;
74 int found = 0;
76 FD_ZERO (&fds2);
78 while (1)
80 int i;
81 for (i = 0; i < maxfd; i++)
83 if (FD_ISSET (i, fds) && pollfd (i) > 0)
85 found++;
86 FD_SET (i, &fds2);
90 if (found)
92 memcpy ((void *) fds, (void *) &fds2, sizeof (fds2));
93 return (found);
96 if (tval && tval->tv_sec < counter)
97 return (0);
98 sleep (1);
99 counter++;
103 #else /* !NO_SELECT */
105 sys_select (int maxfd, fd_set * fds, struct timeval *tval)
107 #ifdef USE_POLL
108 struct pollfd pfd[256];
109 int i;
110 int maxpoll;
111 int timeout;
112 int pollrtn;
114 maxpoll = 0;
115 for (i = 0; i < maxfd; i++)
117 if (FD_ISSET (i, fds))
119 struct pollfd *pfdp = &pfd[maxpoll++];
120 pfdp->fd = i;
121 pfdp->events = POLLIN;
122 pfdp->revents = 0;
126 timeout = (tval != NULL) ? (tval->tv_sec * 1000) + (tval->tv_usec / 1000) : -1;
127 errno = 0;
130 pollrtn = poll (&pfd[0], maxpoll, timeout);
132 while (pollrtn < 0 && errno == EINTR);
134 FD_ZERO (fds);
136 for (i = 0; i < maxpoll; i++)
137 if (pfd[i].revents & POLLIN)
138 FD_SET (pfd[i].fd, fds);
140 return pollrtn;
141 #else /* USE_POLL */
143 struct timeval t2;
144 int selrtn;
148 if (tval)
149 memcpy ((void *) &t2, (void *) tval, sizeof (t2));
150 errno = 0;
151 selrtn = select (maxfd, SELECT_CAST fds, NULL, NULL, tval ? &t2 : NULL);
153 while (selrtn < 0 && errno == EINTR);
155 return (selrtn);
157 #endif /* USE_POLL */
158 #endif /* NO_SELECT */
160 /*******************************************************************
161 A stat() wrapper that will deal with 64 bit filesizes.
162 ********************************************************************/
165 sys_stat (const char *fname, SMB_STRUCT_STAT * sbuf)
167 return stat (fname, sbuf);
170 /*******************************************************************
171 An lstat() wrapper that will deal with 64 bit filesizes.
172 ********************************************************************/
173 #if 0
175 sys_lstat (const char *fname, SMB_STRUCT_STAT * sbuf)
177 return lstat (fname, sbuf);
180 /*******************************************************************
181 An fseek() wrapper that will deal with 64 bit filesizes.
182 ********************************************************************/
185 sys_fseek (FILE * fp, SMB_OFF_T offset, int whence)
187 return fseek (fp, offset, whence);
190 /*******************************************************************
191 An ftell() wrapper that will deal with 64 bit filesizes.
192 ********************************************************************/
194 SMB_OFF_T
195 sys_ftell (FILE * fp)
197 return (SMB_OFF_T) ftell (fp);
199 #endif /* 0 */
200 /*******************************************************************
201 An open() wrapper that will deal with 64 bit filesizes.
202 ********************************************************************/
205 sys_open (const char *path, int oflag, mode_t mode)
207 return open (path, oflag, mode);
210 /*******************************************************************
211 An fopen() wrapper that will deal with 64 bit filesizes.
212 ********************************************************************/
214 FILE *
215 sys_fopen (const char *path, const char *type)
217 return fopen (path, type);
220 #if 0
221 /*******************************************************************
222 A readdir wrapper that will deal with 64 bit filesizes.
223 ********************************************************************/
225 SMB_STRUCT_DIRENT *
226 sys_readdir (DIR * dirp)
228 return readdir (dirp);
231 /*******************************************************************
232 system wrapper for getwd
233 ********************************************************************/
234 char *
235 sys_getwd (char *s)
237 char *wd;
238 #ifdef HAVE_GETCWD
239 wd = (char *) getcwd (s, sizeof (pstring));
240 #else
241 wd = (char *) getwd (s);
242 #endif
243 return wd;
246 /*******************************************************************
247 chown isn't used much but OS/2 doesn't have it
248 ********************************************************************/
251 sys_chown (const char *fname, uid_t uid, gid_t gid)
253 #ifndef HAVE_CHOWN
254 static int done;
255 if (!done)
257 DEBUG (1, ("WARNING: no chown!\n"));
258 done = 1;
260 #else
261 return (chown (fname, uid, gid));
262 #endif
264 #endif /* 0 */
265 /**************************************************************************
266 A wrapper for gethostbyname() that tries avoids looking up hostnames
267 in the root domain, which can cause dial-on-demand links to come up for no
268 apparent reason.
269 ****************************************************************************/
270 struct hostent *
271 sys_gethostbyname (const char *name)
273 #ifdef REDUCE_ROOT_DNS_LOOKUPS
274 char query[256], hostname[256];
275 char *domain;
277 /* Does this name have any dots in it? If so, make no change */
279 if (strchr (name, '.'))
280 return (gethostbyname (name));
282 /* Get my hostname, which should have domain name
283 attached. If not, just do the gethostname on the
284 original string.
287 gethostname (hostname, sizeof (hostname) - 1);
288 hostname[sizeof (hostname) - 1] = 0;
289 if ((domain = strchr (hostname, '.')) == NULL)
290 return (gethostbyname (name));
292 /* Attach domain name to query and do modified query.
293 If names too large, just do gethostname on the
294 original string.
297 if ((strlen (name) + strlen (domain)) >= sizeof (query))
298 return (gethostbyname (name));
300 slprintf (query, sizeof (query) - 1, "%s%s", name, domain);
301 return (gethostbyname (query));
302 #else /* REDUCE_ROOT_DNS_LOOKUPS */
303 return (gethostbyname (name));
304 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
307 /**************************************************************************
308 Wrapper for random().
309 ****************************************************************************/
310 #if 0
311 long
312 sys_random (void)
314 #if defined(HAVE_RANDOM)
315 return (long) random ();
316 #elif defined(HAVE_RAND)
317 return (long) rand ();
318 #else
319 DEBUG (0, ("Error - no random function available !\n"));
320 exit (1);
321 #endif
324 /**************************************************************************
325 Wrapper for srandom().
326 ****************************************************************************/
328 void
329 sys_srandom (unsigned int seed)
331 #if defined(HAVE_SRANDOM)
332 srandom (seed);
333 #elif defined(HAVE_SRAND)
334 srand (seed);
335 #else
336 DEBUG (0, ("Error - no srandom function available !\n"));
337 exit (1);
338 #endif
340 #endif /* 0 */