diff --git a/src/builtin_functions.c b/src/builtin_functions.c
index ce7c02052672eae10b6fc528bb021156b07e911d..2bd25b9a46468e5c36e3ec6183f77907b2e041c0 100644
--- a/src/builtin_functions.c
+++ b/src/builtin_functions.c
@@ -5,7 +5,7 @@
 \*/
 /**/
 #include "global.h"
-RCSID("$Id: builtin_functions.c,v 1.170 1999/05/12 04:40:08 hubbe Exp $");
+RCSID("$Id: builtin_functions.c,v 1.171 1999/05/13 07:25:38 hubbe Exp $");
 #include "interpret.h"
 #include "svalue.h"
 #include "pike_macros.h"
@@ -1505,6 +1505,7 @@ void f_next_object(INT32 args)
     if(sp[-args].type != T_OBJECT)
       SIMPLE_BAD_ARG_ERROR("next_object", 1, "object");
     o=sp[-args].u.object->next;
+    while(o && !o->prog) o=o->next;
   }
   pop_n_elems(args);
   if(!o)
@@ -3908,6 +3909,12 @@ void f__reset_dmalloc(INT32 args)
   pop_n_elems(args);
   reset_debug_malloc();
 }
+
+void f__list_open_fds(INT32 args)
+{
+  extern void list_open_fds(void);
+  list_open_fds();
+}
 #endif
 
 #ifdef PIKE_DEBUG
@@ -4244,6 +4251,7 @@ void init_builtin_efuns(void)
   
 /* function(void:void) */
   ADD_EFUN("_reset_dmalloc",f__reset_dmalloc,tFunc(tVoid,tVoid),OPT_SIDE_EFFECT);
+  ADD_EFUN("_list_open_fds",f__list_open_fds,tFunc(tVoid,tVoid),OPT_SIDE_EFFECT);
 #endif
 #ifdef PIKE_DEBUG
   
diff --git a/src/dmalloc.h b/src/dmalloc.h
index e7087f3fe191b3f38044287aa731f44c32393901..508a00c4d6f6ff5dac36ed24694aa0558a64689d 100644
--- a/src/dmalloc.h
+++ b/src/dmalloc.h
@@ -1,5 +1,5 @@
 /*
- * $Id: dmalloc.h,v 1.12 1999/05/03 07:03:40 hubbe Exp $
+ * $Id: dmalloc.h,v 1.13 1999/05/13 07:25:40 hubbe Exp $
  */
 
 extern char *debug_xalloc(long);
@@ -16,7 +16,7 @@ void low_add_marks_to_memhdr(struct memhdr *to,
 
 extern int verbose_debug_malloc;
 extern int verbose_debug_exit;
-extern void dmalloc_register(void *, int,char *, int);
+extern void dmalloc_register(void *, int,const char *, int);
 extern int dmalloc_unregister(void *, int);
 extern void *debug_malloc(size_t, const char *, int);
 extern void *debug_calloc(size_t, size_t, const char *, int);
@@ -25,6 +25,10 @@ extern void debug_free(void *, const char *, int,int);
 extern char *debug_strdup(const char *, const char *, int);
 extern void reset_debug_malloc(void);
 extern void dmalloc_free(void *p);
+extern int debug_malloc_touch_fd(int, const char *, int);
+extern int debug_malloc_register_fd(int, const char *, int);
+extern int debug_malloc_close_fd(int, const char *, int);
+
 void *debug_malloc_update_location(void *,const char *, int);
 #define malloc(x) debug_malloc((x), __FILE__, __LINE__)
 #define calloc(x, y) debug_calloc((x), (y), __FILE__, __LINE__)
@@ -43,7 +47,13 @@ void debug_malloc_dump_references(void *x);
 #define DMALLOC_POS ,__FILE__,__LINE__
 #define DMALLOC_PROXY_ARGS ,dmalloc_file,dmalloc_line
 void dmalloc_accept_leak(void *);
+#define dmalloc_touch_fd(X) debug_malloc_touch_fd((X),__FILE__,__LINE__)
+#define dmalloc_register_fd(X) debug_malloc_register_fd((X),__FILE__,__LINE__)
+#define dmalloc_close_fd(X) debug_malloc_close_fd((X),__FILE__,__LINE__)
 #else
+#define dmalloc_touch_fd(X) (X)
+#define dmalloc_register_fd(X) (X)
+#define dmalloc_close_fd(X) (X)
 #define dmfree(X) free((X))
 #define dmalloc_accept_leak(X) (void)(X)
 #define DMALLOC_LINE_ARGS 
diff --git a/src/fdlib.c b/src/fdlib.c
index 5f310ee211e8a621386dcccd77a2c481811d5efd..6df4029196c279580ac66dd839a45d92f461b81f 100644
--- a/src/fdlib.c
+++ b/src/fdlib.c
@@ -3,7 +3,7 @@
 #include "error.h"
 #include <math.h>
 
-RCSID("$Id: fdlib.c,v 1.23 1999/01/01 01:03:30 hubbe Exp $");
+RCSID("$Id: fdlib.c,v 1.24 1999/05/13 07:25:41 hubbe Exp $");
 
 #ifdef HAVE_WINSOCK_H
 
@@ -19,7 +19,7 @@ int first_free_handle;
 
 #define FDDEBUG(X)
 
-char *fd_info(int fd)
+char *debug_fd_info(int fd)
 {
   if(fd<0)
     return "BAD";
@@ -37,7 +37,7 @@ char *fd_info(int fd)
   }
 }
 
