diff --git a/src/configure.in b/src/configure.in
index c0771bd238c2bf91d8db7c7b2caad04c681221ce..2815e3db024c6056999f8b621fbfd971814fc78c 100644
--- a/src/configure.in
+++ b/src/configure.in
@@ -1,4 +1,4 @@
-AC_REVISION("$Id: configure.in,v 1.1055 2008/06/24 01:43:46 mast Exp $")
+AC_REVISION("$Id: configure.in,v 1.1056 2008/06/28 19:23:05 mast Exp $")
 AC_INIT(interpret.c)
 AC_CONFIG_HEADER(machine.h)
 
@@ -2361,7 +2361,7 @@ if test "x$PIKE_PATH_TRANSLATE" = "x"; then
     # MinGW system.
     cat <<\EOF
 #!/bin/sh
-# Automatically generated by $Id: configure.in,v 1.1055 2008/06/24 01:43:46 mast Exp $.
+# Automatically generated by $Id: configure.in,v 1.1056 2008/06/28 19:23:05 mast Exp $.
 # MinGW-version. Do NOT edit.
 posix_name="`cat`"
 posix_prefix="/"
@@ -2399,7 +2399,7 @@ EOF
     # Native POSIX system.
   cat <<\EOF
 #!/bin/sh
-# Automatically generated by $Id: configure.in,v 1.1055 2008/06/24 01:43:46 mast Exp $.
+# Automatically generated by $Id: configure.in,v 1.1056 2008/06/28 19:23:05 mast Exp $.
 # POSIX-version. Do NOT edit.
 cat
 EOF
@@ -2408,7 +2408,7 @@ else
   # rntcl-style
   cat <<\EOF
 #!/bin/sh
-# Automatically generated by $Id: configure.in,v 1.1055 2008/06/24 01:43:46 mast Exp $.
+# Automatically generated by $Id: configure.in,v 1.1056 2008/06/28 19:23:05 mast Exp $.
 # RNTCL-version. Do NOT edit.
 sed -e "$PIKE_PATH_TRANSLATE"
 EOF
@@ -8194,7 +8194,12 @@ export PIKE_SRC_DIR
 BUILD_BASE=`pwd`
 export BUILD_BASE
 
-default_master=$prefix/pike/%d.%d.%d/lib/master.pike
+if test "$pike_cv_sys_os" = "Windows_NT"; then
+  # If this doesn't begin with '/', it's relative to pike.exe.
+  default_master=../lib/master.pike
+else
+  default_master=$prefix/pike/%d.%d.%d/lib/master.pike
+fi
 AC_DEFINE_UNQUOTED(DEFAULT_MASTER,"$default_master")
 
 AC_SUBST(OSFLAGS)
diff --git a/src/main.c b/src/main.c
index d0d7d65b1e089250dfac5d960dbaeba43ba61cb7..e1e5df8408aed35616997a8ee570f9dc92387464 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,7 +2,7 @@
 || This file is part of Pike. For copyright information see COPYRIGHT.
 || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
 || for more information.
-|| $Id: main.c,v 1.229 2008/06/05 15:10:45 mast Exp $
+|| $Id: main.c,v 1.230 2008/06/28 19:23:06 mast Exp $
 */
 
 #include "global.h"
@@ -153,13 +153,33 @@ static void set_default_master(void)
 
   if(!master_location[CONSTANT_STRLEN(MASTER_COOKIE)])
   {
-    sprintf(master_location + CONSTANT_STRLEN(MASTER_COOKIE),
-	    DEFAULT_MASTER,
-	    PIKE_MAJOR_VERSION,
-	    PIKE_MINOR_VERSION,
-	    PIKE_BUILD_VERSION);
+    SNPRINTF (master_location + CONSTANT_STRLEN(MASTER_COOKIE),
+	      sizeof (master_location) - CONSTANT_STRLEN (MASTER_COOKIE),
+	      DEFAULT_MASTER,
+	      PIKE_MAJOR_VERSION,
+	      PIKE_MINOR_VERSION,
+	      PIKE_BUILD_VERSION);
   }
 
+#ifdef __NT__
+  if (master_location[CONSTANT_STRLEN (MASTER_COOKIE)] != '/' &&
+      master_location[CONSTANT_STRLEN (MASTER_COOKIE)] != '\\') {
+    char exepath[MAXPATHLEN];
+    if (!GetModuleFileName (NULL, exepath, _MAX_PATH))
+      fprintf (stderr, "Failed to get path to exe file: %d\n",
+	       GetLastError());
+    else {
+      char tmp[MAXPATHLEN * 2];
+      char *p = strrchr (exepath, '\\');
+      if (p) *p = 0;
+      SNPRINTF (tmp, sizeof (tmp), "%s/%s", exepath,
+		master_location + CONSTANT_STRLEN (MASTER_COOKIE));
+      strncpy (master_location + CONSTANT_STRLEN (MASTER_COOKIE), tmp,
+	       sizeof (master_location) - CONSTANT_STRLEN (MASTER_COOKIE));
+    }
+  }
+#endif
+
   TRACE((stderr, "Default master at \"%s\"...\n",
 	 master_location + CONSTANT_STRLEN(MASTER_COOKIE)));
 }