00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __XrdWin32_h__
00027 #define __XrdWin32_h__
00028
00029 #include <errno.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <io.h>
00033 #include <time.h>
00034 #include <direct.h>
00035 #include <sys/types.h>
00036 #include <Winsock2.h>
00037
00038 #ifndef POLLIN
00039 #define POLLIN 0x0001
00040 #define POLLPRI 0x0002
00041 #define POLLOUT 0x0004
00042 #define POLLERR 0x0008
00043 #define POLLHUP 0x0010
00044 #define POLLNVAL 0x0020
00045 #define POLLRDNORM 0x0001
00046 #define POLLWRNORM 0x0002
00047 #define POLLRDBAND 0x0000
00048
00049 struct pollfd {
00050 unsigned int fd;
00051 short events;
00052 short revents;
00053 };
00054 #endif
00055
00056 #ifndef EMSGSIZE
00057 #define EMSGSIZE WSAEMSGSIZE
00058 #endif
00059 #ifndef EAFNOSUPPORT
00060 #define EAFNOSUPPORT WSAEAFNOSUPPORT
00061 #endif
00062 #ifndef EWOULDBLOCK
00063 #define EWOULDBLOCK WSAEWOULDBLOCK
00064 #endif
00065 #ifndef ECONNRESET
00066 #define ECONNRESET WSAECONNRESET
00067 #endif
00068 #ifndef EINPROGRESS
00069 #define EINPROGRESS WSAEINPROGRESS
00070 #endif
00071 #ifndef ENOBUFS
00072 #define ENOBUFS WSAENOBUFS
00073 #endif
00074 #ifndef EPROTONOSUPPORT
00075 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
00076 #endif
00077 #ifndef ECONNREFUSED
00078 #define ECONNREFUSED WSAECONNREFUSED
00079 #endif
00080 #ifndef EBADFD
00081 #define EBADFD WSAENOTSOCK
00082 #endif
00083 #ifndef EOPNOTSUPP
00084 #define EOPNOTSUPP WSAEOPNOTSUPP
00085 #endif
00086 #ifndef ENETUNREACH
00087 #define ENETUNREACH WSAENETUNREACH
00088 #endif
00089 #ifndef EHOSTUNREACH
00090 #define EHOSTUNREACH WSAEHOSTUNREACH
00091 #endif
00092 #ifndef EHOSTDOWN
00093 #define EHOSTDOWN WSAEHOSTDOWN
00094 #endif
00095 #ifndef EISCONN
00096 #define EISCONN WSAEISCONN
00097 #endif
00098 #ifndef ECONNRESET
00099 #define ECONNRESET WSAECONNRESET
00100 #endif
00101 #ifndef ECONNABORTED
00102 #define ECONNABORTED WSAECONNABORTED
00103 #endif
00104 #ifndef ESHUTDOWN
00105 #define ESHUTDOWN WSAESHUTDOWN
00106 #endif
00107 #ifndef ETIMEDOUT
00108 #define ETIMEDOUT WSAETIMEDOUT
00109 #endif
00110 #ifndef ETXTBSY
00111 #define ETXTBSY 26
00112 #endif
00113
00114 #define WEXITSTATUS(w) (((w) >> 8) & 0xff)
00115 #define WIFEXITED(w) (((w) & 0xff) == 0)
00116 #define WTERMSIG(w) ((w) & 0x7f)
00117 #define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
00118
00119 #ifndef S_ISDIR
00120 #define S_ISDIR(m) (((m)&(S_IFMT)) == (S_IFDIR))
00121 #endif
00122 #ifndef S_ISREG
00123 #define S_ISREG(m) (((m)&(S_IFMT)) == (S_IFREG))
00124 #endif
00125 #ifndef S_IXUSR
00126 #define S_IXUSR 00100
00127 #endif
00128 #ifndef S_IRGRP
00129 #define S_IRGRP 00040
00130 #endif
00131 #ifndef S_IXGRP
00132 #define S_IXGRP 00010
00133 #endif
00134 #ifndef S_IROTH
00135 #define S_IROTH 00004
00136 #endif
00137 #ifndef S_IXOTH
00138 #define S_IXOTH 00001
00139 #endif
00140 #ifndef S_IRUSR
00141 #define S_IRUSR S_IREAD
00142 #endif
00143 #ifndef S_IWUSR
00144 #define S_IWUSR S_IWRITE
00145 #endif
00146 #ifndef S_IWGRP
00147 #define S_IWGRP 000020
00148 #endif
00149 #ifndef S_IWOTH
00150 #define S_IWOTH 000002
00151 #endif
00152 #ifndef S_IRWXU
00153 #define S_IRWXU 0000700
00154 #endif
00155 #ifndef S_IRWXG
00156 #define S_IRWXG 0000070
00157 #endif
00158
00159 #ifndef S_ISFIFO
00160 # ifndef S_IFIFO
00161 # define S_IFIFO 0010000
00162 # endif
00163 # define S_ISFIFO(m) ((m & S_IFMT) == S_IFIFO)
00164 #endif
00165
00166 #ifndef S_IFSOCK
00167 #define S_IFSOCK 0140000
00168 #endif
00169
00170 #define _SC_PAGESIZE 1
00171
00172 #define F_GETFL 1
00173 #define F_SETFL 2
00174 #define F_GETFD 4
00175 #define F_SETFD 8
00176
00177 #define FD_CLOEXEC 1
00178 #define O_NDELAY 2
00179 #define O_NONBLOCK 4
00180
00181 #define X_OK 1
00182 #define W_OK 2
00183 #define R_OK 4
00184
00185 #define RTLD_NOW 0x0001
00186
00187 #ifndef STDIN_FILENO
00188 #define STDIN_FILENO 0
00189 #define STDOUT_FILENO 1
00190 #define STDERR_FILENO 2
00191 #endif
00192
00193 #ifndef fsync
00194 #define fsync(a) _commit(a)
00195 #endif
00196
00197 #ifndef socklen_t
00198 #define socklen_t int
00199 #endif
00200
00201 #ifndef SOCKLEN_t
00202 #define SOCKLEN_t int
00203 #endif
00204
00205 #ifndef snprintf
00206 #define snprintf _snprintf
00207 #endif
00208
00209 #ifndef caddr_t
00210 typedef char* caddr_t;
00211 #endif
00212
00213 #ifndef pid_t
00214 typedef int pid_t;
00215 #endif
00216
00217 #ifndef mode_t
00218 typedef unsigned int mode_t;
00219 #endif
00220
00221 #ifndef uint16_t
00222 typedef unsigned short uint16_t;
00223 #endif
00224
00225 struct timezone {
00226 int tz_minuteswest;
00227 int tz_dsttime;
00228 };
00229
00230 inline int poll(struct pollfd *fds, unsigned int nfds, int timeout)
00231 {
00232 unsigned int max_fd = 0;
00233 unsigned int i;
00234
00235 fd_set *open_fds, *read_fds, *write_fds, *except_fds;
00236 struct timeval tv = { timeout / 1000, (timeout % 1000) * 1000 };
00237
00238 for (i = 0; i < nfds; ++i) {
00239 if (fds[i].fd > max_fd) {
00240 max_fd = fds[i].fd;
00241 }
00242 }
00243
00244 size_t fds_size = (max_fd + 1) * sizeof (fd_set);
00245
00246 open_fds = (fd_set *) malloc (fds_size);
00247 read_fds = (fd_set *) malloc (fds_size);
00248 write_fds = (fd_set *) malloc (fds_size);
00249 except_fds = (fd_set *) malloc (fds_size);
00250
00251 if (!open_fds || !read_fds || !write_fds || !except_fds) {
00252 return -1;
00253 }
00254
00255 FD_ZERO(open_fds) ;
00256 FD_ZERO(read_fds) ;
00257 FD_ZERO(write_fds) ;
00258 FD_ZERO(except_fds) ;
00259
00260 for ( i = 0; i < nfds; ++i) {
00261 FD_SET (fds[i].fd, open_fds);
00262 if (fds[i].events & POLLIN)
00263 FD_SET (fds[i].fd, read_fds);
00264 if (fds[i].events & POLLOUT)
00265 FD_SET (fds[i].fd, write_fds);
00266 if (fds[i].events & POLLPRI)
00267 FD_SET (fds[i].fd, except_fds);
00268 }
00269
00270
00271 int ret = select(max_fd + 1, read_fds, write_fds, except_fds, timeout < 0 ? NULL : &tv);
00272
00273 for (i = 0; i < nfds; ++i) {
00274 if (!FD_ISSET (fds[i].fd, open_fds))
00275 fds[i].revents = POLLNVAL;
00276 else if (ret < 0)
00277 fds[i].revents = POLLERR;
00278 else {
00279 fds[i].revents = 0;
00280 if (FD_ISSET (fds[i].fd, read_fds))
00281 fds[i].revents |= POLLIN;
00282 if (FD_ISSET (fds[i].fd, write_fds))
00283 fds[i].revents |= POLLOUT;
00284 if (FD_ISSET (fds[i].fd, except_fds))
00285 fds[i].revents |= POLLPRI;
00286 }
00287 }
00288
00289 free(open_fds);
00290 free(read_fds);
00291 free(write_fds);
00292 free(except_fds);
00293
00294 return ret;
00295 }
00296
00297 struct iovec {
00298 u_long iov_len;
00299 char FAR *iov_base;
00300 };
00301
00302 inline int lrint(double n) { return (int)n; };
00303
00304 extern void gethostbyname_r(const char *inetName, struct hostent *hent, char *buff,
00305 int buffsize, struct hostent **hp, int *rc);
00306 extern void gethostbyaddr_r(char *addr, size_t len, int type, struct hostent *hent, char *buff,
00307 size_t buffsize, struct hostent **hp, int *rc);
00308 extern int getservbyname_r(const char *servname, const char *servtype, struct servent *sent,
00309 char *buff, size_t buffsize, struct servent **sp);
00310 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
00311 extern void *dlopen(const char *libPath, int opt);
00312 extern BOOL dlclose(void *lib);
00313 extern void *dlsym(void *libHandle, const char *pname);
00314 extern char *dlerror();
00315 extern pid_t fork();
00316 extern const char *inet_ntop(int af, const void *src, char *dst, size_t size);
00317 extern int sysconf(int what);
00318 extern int fcntl(int fd, int cmd, long arg);
00319 extern int close(int fd);
00320 extern int writev(int sock, const struct iovec iov[], int nvecs);
00321 extern int posix_memalign (void **memptr, size_t alignment, size_t size);
00322 extern char *index(const char *str, int c);
00323 extern char *cuserid(char * s);
00324
00325 #ifndef localtime_r
00326 #define localtime_r( _clock, _result ) \
00327 ( *(_result) = *localtime( (_clock) ), \
00328 (_result) )
00329 #endif
00330
00331 #define pipe(a) _pipe(a, 256, O_BINARY)
00332 #define rindex strrchr
00333 #define sleep(s) Sleep(s*1000)
00334
00335 #define strtoll(a, b, c) _strtoi64(a, b, c)
00336 #define ntohll(x) (((_int64)(ntohl((int)((x << 32) >> 32))) << 32) | (unsigned int)ntohl(((int)(x >> 32))))
00337 #define htonll(x) ntohll(x)
00338 #define random() rand()
00339
00340 #define usleep(x) Sleep(x / 1000)
00341 #define lstat(a, b) stat(a, b)
00342 #define memalign(a, b) _aligned_malloc(b, a)
00343 struct sockaddr_un { unsigned short sun_family; char sun_path[128]; };
00344 #define setpgid(x,y)
00345 #define fsync(a) _commit(a)
00346 #define ssize_t SSIZE_T
00347
00348 #endif // __XrdWin32_h__
00349