portability.c

Go to the documentation of this file.
00001 
00009 /* $Id: portability.c,v 1.4 2006/03/20 20:20:37 seymour Exp $ */
00010 /* $UTK_Copyright: $ */
00011 
00012 #include <stdio.h>
00013 
00014 #include "portability.h"
00015 #include "utility.h"
00016 
00017 
00018 /* -------------------------------------------------- */
00019 #ifdef WIN32
00020 /* -------------------------------------------------- */
00021 
00022 int gettimeofday(struct timeval* tp, void* tzp) 
00023 {
00024   DWORD t;
00025   t = timeGetTime();
00026   tp->tv_sec = t / 1000;
00027   tp->tv_usec = t % 1000;
00028   /* 0 indicates that the call succeeded. */
00029   return 0;
00030 }
00031 
00032 int kill(int pid, int sig) 
00033 {
00034   int status;
00035   HANDLE pidh = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
00036   status = TerminateProcess(pidh, (UINT)0);
00037   CloseHandle(pidh);
00038   if (status == 0) 
00039     return -1;
00040   else return 0;
00041 }
00042 
00043 int waitpid(int pid, int* status, int options)
00044 {
00045   int retval;
00046   HANDLE pidh = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
00047   retval = WaitForSingleObject(pidh, options);
00048   CloseHandle(pidh);
00049   return retval;
00050 }
00051 
00052 int read_socket(SOCKET sock, const char*buffer, int len)
00053 {
00054   /* DBGPRINTF("read_socket: sock %d len %d buffer[1] %d \n", sock, len, (int)*buffer); */
00055   return recv(sock, buffer, len, 0);
00056 }
00057 
00058 int write_socket(SOCKET sock, const char*buffer, int len)
00059 {
00060   /* DBGPRINTF("write_socket: sock %d len %d buffer[1] %d \n", sock, len, (int)*buffer); */
00061   return send(sock, buffer, len, 0);
00062 }
00063 
00064 int close_socket(SOCKET sock)
00065 {
00066   /* DBGPRINTF("close_socket\n"); */
00067   return closesocket(sock);
00068 }
00069 
00070 int getdomainname(char *name, size_t len)
00071 {
00072   return -1;
00073 }
00074 
00075 int gs_lock_fd_nowait(int fd, int lock_type)
00076 {
00077   ERRPRINTF("File locking is not implemented for Win32\n");
00078   return -1;
00079 }
00080 
00081 int gs_lock_fd(int fd, int lock_type) 
00082 {
00083   ERRPRINTF("File locking is not implemented for Win32\n");
00084   return -1;
00085 }
00086 
00087 int gs_unlock_fd(int fd) 
00088 {
00089   ERRPRINTF("File locking is not implemented for Win32\n");
00090   return -1;
00091 }
00092 
00093 void initialize_sockets()
00094 {
00095   WORD wVersionRequested = MAKEWORD( 2, 0 );
00096   WSADATA wsaData;
00097   WSAStartup(wVersionRequested, &wsaData );
00098 }    
00099 
00100 
00101 void cleanup_sockets()
00102 {
00103   WSACleanup();
00104 }    
00105 
00106 
00107 
00108 /* -------------------------------------------------- */
00109 #else  /* not WIN32 */
00110 /* -------------------------------------------------- */
00111 
00112 int gs_lock_fd_nowait(int fd, int lock_type)
00113 {
00114   struct flock fl;
00115   
00116   fl.l_type   = lock_type;
00117   fl.l_whence = SEEK_SET;
00118   fl.l_start  = 0;
00119   fl.l_len    = sizeof(int);
00120   fl.l_pid    = getpid();
00121   
00122   return fcntl(fd, F_SETLK, &fl);
00123 }
00124 
00125 int gs_lock_fd(int fd, int lock_type) 
00126 {               
00127   struct flock fl;
00128   
00129   fl.l_type   = lock_type;
00130   fl.l_whence = SEEK_SET;
00131   fl.l_start  = 0;
00132   fl.l_len    = sizeof(int);
00133   fl.l_pid    = getpid();
00134   
00135   return fcntl(fd, F_SETLKW, &fl);
00136 }
00137 
00138 int gs_unlock_fd(int fd) 
00139 {
00140   struct flock fl;
00141 
00142   fl.l_type   = F_UNLCK;
00143   fl.l_whence = SEEK_SET;
00144   fl.l_start  = 0;
00145   fl.l_len    = sizeof(int);
00146   fl.l_pid    = getpid();
00147 
00148   if(fcntl(fd, F_SETLK, &fl) == -1) {
00149     return -1;
00150   }
00151 
00152   return 0;
00153 }
00154 
00155 
00156 void initialize_sockets() {}    
00157 
00158 void cleanup_sockets() {}    
00159 
00160 
00161 #endif