-int fd_query_properties(int fd, int guess)
+int debug_fd_query_properties(int fd, int guess)
 {
   switch(fd_type[fd])
   {
@@ -86,7 +86,7 @@ void fd_exit()
   mt_destroy(&fd_mutex);
 }
 
-FD fd_open(char *file, int open_mode, int create_mode)
+FD debug_fd_open(char *file, int open_mode, int create_mode)
 {
   HANDLE x;
   FD fd;
@@ -168,7 +168,7 @@ FD fd_open(char *file, int open_mode, int create_mode)
   return fd;
 }
 
-FD fd_socket(int domain, int type, int proto)
+FD debug_fd_socket(int domain, int type, int proto)
 {
   FD fd;
   SOCKET s;
@@ -202,7 +202,7 @@ FD fd_socket(int domain, int type, int proto)
   return fd;
 }
 
-int fd_pipe(int fds[2])
+int debug_fd_pipe(int fds[2] DMALLOC_LINE_ARGS)
 {
   HANDLE files[2];
   mt_lock(&fd_mutex);
@@ -237,10 +237,15 @@ int fd_pipe(int fds[2])
   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_file, dmalloc_line );
+  debug_malloc_register_fd( fds[1], dmalloc_file, dmalloc_line );
+#endif
+  
   return 0;
 }
 
