Index: getopt/getopt.c
--- getopt/getopt.c.orig	2002-09-07 04:27:51 +0200
+++ getopt/getopt.c	2007-10-23 08:55:09 +0200
@@ -40,6 +40,7 @@
 #endif
 
 #include <stdio.h>
+#include <string.h>
 
 /* Comment out all this code if we are using the GNU C Library, and are not
    actually compiling the library itself.  This code is part of the GNU C
Index: src/Makefile.in
--- src/Makefile.in.orig	2005-05-30 04:13:28 +0200
+++ src/Makefile.in	2007-10-23 08:55:09 +0200
@@ -73,7 +73,7 @@
 VERSION = @VERSION@
 YACC = @YACC@
 
-CFLAGS = -Wall -ggdb -O3 -DPORTFWD_CONF=\"$(sysconfdir)/portfwd.cfg\"
+CFLAGS = -DPORTFWD_CONF=\"$(sysconfdir)/portfwd.cfg\"
 CXXFLAGS = $(CFLAGS)
 INCLUDES = -I$(top_srcdir)/src
 
Index: src/director.cc
--- src/director.cc.orig	2005-05-30 04:13:28 +0200
+++ src/director.cc	2007-10-23 08:55:09 +0200
@@ -195,7 +195,7 @@
   address.len = 0;
   address.addr = (char *) malloc(address_buf_size * sizeof(char *));
   if (!address.addr) {
-    syslog(LOG_ERR, "director::director(): malloc(%d) failed", address_buf_size);
+    syslog(LOG_ERR, "director::director(): malloc(%ld) failed", address_buf_size);
     exit(1);
   }
 }
@@ -352,7 +352,7 @@
    */
 
   if (addr_buf_len > address_buf_size) {
-    syslog(LOG_ERR, "Insufficient space in local buffer for address (local_buffer_size=%d < address_length=%d)", address_buf_size, addr_buf_len);
+    syslog(LOG_ERR, "Insufficient space in local buffer for address (local_buffer_size=%ld < address_length=%ld)", (long)address_buf_size, (long)addr_buf_len);
     return -1;
   }
 
Index: src/portfwd.cc
--- src/portfwd.cc.orig	2005-05-30 04:13:28 +0200
+++ src/portfwd.cc	2007-10-23 08:55:09 +0200
@@ -37,6 +37,8 @@
 
 int on_the_fly_dns = 0;
 int foreground = 0;
+int dmz = -1;
+const char *pidfile = NULL;
 
 void usage(FILE *out) 
 {
@@ -52,7 +54,8 @@
 
 	       "       -f               | --on-the-fly-dns\n"
 	       "       -g               | --foreground\n"
-	       "       -c <config-file> | --config <config-file>\n",
+	       "       -c <config-file> | --config <config-file>\n"
+	       "       -p <pid-file>    | --pidfile <pid-file>\n",
 	  prog);
 }
 
@@ -82,14 +85,16 @@
     {"on-the-fly-dns", 0, 0, 'f'},
     {"foreground", 0, 0, 'g'},
     {"config", 1, 0, 'c'},
+    {"pidfile", 1, 0, 'p'},
     {0, 0, 0, 0}
   };
 
   *config  = 0;
+  pidfile = 0;
 
   for (;;) {
 		 
-    opt = getopt_long(argc, (char ** const) argv, "hvdtfgc:", long_options, &option_index);
+    opt = getopt_long(argc, (char ** const) argv, "hvdtfgc:p:", long_options, &option_index);
     if (opt == -1)
       break;
 
@@ -125,6 +130,13 @@
       }
       *config = optarg;
       break;
+    case 'p':
+      if (pidfile) {
+	fprintf(stderr, "%s: Pid-file redefinition: %s", me, optarg);
+	exit(1);
+      }
+      pidfile = optarg;
+      break;
     case '?':
       usage(stderr);
       exit(1);
@@ -246,6 +258,9 @@
 
   closelog();
 
+  if (!dmz && pidfile)
+      unlink(pidfile);
+
   exit(0);
 }
 
@@ -291,7 +306,7 @@
    * Go to background.
    */
   if (!foreground) {
-    int dmz = daemonize();
+    dmz = daemonize(pidfile);
     if (dmz) {
       syslog(LOG_ERR, "daemonize() failed: %d", dmz);
       exit(1);
Index: src/portfwd.h
--- src/portfwd.h.orig	2005-05-30 04:13:28 +0200
+++ src/portfwd.h	2007-10-23 08:55:09 +0200
@@ -18,6 +18,7 @@
 
 extern int on_the_fly_dns;
 extern int foreground;
+extern const char *pidfile;
 
 #endif /* PORTFWD_H */
 
Index: src/util.cc
--- src/util.cc.orig	2005-05-30 04:13:28 +0200
+++ src/util.cc	2007-10-23 08:55:09 +0200
@@ -118,7 +118,7 @@
   return 0;
 }
 
-int daemonize()
+int daemonize(const char *pidfile)
 {
   ONVERBOSE(syslog(LOG_INFO, "Daemonizing"));
   
@@ -146,6 +146,20 @@
       return -1;
     }
 
+    if (pidfile) {
+      int pidfd;
+      size_t n;
+      char buf[20];
+      n = snprintf(buf,sizeof(buf),"%lu",(unsigned long)pid);
+      if (n < sizeof(buf)) {
+        pidfd = open(pidfile, O_CREAT|O_TRUNC|O_WRONLY, 0666);
+        if (pidfd != -1) {
+          write(pidfd,buf,n);
+          close(pidfd);
+        }
+      }
+    }
+
     /* 
      * Parent exits
      */
Index: src/util.h
--- src/util.h.orig	2005-05-30 04:13:28 +0200
+++ src/util.h	2007-10-23 08:55:09 +0200
@@ -28,7 +28,7 @@
 
 int cd_root();
 int std_to_null();
-int daemonize();
+int daemonize(const char *pidfile);
 
 void socket_close(int fd); 
 
Index: src/util.hpp
--- src/util.hpp.orig	2001-05-15 02:25:01 +0200
+++ src/util.hpp	2007-10-23 08:55:09 +0200
@@ -15,7 +15,7 @@
 {
   sample = new T[size];
   if (!sample) {
-    syslog(LOG_EMERG, "%s:%s (%d x %d)\n", "safe_new()", "could not allocate array", size, sizeof(T));
+    syslog(LOG_EMERG, "%s:%s (%ld x %ld)\n", "safe_new()", "could not allocate array", (long)size, (long)sizeof(T));
     exit(1);
   }
 
Index: src/vector.hpp
--- src/vector.hpp.orig	2005-06-28 23:46:27 +0200
+++ src/vector.hpp	2007-10-23 08:55:09 +0200
@@ -55,8 +55,6 @@
   T *begin_ptr() const;
   T *past_end_ptr() const;
   static T *next_ptr(T *i);
-
-  friend std::ostream& operator << <T> (std::ostream& out, const vector<T>& v);
 };
 
 template <class T>
