r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / lib / system.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba system utilities
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 1998-2002
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 #include "system/network.h"
24 #include "system/filesys.h"
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 /*******************************************************************
44  A wrapper for usleep in case we don't have one.
45 ********************************************************************/
46
47 int sys_usleep(long usecs)
48 {
49 #ifndef HAVE_USLEEP
50         struct timeval tval;
51 #endif
52
53         /*
54          * We need this braindamage as the glibc usleep
55          * is not SPEC1170 complient... grumble... JRA.
56          */
57
58         if(usecs < 0 || usecs > 1000000) {
59                 errno = EINVAL;
60                 return -1;
61         }
62
63 #if HAVE_USLEEP
64         usleep(usecs);
65         return 0;
66 #else /* HAVE_USLEEP */
67         /*
68          * Fake it with select...
69          */
70         tval.tv_sec = 0;
71         tval.tv_usec = usecs/1000;
72         select(0,NULL,NULL,NULL,&tval);
73         return 0;
74 #endif /* HAVE_USLEEP */
75 }
76
77
78 /*******************************************************************
79 A read wrapper that will deal with EINTR.
80 ********************************************************************/
81
82 ssize_t sys_read(int fd, void *buf, size_t count)
83 {
84         ssize_t ret;
85
86         do {
87                 ret = read(fd, buf, count);
88         } while (ret == -1 && errno == EINTR);
89         return ret;
90 }
91
92 /*******************************************************************
93 A write wrapper that will deal with EINTR.
94 ********************************************************************/
95
96 ssize_t sys_write(int fd, const void *buf, size_t count)
97 {
98         ssize_t ret;
99
100         do {
101                 ret = write(fd, buf, count);
102         } while (ret == -1 && errno == EINTR);
103         return ret;
104 }
105
106
107
108 /**************************************************************************
109 A wrapper for gethostbyname() that tries avoids looking up hostnames 
110 in the root domain, which can cause dial-on-demand links to come up for no
111 apparent reason.
112 ****************************************************************************/
113
114 struct hostent *sys_gethostbyname(const char *name)
115 {
116 #ifdef REDUCE_ROOT_DNS_LOOKUPS
117         char query[256], hostname[256];
118         char *domain;
119
120         /* Does this name have any dots in it? If so, make no change */
121
122         if (strchr_m(name, '.'))
123                 return(gethostbyname(name));
124
125         /* Get my hostname, which should have domain name 
126                 attached. If not, just do the gethostname on the
127                 original string. 
128         */
129
130         gethostname(hostname, sizeof(hostname) - 1);
131         hostname[sizeof(hostname) - 1] = 0;
132         if ((domain = strchr_m(hostname, '.')) == NULL)
133                 return(gethostbyname(name));
134
135         /* Attach domain name to query and do modified query.
136                 If names too large, just do gethostname on the
137                 original string.
138         */
139
140         if((strlen(name) + strlen(domain)) >= sizeof(query))
141                 return(gethostbyname(name));
142
143         slprintf(query, sizeof(query)-1, "%s%s", name, domain);
144         return(gethostbyname(query));
145 #else /* REDUCE_ROOT_DNS_LOOKUPS */
146         return(gethostbyname(name));
147 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
148 }
149
150 const char *sys_inet_ntoa(struct ipv4_addr in)
151 {
152         struct in_addr in2;
153         in2.s_addr = in.addr;
154         return inet_ntoa(in2);
155 }
156
157 struct ipv4_addr sys_inet_makeaddr(int net, int host)
158 {
159         struct in_addr in;
160         struct ipv4_addr in2;
161         in = inet_makeaddr(net, host);
162         in2.addr = in.s_addr;
163         return in2;
164 }
165