-FD fd_accept(FD fd, struct sockaddr *addr, int *addrlen)
+FD debug_fd_accept(FD fd, struct sockaddr *addr, int *addrlen)
 {
   FD new_fd;
   SOCKET s;
@@ -284,7 +289,7 @@ FD fd_accept(FD fd, struct sockaddr *addr, int *addrlen)
 
 
 #define SOCKFUN(NAME,X1,X2) \
-int PIKE_CONCAT(fd_,NAME) X1 { SOCKET ret; \
+int PIKE_CONCAT(debug_fd_,NAME) X1 { SOCKET ret; \
   FDDEBUG(fprintf(stderr, #NAME " on %d (%d)\n",fd,da_handle[fd])); \
   mt_lock(&fd_mutex); \
   if(fd_type[fd] != FD_SOCKET) { \
@@ -317,7 +322,18 @@ int PIKE_CONCAT(fd_,NAME) X1 { SOCKET ret; \
 
 
 SOCKFUN2(bind, struct sockaddr *, int)
-int fd_connect (FD fd, struct sockaddr *a, int len)
+SOCKFUN4(getsockopt,int,int,void*,int*)
+SOCKFUN4(setsockopt,int,int,void*,int)
+SOCKFUN3(recv,void *,int,int)
+SOCKFUN2(getsockname,struct sockaddr *,int)
+SOCKFUN2(getpeername,struct sockaddr *,int)
+SOCKFUN5(recvfrom,void *,int,int,struct sockaddr *,int*)
+SOCKFUN3(send,void *,int,int)
+SOCKFUN5(sendto,void *,int,int,struct sockaddr *,int*)
+SOCKFUN1(shutdown, int)
+SOCKFUN1(listen, int)
+
+int debug_fd_connect (FD fd, struct sockaddr *a, int len)
 {
   SOCKET ret;
   mt_lock(&fd_mutex);
@@ -339,18 +355,8 @@ int fd_connect (FD fd, struct sockaddr *a, int len)
   FDDEBUG(fprintf(stderr, "connect returned %d (%d)\n",ret,errno)); 
   return (int)ret; 
 }
-SOCKFUN4(getsockopt,int,int,void*,int*)
-SOCKFUN4(setsockopt,int,int,void*,int)
-SOCKFUN3(recv,void *,int,int)
-SOCKFUN2(getsockname,struct sockaddr *,int)
-SOCKFUN2(getpeername,struct sockaddr *,int)
-SOCKFUN5(recvfrom,void *,int,int,struct sockaddr *,int*)
-SOCKFUN3(send,void *,int,int)
-SOCKFUN5(sendto,void *,int,int,struct sockaddr *,int*)
-SOCKFUN1(shutdown, int)
-SOCKFUN1(listen, int)
 
-int fd_close(FD fd)
+int debug_fd_close(FD fd)
 {
   long h;
   int type;
@@ -389,7 +395,7 @@ int fd_close(FD fd)
   return 0;
 }
 
-long fd_write(FD fd, void *buf, long len)
+long debug_fd_write(FD fd, void *buf, long len)
 {
   DWORD ret;
   long handle;
@@ -431,7 +437,7 @@ long fd_write(FD fd, void *buf, long len)
   }
 }
 
-long fd_read(FD fd, void *to, long len)
+long debug_fd_read(FD fd, void *to, long len)
 {
   DWORD ret;
   int rret;
@@ -481,7 +487,7 @@ long fd_read(FD fd, void *to, long len)
   }
 }
 
-long fd_lseek(FD fd, long pos, int where)
+long debug_fd_lseek(FD fd, long pos, int where)
 {
   long ret;
   mt_lock(&fd_mutex);
@@ -511,7 +517,7 @@ long fd_lseek(FD fd, long pos, int where)
   return ret;
 }
 
-int fd_flock(FD fd, int oper)
+int debug_fd_flock(FD fd, int oper)
 {
   long ret;
   mt_lock(&fd_mutex);
@@ -570,7 +576,7 @@ static long convert_filetime_to_time_t(FILETIME tmp)
   return (long)floor(t);
 }
 
-int fd_fstat(FD fd, struct stat *s)
+int debug_fd_fstat(FD fd, struct stat *s)
 {
   DWORD x;
 
@@ -621,7 +627,7 @@ int fd_fstat(FD fd, struct stat *s)
   return 0;
 }
 
-int fd_select(int fds, FD_SET *a, FD_SET *b, FD_SET *c, struct timeval *t)
+int debug_fd_select(int fds, FD_SET *a, FD_SET *b, FD_SET *c, struct timeval *t)
 {
   int ret;
   ret=select(fds,a,b,c,t);
@@ -634,7 +640,7 @@ int fd_select(int fds, FD_SET *a, FD_SET *b, FD_SET *c, struct timeval *t)
 }
 
 
-int fd_ioctl(FD fd, int cmd, void *data)
+int debug_fd_ioctl(FD fd, int cmd, void *data)
 {
   int ret;
   FDDEBUG(fprintf(stderr,"ioctl(%d (%d,%d,%p)\n",fd,da_handle[fd],cmd,data));
@@ -657,7 +663,7 @@ int fd_ioctl(FD fd, int cmd, void *data)
 }
 
 
-FD fd_dup(FD from)
+FD debug_fd_dup(FD from)
 {
   FD fd;
   HANDLE x,p=GetCurrentProcess();
@@ -682,7 +688,7 @@ FD fd_dup(FD from)
   return fd;
 }
 
-FD fd_dup2(FD from, FD to)
+FD debug_fd_dup2(FD from, FD to)
 {
   HANDLE x,p=GetCurrentProcess();
   if(!DuplicateHandle(p,(HANDLE)da_handle[from],p,&x,NULL,0,DUPLICATE_SAME_ACCESS))
diff --git a/src/fdlib.h b/src/fdlib.h
index c8b42bf561ef0a11d1517c42c3498fe899a53c03..aa719aaeb001d2ea912e496be9ccb294832d8013 100644
--- a/src/fdlib.h
+++ b/src/fdlib.h
@@ -1,5 +1,5 @@
 /*
- * $Id: fdlib.h,v 1.21 1998/11/22 11:02:47 hubbe Exp $
+ * $Id: fdlib.h,v 1.22 1999/05/13 07:25:42 hubbe Exp $
  */
 #ifndef FDLIB_H
 #define FDLIB_H
@@ -57,23 +57,56 @@
 
 typedef int FD;
 
-#define SOCKFUN1(NAME,T1) int PIKE_CONCAT(fd_,NAME) (FD,T1);
-#define SOCKFUN2(NAME,T1,T2) int PIKE_CONCAT(fd_,NAME) (FD,T1,T2);
-#define SOCKFUN3(NAME,T1,T2,T3) int PIKE_CONCAT(fd_,NAME) (FD,T1,T2,T3);
-#define SOCKFUN4(NAME,T1,T2,T3,T4) int PIKE_CONCAT(fd_,NAME) (FD,T1,T2,T3,T4);
-#define SOCKFUN5(NAME,T1,T2,T3,T4,T5) int PIKE_CONCAT(fd_,NAME) (FD,T1,T2,T3,T4,T5);
+#define SOCKFUN1(NAME,T1) int PIKE_CONCAT(debug_fd_,NAME) (FD,T1);
+#define SOCKFUN2(NAME,T1,T2) int PIKE_CONCAT(debug_fd_,NAME) (FD,T1,T2);
+#define SOCKFUN3(NAME,T1,T2,T3) int PIKE_CONCAT(debug_fd_,NAME) (FD,T1,T2,T3);
+#define SOCKFUN4(NAME,T1,T2,T3,T4) int PIKE_CONCAT(debug_fd_,NAME) (FD,T1,T2,T3,T4);
+#define SOCKFUN5(NAME,T1,T2,T3,T4,T5) int PIKE_CONCAT(debug_fd_,NAME) (FD,T1,T2,T3,T4,T5);
+
+
+#define fd_info(fd) debug_fd_info(dmalloc_touch_fd(fd))
+#define fd_query_properties(fd,Y) \
+        debug_fd_query_properties(dmalloc_touch_fd(fd),(Y))
+#define fd_open(X,Y,Z) dmalloc_register_fd(debug_fd_open((X),(Y),(Z)))
+#define fd_socket(X,Y,Z) dmalloc_register_fd(debug_fd_socket((X),(Y),(Z)))
+#define fd_pipe(X) debug_fd_pipe( (X) DMALLOC_POS )
+#define fd_accept(X,Y,Z) dmalloc_register_fd(debug_fd_accept((X),(Y),(Z)))
+
+#define fd_bind(fd,X,Y) debug_fd_bind(dmalloc_touch_fd(fd), (X), (Y))
+#define fd_getsockopt(fd,X,Y,Z,Q) debug_fd_getsockopt(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q))
+#define fd_setsockopt(fd,X,Y,Z,Q) debug_fd_setsockopt(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q))
+#define fd_recv(fd,X,Y,Z) debug_fd_recv(dmalloc_touch_fd(fd), (X), (Y),(Z))
+#define fd_getsockname(fd,X,Y) debug_fd_getsockname(dmalloc_touch_fd(fd), (X), (Y))
+#define fd_getpeername(fd,X,Y) debug_fd_getpeername(dmalloc_touch_fd(fd), (X), (Y))
+#define fd_recvfrom(fd,X,Y,Z,Q,P) debug_fd_recvfrom(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q),(P))
+#define fd_send(fd,X,Y,Z) debug_fd_send(dmalloc_touch_fd(fd), (X), (Y),(Z))
+#define fd_sendto(fd,X,Y,Z,Q,P) debug_fd_sendto(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q),(P))
+#define fd_shutdown(fd,X) debug_fd_shutdown(dmalloc_touch_fd(fd), (X))
+#define fd_listen(fd,X) debug_fd_listen(dmalloc_touch_fd(fd), (X))
+#define fd_close(fd) debug_fd_close(dmalloc_close_fd(fd))
+#define fd_write(fd,X,Y) debug_fd_write(dmalloc_touch_fd(fd),(X),(Y))
+#define fd_read(fd,X,Y) debug_fd_read(dmalloc_touch_fd(fd),(X),(Y))
+#define fd_lseek(fd,X,Y) debug_fd_lseek(dmalloc_touch_fd(fd),(X),(Y))
+#define fd_flock(fd,X) debug_fd_flock(dmalloc_touch_fd(fd),(X))
+#define fd_fstat(fd,X) debug_fd_fstat(dmalloc_touch_fd(fd),(X))
+#define fd_select debug_fd_select /* fixme */
+#define fd_ioctl(fd,X,Y) debug_fd_ioctl(dmalloc_touch_fd(fd),(X),(Y))
+#define fd_dup(fd) dmalloc_register_fd(debug_fd_dup(dmalloc_touch_fd(fd)))
+#define fd_dup2(fd,to) dmalloc_register_fd(debug_fd_dup2(dmalloc_touch_fd(fd),dmalloc_close_fd(to)))
+#define fd_connect(fd,X,Z) debug_fd_connect(dmalloc_touch_fd(fd),(X),(Z))
+
 
 /* Prototypes begin here */
-char *fd_info(int fd);
-int fd_query_properties(int fd, int guess);
+char *debug_fd_info(int fd);
+int debug_fd_query_properties(int fd, int guess);
 void fd_init();
 void fd_exit();
-FD fd_open(char *file, int open_mode, int create_mode);
-FD fd_socket(int domain, int type, int proto);
-int fd_pipe(int fds[2]);
-FD fd_accept(FD fd, struct sockaddr *addr, int *addrlen);
+FD debug_fd_open(char *file, int open_mode, int create_mode);
+FD debug_fd_socket(int domain, int type, int proto);
+int debug_fd_pipe(int fds[2] DMALLOC_LINE_ARGS);
+FD debug_fd_accept(FD fd, struct sockaddr *addr, int *addrlen);
 SOCKFUN2(bind, struct sockaddr *, int)
-int fd_connect (FD fd, struct sockaddr *a, int len);
+int debug_fd_connect (FD fd, struct sockaddr *a, int len);
 SOCKFUN4(getsockopt,int,int,void*,int*)
 SOCKFUN4(setsockopt,int,int,void*,int)
 SOCKFUN3(recv,void *,int,int)
@@ -82,16 +115,16 @@ SOCKFUN3(send,void *,int,int)
 SOCKFUN5(sendto,void *,int,int,struct sockaddr *,int*)
 SOCKFUN1(shutdown, int)
 SOCKFUN1(listen, int)
-int fd_close(FD fd);
-long fd_write(FD fd, void *buf, long len);
-long fd_read(FD fd, void *to, long len);
-long fd_lseek(FD fd, long pos, int where);
-int fd_flock(FD fd, int oper);
-int fd_fstat(FD fd, struct stat *s);
-int fd_select(int fds, FD_SET *a, FD_SET *b, FD_SET *c, struct timeval *t);
-int fd_ioctl(FD fd, int cmd, void *data);
-FD fd_dup(FD from);
-FD fd_dup2(FD from, FD to);
+int debug_fd_close(FD fd);
+long debug_fd_write(FD fd, void *buf, long len);
+long debug_fd_read(FD fd, void *to, long len);
+long debug_fd_lseek(FD fd, long pos, int where);
+int debug_fd_flock(FD fd, int oper);
+int debug_fd_fstat(FD fd, struct stat *s);
+int debug_fd_select(int fds, FD_SET *a, FD_SET *b, FD_SET *c, struct timeval *t);
+int debug_fd_ioctl(FD fd, int cmd, void *data);
+FD debug_fd_dup(FD from);
+FD debug_fd_dup2(FD from, FD to);
 /* Prototypes end here */
 
 #undef SOCKFUN1
@@ -209,42 +242,46 @@ typedef int FD;
 #define fd_EXCL O_EXCL
 
 #define fd_query_properties(X,Y) ( fd_INTERPROCESSABLE | (Y))
-#define fd_open open
+
+#define fd_open(X,Y,Z) dmalloc_register_fd(open((X),(Y),(Z)))
+#define fd_socket(X,Y,Z) dmalloc_register_fd(socket((X),(Y),(Z)))
+#define fd_pipe pipe /* FIXME */
+#define fd_accept(X,Y,Z) dmalloc_register_fd(accept((X),(Y),(Z)))
+
+#define fd_bind(fd,X,Y) bind(dmalloc_touch_fd(fd), (X), (Y))
+#define fd_getsockopt(fd,X,Y,Z,Q) getsockopt(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q))
+#define fd_setsockopt(fd,X,Y,Z,Q) setsockopt(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q))
+#define fd_recv(fd,X,Y,Z) recv(dmalloc_touch_fd(fd), (X), (Y),(Z))
+#define fd_getsockname(fd,X,Y) getsockname(dmalloc_touch_fd(fd), (X), (Y))
+#define fd_getpeername(fd,X,Y) getpeername(dmalloc_touch_fd(fd), (X), (Y))
+#define fd_recvfrom(fd,X,Y,Z,Q,P) recvfrom(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q),(P))
+#define fd_send(fd,X,Y,Z) send(dmalloc_touch_fd(fd), (X), (Y),(Z))
+#define fd_sendto(fd,X,Y,Z,Q,P) sendto(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q),(P))
+#define fd_shutdown(fd,X) shutdown(dmalloc_touch_fd(fd), (X))
+#define fd_listen(fd,X) listen(dmalloc_touch_fd(fd), (X))
+
 #ifdef HAVE_BROKEN_F_SETFD
-#define fd_close(FD__) (set_close_on_exec(FD__,0), close(FD__))
+#define fd_close(fd) (set_close_on_exec(fd,0),close(dmalloc_close_fd(fd)))
 #else /* !HAVE_BROKEN_F_SETFD */
-#define fd_close close
+#define fd_close(fd) close(dmalloc_close_fd(fd))
 #endif /* HAVE_BROKEN_F_SETFD */
-#define fd_read read
-#define fd_write write
-#define fd_ioctl ioctl
-#define fd_pipe pipe
-
-#define fd_socket socket
-#define fd_bind bind
-#define fd_connect connect
-#define fd_getsockopt getsockopt
-#define fd_setsockopt setsockopt
-#define fd_getsockname getsockname
-#define fd_getpeername getpeername
-#define fd_recv recv
-#define fd_send send
-#define fd_sendto sendto
-#define fd_recvfrom recvfrom
-#define fd_shutdown shutdown
-#define fd_accept accept
-#define fd_lseek lseek
-#define fd_fstat fstat
-#define fd_dup dup
+
+#define fd_write(fd,X,Y) write(dmalloc_touch_fd(fd),(X),(Y))
+#define fd_read(fd,X,Y) read(dmalloc_touch_fd(fd),(X),(Y))
+#define fd_lseek(fd,X,Y) lseek(dmalloc_touch_fd(fd),(X),(Y))
+#define fd_fstat(fd,X) fstat(dmalloc_touch_fd(fd),(X))
+#define fd_select select /* fixme */
+#define fd_ioctl(fd,X,Y) ioctl(dmalloc_touch_fd(fd),(X),(Y))
+#define fd_dup(fd) dmalloc_register_fd(dup(dmalloc_touch_fd(fd)))
+#define fd_connect(fd,X,Z) connect(dmalloc_touch_fd(fd),(X),(Z))
+
 #ifdef HAVE_BROKEN_F_SETFD
-#define fd_dup2(FD__, FD2__)  (set_close_on_exec(FD2__, 0), dup2(FD__, FD2__))
+#define fd_dup2(fd,to) (set_close_on_exec(to,0), dmalloc_register_fd(dup2(dmalloc_touch_fd(fd),dmalloc_close_fd(to))))
 #else /* !HAVE_BROKEN_F_SETFD */
-#define fd_dup2 dup2
+#define fd_dup2(fd,to) dmalloc_register_fd(dup2(dmalloc_touch_fd(fd),dmalloc_close_fd(to)))
 #endif /* HAVE_BROKEN_F_SETFD */
-#define fd_listen listen
 
-#define fd_select select
-#define fd_socketpair socketpair
+#define fd_socketpair socketpair /* fixme */
 
 #define fd_fd_set fd_set
 #define fd_FD_CLR FD_CLR
@@ -254,7 +291,7 @@ typedef int FD;
 
 #ifdef HAVE_FLOCK
 #define HAVE_FD_FLOCK
-#define fd_flock flock
+#define fd_flock(fd,X) flock(dmalloc_touch_fd(fd),(X))
 #define fd_LOCK_SH LOCK_SH
 #define fd_LOCK_EX LOCK_EX
 #define fd_LOCK_UN LOCK_UN
@@ -265,13 +302,12 @@ typedef int FD;
 #define fd_LOCK_EX F_LOCK
 #define fd_LOCK_UN F_ULOCK
 #define fd_LOCK_NB F_TLOCK
-#define fd_lockf(fd,mode) lockf(fd,mode,0)
+
+#define fd_flock(fd,mode) lockf(dmalloc_touch_fd(fd),mode,0)
 #endif
 #endif
 
 
-
-
 #define fd_shutdown_read 0
 #define fd_shutdown_write 1
 #define fd_shutdown_both 2
diff --git a/src/pike_memory.c b/src/pike_memory.c
index e5ab0aec27cccd8a919251b07a01fe307a100cc4..9d2192ebf856c8f5b1a5f53275657815f47fff7b 100644
--- a/src/pike_memory.c
+++ b/src/pike_memory.c
@@ -10,7 +10,7 @@
 #include "pike_macros.h"
 #include "gc.h"
 
-RCSID("$Id: pike_memory.c,v 1.38 1999/05/03 07:03:41 hubbe Exp $");
+RCSID("$Id: pike_memory.c,v 1.39 1999/05/13 07:25:43 hubbe Exp $");
 
 /* strdup() is used by several modules, so let's provide it */
 #ifndef HAVE_STRDUP
@@ -711,6 +711,10 @@ char *do_pad(char *mem, long size)
   return mem;
 }
 
+#define FD2PTR(X) (void *)((X)*4+1)
+#define PTR2FD(X) (((long)(X))>>2)
+
+
 void check_pad(struct memhdr *mh, int freeok)
 {
   static int out_biking=0;
@@ -960,7 +964,7 @@ static struct memhdr *low_make_memhdr(void *p, int s, int locnum)
   return mh;
 }
 
-void dmalloc_register(void *p, int s, char *file, int line)
+void dmalloc_register(void *p, int s, const char *file, int line)
 {
   low_make_memhdr(p,s,location_number(file, line));
 }
@@ -1138,7 +1142,43 @@ void debug_malloc_dump_references(void *x)
   dump_memhdr_locations(my_find_memhdr(x,0),0);
 }
 
-void cleanup_memhdrs()
+void list_open_fds(void)
+{
+  unsigned long h;
+  mt_lock(&debug_malloc_mutex);
+
+  for(h=0;h<memhdr_hash_table_size;h++)
+  {
+    struct memhdr *m;
+    for(m=memhdr_hash_table[h];m;m=m->next)
+    {
+      struct memhdr *tmp;
+      struct memloc *l;
+      void *p=m->data;
+      
+      if( 1 & (long) p )
+      {
+	if( FD2PTR( PTR2FD(p) ) == p)
+	{
+	  fprintf(stderr,"Filedescriptor %ld\n",PTR2FD(p));
+
+	  for(l=m->locations;l;l=l->next)
+	  {
+	    struct fileloc *f=find_file_location(l->locnum);
+	    fprintf(stderr,"  *** %s:%d (%d times) %s\n",
+		    f->file,
+		    f->line,
+		    l->times,
+		    find_location(&no_leak_memlocs, l->locnum) ? "" : " *");
+	  }
+	}
+      }
+    }
+  }
+  mt_unlock(&debug_malloc_mutex);
+}
+
+void cleanup_memhdrs(void)
 {
   unsigned long h;
   mt_lock(&debug_malloc_mutex);
@@ -1178,11 +1218,18 @@ void cleanup_memhdrs()
 	  first=0;
 	}
 
-	
 	fprintf(stderr, "LEAK: (%p) %ld bytes\n",p, m->size);
+	if( 1 & (long) p )
+	{
+	  if( FD2PTR( PTR2FD(p) ) == p)
+	  {
+	    fprintf(stderr," Filedescriptor %ld\n",PTR2FD(p));
+	  }
+	}else{
 #ifdef PIKE_DEBUG
-	describe_something(p, attempt_to_identify(p),0);
+	  describe_something(p, attempt_to_identify(p),0);
 #endif
+	}
 	mt_lock(&debug_malloc_mutex);
 
 	/* Now we must reassure 'm' */
@@ -1239,6 +1286,27 @@ void * debug_malloc_update_location(void *p,const char *fn, int line)
   return p;
 }
 
+int debug_malloc_touch_fd(int fd, const char *fn, int line)
+{
+  if(fd==-1) return fd;
+  debug_malloc_update_location( FD2PTR(fd), fn, line);
+  return fd;
+}
+
+int debug_malloc_register_fd(int fd, const char *fn, int line)
+{
+  if(fd==-1) return fd;
+  dmalloc_register( FD2PTR(fd), 0 , fn, line);
+  return fd;
+}
+
+int debug_malloc_close_fd(int fd, const char *fn, int line)
+{
+  if(fd==-1) return fd;
+  dmalloc_unregister( FD2PTR(fd), 1);
+  return fd;
+}
+
 void reset_debug_malloc(void)
 {
   INT32 h;