pike.git / src / fdlib.c

version» Context lines:

pike.git/src/fdlib.c:332:    case FD_SOCKET: return "IS SOCKET";    case FD_CONSOLE: return "IS CONSOLE";    case FD_FILE: return "IS FILE";    case FD_PIPE: return "IS PIPE";    default: return "NOT OPEN";    }   }      PMOD_EXPORT int debug_fd_query_properties(int fd, int guess)   { -  switch(fd_type[fd]) +  int type; +  +  if (fd_to_handle(fd, &type, NULL) < 0) return 0; +  +  switch(type)    {    case FD_SOCKET:    return fd_BUFFERED | fd_CAN_NONBLOCK | fd_CAN_SHUTDOWN;       case FD_FILE:    return fd_INTERPROCESSABLE;       case FD_CONSOLE:    return fd_CAN_NONBLOCK | fd_INTERPROCESSABLE;   
pike.git/src/fdlib.c:1322:    l = wcslen(fname);       /* Get rid of terminating slashes. */    while (l && ISSEPARATOR(fname[l-1])) {    fname[--l] = 0;    }       omode=0;    FDDEBUG(fprintf(stderr, "fd_open(%S, 0x%x, %o)\n",    fname, open_mode, create_mode)); -  if(first_free_handle == FD_NO_MORE_FREE) -  { +  +  fd = allocate_fd(FD_FILE, INVALID_HANDLE_VALUE); +  if (fd < 0) {    free(fname); -  errno=EMFILE; +     return -1;    }       if(open_mode & fd_RDONLY) omode|=GENERIC_READ;    if(open_mode & fd_WRONLY) omode|=GENERIC_WRITE;       switch(open_mode & (fd_CREAT | fd_TRUNC | fd_EXCL))    {    case fd_CREAT | fd_TRUNC:    cmode=CREATE_ALWAYS;
pike.git/src/fdlib.c:1388:    /* An invalid name means the file doesn't exist. This is    * consistent with fd_stat, opendir, and unix. */    errno = ENOENT;    else    set_errno_from_win32_error (err);    return -1;    }       SetHandleInformation(x,HANDLE_FLAG_INHERIT|HANDLE_FLAG_PROTECT_FROM_CLOSE,0);    -  mt_lock(&fd_mutex); +  set_fd_handle(fd, x);    -  fd=first_free_handle; -  first_free_handle=fd_type[fd]; -  fd_type[fd]=FD_FILE; -  da_handle[fd] = x; -  -  mt_unlock(&fd_mutex); -  +     if(open_mode & fd_APPEND)    fd_lseek(fd,0,SEEK_END);       FDDEBUG(fprintf(stderr, "Opened %S file as %d (%d)\n", fname, fd, x));       free(fname);       return fd;   }      PMOD_EXPORT FD debug_fd_socket(int domain, int type, int proto)   {    FD fd;    SOCKET s; -  mt_lock(&fd_mutex); -  if(first_free_handle == FD_NO_MORE_FREE) -  { -  mt_unlock(&fd_mutex); -  errno=EMFILE; -  return -1; -  } -  mt_unlock(&fd_mutex); +     -  +  fd = allocate_fd(FD_SOCKET, (HANDLE)INVALID_SOCKET); +  if (fd < 0) return -1; +     s=socket(domain, type, proto);       if(s==INVALID_SOCKET)    { -  set_errno_from_win32_error (WSAGetLastError()); +  DWORD err = WSAGetLastError(); +  free_fd(fd); +  set_errno_from_win32_error (err);    return -1;    }       SetHandleInformation((HANDLE)s,    HANDLE_FLAG_INHERIT|HANDLE_FLAG_PROTECT_FROM_CLOSE, 0); -  mt_lock(&fd_mutex); -  fd=first_free_handle; -  first_free_handle=fd_type[fd]; -  fd_type[fd] = FD_SOCKET; -  da_handle[fd] = (HANDLE)s; -  mt_unlock(&fd_mutex); +     -  +  set_fd_handle(fd, (HANDLE)s); +     FDDEBUG(fprintf(stderr,"New socket: %d (%d)\n",fd,s));       return fd;   }      PMOD_EXPORT int debug_fd_pipe(int fds[2] DMALLOC_LINE_ARGS)   { -  +  int tmp_fds[2];    HANDLE files[2]; -  mt_lock(&fd_mutex); -  if(first_free_handle == FD_NO_MORE_FREE) -  { -  mt_unlock(&fd_mutex); -  errno=EMFILE; +  +  tmp_fds[0] = allocate_fd(FD_PIPE, INVALID_HANDLE_VALUE); +  if (tmp_fds[0] < 0) {    return -1;    } -  mt_unlock(&fd_mutex); +  tmp_fds[1] = allocate_fd(FD_PIPE, INVALID_HANDLE_VALUE); +  if (tmp_fds[1] < 0) { +  free_fd(tmp_fds[0]); +  return -1; +  } +     if(!CreatePipe(&files[0], &files[1], NULL, 0))    { -  set_errno_from_win32_error (GetLastError()); +  DWORD err = GetLastError(); +  free_fd(tmp_fds[0]); +  free_fd(tmp_fds[1]); +  set_errno_from_win32_error (err);    return -1;    }       FDDEBUG(fprintf(stderr,"ReadHANDLE=%d WriteHANDLE=%d\n",files[0],files[1]));       SetHandleInformation(files[0],HANDLE_FLAG_INHERIT|HANDLE_FLAG_PROTECT_FROM_CLOSE,0);    SetHandleInformation(files[1],HANDLE_FLAG_INHERIT|HANDLE_FLAG_PROTECT_FROM_CLOSE,0); -  mt_lock(&fd_mutex); -  fds[0]=first_free_handle; -  first_free_handle=fd_type[fds[0]]; -  fd_type[fds[0]]=FD_PIPE; -  da_handle[fds[0]] = files[0]; +     -  fds[1]=first_free_handle; -  first_free_handle=fd_type[fds[1]]; -  fd_type[fds[1]]=FD_PIPE; -  da_handle[fds[1]] = files[1]; +  fds[0] = tmp_fds[0]; +  set_fd_handle(fds[0], files[0]); +  fds[1] = tmp_fds[1]; +  set_fd_handle(fds[1], files[1]);    -  mt_unlock(&fd_mutex); +     FDDEBUG(fprintf(stderr,"New pipe: %d (%d) -> %d (%d)\n",fds[0],files[0], fds[1], fds[1]));;      #ifdef DEBUG_MALLOC    debug_malloc_register_fd( fds[0], DMALLOC_LOCATION());    debug_malloc_register_fd( fds[1], DMALLOC_LOCATION());   #endif       return 0;   }      PMOD_EXPORT FD debug_fd_accept(FD fd, struct sockaddr *addr,    ACCEPT_SIZE_T *addrlen)   {    FD new_fd;    SOCKET s; -  mt_lock(&fd_mutex); +     FDDEBUG(fprintf(stderr,"Accept on %d (%ld)..\n",    fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]))); -  if(first_free_handle == FD_NO_MORE_FREE) -  { -  mt_unlock(&fd_mutex); -  errno=EMFILE; -  return -1; -  } -  if(fd_type[fd]!=FD_SOCKET) -  { -  mt_unlock(&fd_mutex); -  errno=ENOTSUPP; -  return -1; -  } -  s=(SOCKET)da_handle[fd]; -  mt_unlock(&fd_mutex); -  s=accept(s, addr, addrlen); +  +  if (fd_to_socket(fd, &s) < 0) return -1; +  +  new_fd = allocate_fd(FD_SOCKET, (HANDLE)INVALID_SOCKET); +  if (new_fd < 0) return -1; +  +  s = accept(s, addr, addrlen); +     if(s==INVALID_SOCKET)    { -  set_errno_from_win32_error (WSAGetLastError()); +  DWORD err = WSAGetLastError(); +  free_fd(new_fd); +  set_errno_from_win32_error(err);    FDDEBUG(fprintf(stderr,"Accept failed with errno %d\n",errno));    return -1;    }       SetHandleInformation((HANDLE)s,    HANDLE_FLAG_INHERIT|HANDLE_FLAG_PROTECT_FROM_CLOSE, 0); -  mt_lock(&fd_mutex); -  new_fd=first_free_handle; -  first_free_handle=fd_type[new_fd]; -  fd_type[new_fd]=FD_SOCKET; -  da_handle[new_fd] = (HANDLE)s; +  set_fd_handle(new_fd, (HANDLE)s);       FDDEBUG(fprintf(stderr,"Accept on %d (%ld) returned new socket: %d (%ld)\n",    fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]),    new_fd, PTRDIFF_T_TO_LONG((ptrdiff_t)s)));    -  mt_unlock(&fd_mutex); -  +     return new_fd;   }         #define SOCKFUN(NAME,X1,X2) \   PMOD_EXPORT int PIKE_CONCAT(debug_fd_,NAME) X1 { \    SOCKET s; \    int ret; \    FDDEBUG(fprintf(stderr, #NAME " on %d (%ld)\n", \    fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]))); \ -  mt_lock(&fd_mutex); \ -  if(fd_type[fd] != FD_SOCKET) { \ -  mt_unlock(&fd_mutex); \ -  errno = ENOTSUPP; \ -  return -1; \ -  } \ -  s = (SOCKET)da_handle[fd]; \ -  mt_unlock(&fd_mutex); \ +  if (fd_to_socket(fd, &s) < 0) return -1; \    ret = NAME X2; \    if(ret == SOCKET_ERROR) { \    set_errno_from_win32_error (WSAGetLastError()); \    ret = -1; \    } \    FDDEBUG(fprintf(stderr, #NAME " returned %d (%d)\n", ret, errno)); \    return ret; \   }      #define SOCKFUN1(NAME,T1) \
pike.git/src/fdlib.c:1587:   SOCKFUN2(getpeername,struct sockaddr *,ACCEPT_SIZE_T *)   SOCKFUN5(recvfrom,void *,int,int,struct sockaddr *,ACCEPT_SIZE_T *)   SOCKFUN3(send,void *,int,int)   SOCKFUN5(sendto,void *,int,int,struct sockaddr *,unsigned int)   SOCKFUN1(shutdown, int)   SOCKFUN1(listen, int)      PMOD_EXPORT int debug_fd_connect (FD fd, struct sockaddr *a, int len)   {    SOCKET ret; -  mt_lock(&fd_mutex); +     FDDEBUG(fprintf(stderr, "connect on %d (%ld)\n",    fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]));    for(ret=0;ret<len;ret++)    fprintf(stderr," %02x",((unsigned char *)a)[ret]);    fprintf(stderr,"\n"); -  ) -  if(fd_type[fd] != FD_SOCKET) -  { -  mt_unlock(&fd_mutex); -  errno=ENOTSUPP; -  return -1; -  } -  ret=(SOCKET)da_handle[fd]; -  mt_unlock(&fd_mutex); +  ); +  if (fd_to_socket(fd, &ret) < 0) return -1; +     ret=connect(ret,a,len); -  +     if(ret == SOCKET_ERROR) set_errno_from_win32_error (WSAGetLastError());    FDDEBUG(fprintf(stderr, "connect returned %d (%d)\n",ret,errno));    return DO_NOT_WARN((int)ret);   }      PMOD_EXPORT int debug_fd_close(FD fd)   {    HANDLE h;    int type; -  mt_lock(&fd_mutex); -  h = da_handle[fd]; -  FDDEBUG(fprintf(stderr,"Closing %d (%ld)\n", -  fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]))); -  type=fd_type[fd]; -  mt_unlock(&fd_mutex); +  +  if (fd_to_handle(fd, &type, &h) < 0) return -1; +  +  FDDEBUG(fprintf(stderr, "Closing %d (%ld)\n", fd, PTRDIFF_T_TO_LONG(h))); +  +  free_fd(fd); +     switch(type)    {    case FD_SOCKET:    if(closesocket((SOCKET)h))    {    set_errno_from_win32_error (GetLastError());    FDDEBUG(fprintf(stderr,"Closing %d (%ld) failed with errno=%d\n",    fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]),    errno));    return -1;    }    break;       default:    if(!CloseHandle(h))    {    set_errno_from_win32_error (GetLastError());    return -1;    }    } -  +     FDDEBUG(fprintf(stderr,"%d (%ld) closed\n",    fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]))); -  mt_lock(&fd_mutex); -  if(fd_type[fd]<FD_NO_MORE_FREE) -  { -  fd_type[fd]=first_free_handle; -  first_free_handle=fd; -  } -  mt_unlock(&fd_mutex); +        return 0;   }      PMOD_EXPORT ptrdiff_t debug_fd_write(FD fd, void *buf, ptrdiff_t len)   {    int kind;    HANDLE handle;    -  mt_lock(&fd_mutex); +     FDDEBUG(fprintf(stderr, "Writing %d bytes to %d (%d)\n",    len, fd, da_handle[fd])); -  kind = fd_type[fd]; -  handle = da_handle[fd]; -  mt_unlock(&fd_mutex); +     -  +  if (fd_to_handle(fd, &kind, &handle) < 0) return -1; +     switch(kind)    {    case FD_SOCKET:    {    ptrdiff_t ret = send((SOCKET)handle, buf,    DO_NOT_WARN((int)len),    0);    if(ret<0)    {    set_errno_from_win32_error (WSAGetLastError());
pike.git/src/fdlib.c:1709:    }       default:    errno=ENOTSUPP;    return -1;    }   }      PMOD_EXPORT ptrdiff_t debug_fd_read(FD fd, void *to, ptrdiff_t len)   { +  int type;    DWORD ret;    ptrdiff_t rret;    HANDLE handle;    -  mt_lock(&fd_mutex); +     FDDEBUG(fprintf(stderr,"Reading %d bytes from %d (%d) to %lx\n",    len, fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]),    PTRDIFF_T_TO_LONG((ptrdiff_t)to))); -  ret=fd_type[fd]; -  handle=da_handle[fd]; -  mt_unlock(&fd_mutex); +     -  switch(ret) +  if (fd_to_handle(fd, &type, &handle) < 0) return -1; +  +  switch(type)    {    case FD_SOCKET:    rret=recv((SOCKET)handle, to,    DO_NOT_WARN((int)len),    0);    if(rret<0)    {    set_errno_from_win32_error (WSAGetLastError());    FDDEBUG(fprintf(stderr,"Read on %d failed %ld\n",fd,errno));    return -1;
pike.git/src/fdlib.c:1767:       default:    errno=ENOTSUPP;    return -1;    }   }      PMOD_EXPORT PIKE_OFF_T debug_fd_lseek(FD fd, PIKE_OFF_T pos, int where)   {    PIKE_OFF_T ret; +  int type;    HANDLE h;    -  mt_lock(&fd_mutex); -  if(fd_type[fd]!=FD_FILE) +  if (fd_to_handle(fd, &type, &h) < 0) return -1; +  if(type != FD_FILE)    { -  mt_unlock(&fd_mutex); +     errno=ENOTSUPP;    return -1;    } -  +    #if FILE_BEGIN != SEEK_SET || FILE_CURRENT != SEEK_CUR || FILE_END != SEEK_END    switch(where)    {    case SEEK_SET: where=FILE_BEGIN; break;    case SEEK_CUR: where=FILE_CURRENT; break;    case SEEK_END: where=FILE_END; break;    }   #endif -  h = da_handle[fd]; -  mt_unlock(&fd_mutex); +        {   #ifdef INT64   #ifdef HAVE_SETFILEPOINTEREX    /* Windows NT based. */    LARGE_INTEGER li_pos;    LARGE_INTEGER li_ret;    li_pos.QuadPart = pos;    li_ret.QuadPart = 0;    if(!SetFilePointerEx(h, li_pos, &li_ret, where)) {
pike.git/src/fdlib.c:1828:    return -1;    }   #endif /* INT64 */    }       return ret;   }      PMOD_EXPORT int debug_fd_ftruncate(FD fd, PIKE_OFF_T len)   { +  int type;    HANDLE h;    LONG oldfp_lo, oldfp_hi, len_hi;    DWORD err;    -  mt_lock(&fd_mutex); -  if(fd_type[fd]!=FD_FILE) +  if (fd_to_handle(fd, &type, &h) < 0) return -1; +  if(type != FD_FILE)    { -  mt_unlock(&fd_mutex); +     errno=ENOTSUPP;    return -1;    } -  h = da_handle[fd]; -  mt_unlock(&fd_mutex); +        oldfp_hi = 0;    oldfp_lo = SetFilePointer(h, 0, &oldfp_hi, FILE_CURRENT);    if(!~oldfp_lo) {    err = GetLastError();    if(err != NO_ERROR) {    set_errno_from_win32_error (err);    return -1;    }    }
pike.git/src/fdlib.c:1888:    return -1;    }    }       return 0;   }      PMOD_EXPORT int debug_fd_flock(FD fd, int oper)   {    long ret; +  int type;    HANDLE h; -  mt_lock(&fd_mutex); -  if(fd_type[fd]!=FD_FILE) +  +  if (fd_to_handle(fd, &type, &h) < 0) return -1; +  if(type != FD_FILE)    { -  mt_unlock(&fd_mutex); +     errno=ENOTSUPP;    return -1;    } -  h = da_handle[fd]; -  mt_unlock(&fd_mutex); +        if(oper & fd_LOCK_UN)    {    ret=UnlockFile(h,    0,    0,    0xffffffff,    0xffffffff);    }else{    DWORD flags = 0;
pike.git/src/fdlib.c:1941:       return 0;   }         /* Note: s->st_ctime is set to the file creation time. It should    * probably be the last access time to be closer to the unix    * counterpart, but the creation time is admittedly more useful. */   PMOD_EXPORT int debug_fd_fstat(FD fd, PIKE_STAT_T *s)   { +  int type; +  HANDLE h;    FILETIME c,a,m;    -  mt_lock(&fd_mutex); +     FDDEBUG(fprintf(stderr, "fstat on %d (%ld)\n",    fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]))); -  if(fd_type[fd]!=FD_FILE) +  if (fd_to_handle(fd, &type, &h) < 0) return -1; +  if (type != FD_FILE)    {    errno=ENOTSUPP; -  mt_unlock(&fd_mutex); +     return -1;    }       memset(s, 0, sizeof(PIKE_STAT_T));    s->st_nlink=1;    -  switch(fd_type[fd]) +  switch(type)    {    case FD_SOCKET:    s->st_mode=S_IFSOCK;    break;       default: -  switch(GetFileType(da_handle[fd])) +  switch(GetFileType(h))    {    default:    case FILE_TYPE_UNKNOWN: s->st_mode=0; break;       case FILE_TYPE_DISK:    s->st_mode=S_IFREG;    {    DWORD high, err; -  s->st_size=GetFileSize(da_handle[fd],&high); +  s->st_size=GetFileSize(h, &high);    if (s->st_size == INVALID_FILE_SIZE &&    (err = GetLastError()) != NO_ERROR) {    set_errno_from_win32_error (err); -  mt_unlock(&fd_mutex); +     return -1;    }   #ifdef INT64    s->st_size += (INT64) high << 32;   #else    if (high) s->st_size = MAXDWORD;   #endif    } -  if(!GetFileTime(da_handle[fd], &c, &a, &m)) +  +  if(!GetFileTime(h, &c, &a, &m))    {    set_errno_from_win32_error (GetLastError()); -  mt_unlock(&fd_mutex); +     return -1;    }       /* FIXME: Determine the filesystem type to use    * fat_filetimes_to_stattimes when necessary. */       nonfat_filetimes_to_stattimes (&c, &a, &m, s);    break;       case FILE_TYPE_CHAR: s->st_mode=S_IFCHR; break;    case FILE_TYPE_PIPE: s->st_mode=S_IFIFO; break;    }    }    s->st_mode |= 0666; -  mt_unlock(&fd_mutex); +     return 0;   }         #ifdef FD_DEBUG   static void dump_FDSET(FD_SET *x, int fds)   {    if(x)    {    int e, first=1;    fprintf(stderr,"["); -  for(e=0;e<fds;e++) +  for(e = 0; e < FD_SET_SIZE; e++)    {    if(FD_ISSET(da_handle[e],x))    {    if(!first) fprintf(stderr,",",e);    fprintf(stderr,"%d",e);    first=0;    }    }    fprintf(stderr,"]");    }else{
pike.git/src/fdlib.c:2072:    fprintf(stderr,",(%ld,%06ld));\n", (long) t->tv_sec,(long) t->tv_usec);    )       return ret;   }         PMOD_EXPORT int debug_fd_ioctl(FD fd, int cmd, void *data)   {    int ret; +  SOCKET s; +     FDDEBUG(fprintf(stderr,"ioctl(%d (%ld,%d,%p)\n",    fd, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[fd]), cmd, data)); -  switch(fd_type[fd]) -  { -  case FD_SOCKET: -  ret=ioctlsocket((SOCKET)da_handle[fd], cmd, data); +  +  if (fd_to_socket(fd, &s) < 0) return -1; +  +  ret = ioctlsocket(s, cmd, data); +     FDDEBUG(fprintf(stderr,"ioctlsocket returned %ld (%d)\n",ret,errno)); -  +     if(ret==SOCKET_ERROR)    {    set_errno_from_win32_error (WSAGetLastError());    return -1;    }    return ret; -  -  default: -  errno=ENOTSUPP; -  return -1; +    } - } +          PMOD_EXPORT FD debug_fd_dup(FD from)   {    FD fd; -  HANDLE x,p=GetCurrentProcess(); +  int type; +  HANDLE h,x,p=GetCurrentProcess();    -  mt_lock(&fd_mutex); - #ifdef DEBUG -  if(fd_type[from]>=FD_NO_MORE_FREE) -  Pike_fatal("fd_dup() on file which is not open!\n"); - #endif -  if(!DuplicateHandle(p,da_handle[from],p,&x,0,0,DUPLICATE_SAME_ACCESS)) +  if (fd_to_handle(from, &type, &h) < 0) return -1; +  +  fd = allocate_fd(type, +  (type == FD_SOCKET)? +  (HANDLE)INVALID_SOCKET:INVALID_HANDLE_VALUE); +  +  if(!DuplicateHandle(p, h, p, &x, 0, 0, DUPLICATE_SAME_ACCESS))    { -  set_errno_from_win32_error (GetLastError()); -  mt_unlock(&fd_mutex); +  DWORD err = GetLastError(); +  free_fd(fd); +  set_errno_from_win32_error(err);    return -1;    }    -  fd=first_free_handle; -  first_free_handle=fd_type[fd]; -  fd_type[fd]=fd_type[from]; -  da_handle[fd] = x; -  mt_unlock(&fd_mutex); +  set_fd_handle(fd, x);       FDDEBUG(fprintf(stderr,"Dup %d (%ld) to %d (%d)\n",    from, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[from]), fd, x));    return fd;   }      PMOD_EXPORT FD debug_fd_dup2(FD from, FD to)   { -  HANDLE x,p=GetCurrentProcess(); +  int type; +  HANDLE h,x,p=GetCurrentProcess();    -  mt_lock(&fd_mutex); -  if(!DuplicateHandle(p,da_handle[from],p,&x,0,0,DUPLICATE_SAME_ACCESS)) -  { -  set_errno_from_win32_error (GetLastError()); -  mt_unlock(&fd_mutex); +  if ((from == to) || (to < 0) || (to >= FD_SETSIZE)) { +  errno = EINVAL;    return -1;    }    -  if(fd_type[to] < FD_NO_MORE_FREE) +  if (fd_to_handle(from, &type, &h) < 0) return -1; +  +  if(!DuplicateHandle(p, h, p, &x, 0, 0, DUPLICATE_SAME_ACCESS))    { -  if(!CloseHandle(da_handle[to])) -  { +     set_errno_from_win32_error (GetLastError()); -  mt_unlock(&fd_mutex); +     return -1;    } -  }else{ -  int *prev,next; -  for(prev=&first_free_handle;(next=*prev) != FD_NO_MORE_FREE;prev=fd_type+next) -  { -  if(next==to) -  { -  *prev=fd_type[next]; -  break; +  +  if (reallocate_fd(to, type, x) < 0) { +  if (type == FD_SOCKET) { +  closesocket((SOCKET)x); +  } else { +  CloseHandle(x);    } -  +  return -1;    } -  } -  fd_type[to]=fd_type[from]; -  da_handle[to] = x; -  mt_unlock(&fd_mutex); +        FDDEBUG(fprintf(stderr,"Dup2 %d (%d) to %d (%d)\n",    from, PTRDIFF_T_TO_LONG((ptrdiff_t)da_handle[from]), to, x));       return to;   }      PMOD_EXPORT const char *debug_fd_inet_ntop(int af, const void *addr,    char *cp, size_t sz)   {