Index: /trunk/src/kmk/kmkbuiltin/install.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/install.c	(revision 3214)
+++ /trunk/src/kmk/kmkbuiltin/install.c	(revision 3215)
@@ -122,4 +122,5 @@
 # define IS_SLASH(ch)   ((ch) == '/')
 #endif
+
 
 /*********************************************************************************************************************************
Index: /trunk/src/kmk/kmkbuiltin/ln.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/ln.c	(revision 3214)
+++ /trunk/src/kmk/kmkbuiltin/ln.c	(revision 3215)
@@ -42,4 +42,5 @@
 #endif /* no $id */
 
+#define FAKES_NO_GETOPT_H /* bird */
 #include "config.h"
 #ifndef _MSC_VER
@@ -55,5 +56,5 @@
 #include <string.h>
 #include <unistd.h>
-#include "getopt.h"
+#include "getopt_r.h"
 #ifdef _MSC_VER
 # include "mscfakes.h"
@@ -93,4 +94,5 @@
 {
 	LNINSTANCE This;
+	struct getopt_state_r gos;
 	struct stat sb;
 	char *sourcedir;
@@ -107,11 +109,6 @@
 	This.linkf = NULL;
 
-	/* kmk: reset getopt() and set program name. */
-	opterr = 1;
-	optarg = NULL;
-	optopt = 0;
-	optind = 0; /* init */
-
-	while ((ch = getopt_long(argc, argv, "fhinsv", long_options, NULL)) != -1)
+	getopt_initialize_r(&gos, argc, argv, "fhinsv", long_options, envp, pCtx);
+	while ((ch = getopt_long_r(&gos, NULL)) != -1)
 		switch (ch) {
 		case 'f':
@@ -143,6 +140,6 @@
 		}
 
-	argv += optind;
-	argc -= optind;
+	argv += gos.optind;
+	argc -= gos.optind;
 
 	This.linkf = This.sflag ? symlink : link;
Index: /trunk/src/kmk/kmkbuiltin/mkdir.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/mkdir.c	(revision 3214)
+++ /trunk/src/kmk/kmkbuiltin/mkdir.c	(revision 3215)
@@ -42,4 +42,5 @@
 #endif
 
+#define FAKES_NO_GETOPT_H /* bird */
 #include "config.h"
 #include <sys/types.h>
@@ -61,5 +62,5 @@
 # include <alloca.h>
 #endif
-#include "getopt.h"
+#include "getopt_r.h"
 #ifdef __HAIKU__
 # include "haikufakes.h"
@@ -90,4 +91,5 @@
 kmk_builtin_mkdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
 {
+	struct getopt_state_r gos;
 	int ch, exitval, success, pflag, vflag;
 	mode_t omode, *set = (mode_t *)NULL;
@@ -97,13 +99,9 @@
 	mode = NULL;
 
-	/* kmk: reset getopt and set progname */
-	opterr = 1;
-	optarg = NULL;
-	optopt = 0;
-	optind = 0; /* init */
-	while ((ch = getopt_long(argc, argv, "m:pv", long_options, NULL)) != -1)
+	getopt_initialize_r(&gos, argc, argv, "m:pv", long_options, envp, pCtx);
+	while ((ch = getopt_long_r(&gos, NULL)) != -1)
 		switch(ch) {
 		case 'm':
-			mode = optarg;
+			mode = gos.optarg;
 			break;
 		case 'p':
@@ -123,6 +121,6 @@
 		}
 
-	argc -= optind;
-	argv += optind;
+	argc -= gos.optind;
+	argv += gos.optind;
 	if (argv[0] == NULL)
 		return usage(pCtx, 1);
Index: /trunk/src/kmk/kmkbuiltin/mv.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/mv.c	(revision 3214)
+++ /trunk/src/kmk/kmkbuiltin/mv.c	(revision 3215)
@@ -51,4 +51,5 @@
 *   Header Files                                                                                                                 *
 *********************************************************************************************************************************/
+#define FAKES_NO_GETOPT_H /* bird */
 #include "config.h"
 #include <sys/types.h>
@@ -80,5 +81,5 @@
 #endif
 #include <unistd.h>
-#include "getopt.h"
+#include "getopt_r.h"
 #ifdef __sun__
 # include "solfakes.h"
@@ -121,5 +122,5 @@
 
 static int	do_move(PMVINSTANCE, char *, char *);
-#ifdef CROSS_DEVICE_MOVE
+#if 0 // def CROSS_DEVICE_MOVE
 static int	fastcopy(char *, char *, struct stat *);
 static int	copy(char *, char *);
@@ -128,32 +129,9 @@
 
 
-#if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(__DragonFly__) && !defined(__OpenBSD__)
-# ifdef __OS2__
-static
-# endif
-const char *user_from_uid(uid_t id, int x)
-{
-	static char s_buf[64];
-	sprintf(s_buf, "%ld", (long int)id);
-	(void)x;
-	return s_buf;
-}
-# ifdef __OS2__
-static
-# endif
-const char *group_from_gid(gid_t id, int x)
-{
-	static char s_buf[64];
-	sprintf(s_buf, "%ld", (long int)id);
-	(void)x;
-	return s_buf;
-}
-#endif /* 'not in libc' */
-
-
 int
 kmk_builtin_mv(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
 {
 	MVINSTANCE This;
+	struct getopt_state_r gos;
 	size_t baselen, len;
 	int rval;
@@ -170,11 +148,6 @@
 	This.vflg = 0;
 
-	/* kmk: reset getopt and set progname */
-	opterr = 1;
-	optarg = NULL;
-	optopt = 0;
-	optind = 0; /* init */
-
-	while ((ch = getopt_long(argc, argv, "finv", long_options, NULL)) != -1)
+	getopt_initialize_r(&gos, argc, argv, "finv", long_options, envp, pCtx);
+	while ((ch = getopt_long_r(&gos, NULL)) != -1)
 		switch (ch) {
 		case 'i':
@@ -201,6 +174,6 @@
 			return usage(pCtx, 1);
 		}
-	argc -= optind;
-	argv += optind;
+	argc -= gos.optind;
+	argv += gos.optind;
 
 	if (argc < 2)
@@ -301,8 +274,15 @@
 		} else if (access(to, W_OK) && !stat(to, &sb)) {
 			bsd_strmode(sb.st_mode, modep);
+#if 0 /* probably not thread safe, also BSDism. */
 			(void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
 			    modep + 1, modep[9] == ' ' ? "" : " ",
 			    user_from_uid((unsigned long)sb.st_uid, 0),
 			    group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
+#else
+			(void)fprintf(stderr, "override %s%s%ul/%ul for %s? %s",
+			              modep + 1, modep[9] == ' ' ? "" : " ",
+			              (unsigned long)sb.st_uid, (unsigned long)sb.st_gid,
+			              to, YESNO);
+#endif
 			ask = 1;
 		}
@@ -335,5 +315,5 @@
 
 	if (errno == EXDEV) {
-#ifndef CROSS_DEVICE_MOVE
+#if 1 //ndef CROSS_DEVICE_MOVE
 		warnx(pThis->pCtx, "cannot move `%s' to a different device: `%s'", from, to);
 		return (1);
@@ -368,5 +348,5 @@
 	}
 
-#ifdef CROSS_DEVICE_MOVE
+#if 0//def CROSS_DEVICE_MOVE
 	/*
 	 * If rename fails because we're trying to cross devices, and
@@ -383,5 +363,5 @@
 }
 
-#ifdef CROSS_DEVICE_MOVE
+#if 0 //def CROSS_DEVICE_MOVE - using static buffers and fork.
 int
 static fastcopy(char *from, char *to, struct stat *sbp)
Index: /trunk/src/kmk/kmkbuiltin/printf.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/printf.c	(revision 3214)
+++ /trunk/src/kmk/kmkbuiltin/printf.c	(revision 3215)
@@ -50,4 +50,5 @@
 *   Header Files                                                                                                                 *
 *********************************************************************************************************************************/
+#define FAKES_NO_GETOPT_H /* bird */
 #if !defined(KMK_BUILTIN_STANDALONE) && !defined(BUILTIN) && !defined(SHELL)
 # include "../makeint.h"
@@ -70,5 +71,5 @@
 #include <string.h>
 #include <unistd.h>
-#include "getopt.h"
+#include "getopt_r.h"
 #ifdef __sun__
 # include "solfakes.h"
@@ -139,4 +140,5 @@
 {
     PKMKBUILTINCTX pCtx;
+    /* former globals */
     size_t b_length;
     char *b_fmt;
@@ -146,4 +148,10 @@
     char *g_o;
 #endif
+    /* former function level statics in common_printf(); both need freeing. */
+    char *a, *t;
+
+    /* former function level statics in conv_expand(); needs freeing. */
+    char *conv_str;
+
     /* Buffer the output because windows doesn't do line buffering of stdout. */
     size_t g_cchBuf;
@@ -167,8 +175,9 @@
 *   Internal Functions                                                                                                           *
 *********************************************************************************************************************************/
-static int	 common_printf(PPRINTFINSTANCE pThis, int argc, char *argv[]);
+static int 	 common_printf(PPRINTFINSTANCE pThis, char *argv[], PKMKBUILTINCTX pCtx);
+static int 	 common_printf_inner(PPRINTFINSTANCE pThis, char *argv[]);
 static void	 conv_escape_str(PPRINTFINSTANCE, char *, void (*)(PPRINTFINSTANCE, int));
 static char	*conv_escape(PPRINTFINSTANCE, char *, char *);
-static char	*conv_expand(const char *);
+static const char *conv_expand(PPRINTFINSTANCE, const char *);
 static int	 getchr(PPRINTFINSTANCE);
 static double	 getdouble(PPRINTFINSTANCE);
@@ -191,23 +200,10 @@
 int kmk_builtin_printf(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
 {
+	PRINTFINSTANCE This;
+	struct getopt_state_r gos;
 	int ch;
-	PRINTFINSTANCE This;
-	This.pCtx = pCtx;
-	This.b_length = 0;
-	This.b_fmt = NULL;
-	This.rval = 0;
-	This.gargv = NULL;
-#ifndef KMK_BUILTIN_STANDALONE
-	This.g_o = NULL;
-#endif
-	This.g_cchBuf = 0;
-
-	/* kmk: reset getopt, set progname and reset buffer. */
-	opterr = 1;
-	optarg = NULL;
-	optopt = 0;
-	optind = 0; /* init */
-
-	while ((ch = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
+
+	getopt_initialize_r(&gos, argc, argv, "", long_options, envp, pCtx);
+	while ((ch = getopt_long_r(&gos, NULL)) != -1) {
 		switch (ch) {
 		case 261:
@@ -221,10 +217,14 @@
 		}
 	}
-	argc -= optind;
-	argv += optind;
+	argc -= gos.optind;
+	argv += gos.optind;
 
 	if (argc < 1)
 		return usage(pCtx, 1);
-	return common_printf(&This, argc, argv);
+
+#ifndef KMK_BUILTIN_STANDALONE
+	This.g_o = NULL;
+#endif
+	return common_printf(&This, argv, pCtx);
 }
 
@@ -249,13 +249,6 @@
 	    fatal(NILF, strlen(funcname) + INTSTR_LENGTH, _("$(%s): no format string\n"), funcname);
 
-	This.pCtx = NULL;
-	This.b_length = 0;
-	This.b_fmt = NULL;
-	This.rval = 0;
-	This.gargv = NULL;
-	This.g_cchBuf = 0;
 	This.g_o = o;
-
-	rc = common_printf(&This, argc, argv);
+	rc = common_printf(&This, argv, NULL);
 	o = This.g_o;
 
@@ -266,5 +259,38 @@
 #endif /* KMK_BUILTIN_STANDALONE */
 
-static int common_printf(PPRINTFINSTANCE pThis, int argc, char *argv[])
+static int common_printf(PPRINTFINSTANCE pThis, char *argv[], PKMKBUILTINCTX pCtx)
+{
+	int rc;
+
+	/* Init all but g_o. */
+	pThis->pCtx = pCtx;
+	pThis->b_length = 0;
+	pThis->b_fmt = NULL;
+	pThis->rval = 0;
+	pThis->gargv = NULL;
+	pThis->g_cchBuf = 0;
+	pThis->a = NULL;
+	pThis->t = NULL;
+	pThis->conv_str = NULL;
+
+	rc = common_printf_inner(pThis, argv);
+
+	/* Cleanup allocations. */
+	if (pThis->a) {
+		free(pThis->a);
+		pThis->a = NULL;
+	}
+	if (pThis->t) {
+		free(pThis->t);
+		pThis->t = NULL;
+	}
+	if (pThis->conv_str) {
+		free(pThis->conv_str);
+		pThis->conv_str = NULL;
+	}
+	return rc;
+}
+
+static int common_printf_inner(PPRINTFINSTANCE pThis, char *argv[])
 {
 	char *fmt, *start;
@@ -275,10 +301,4 @@
 	char longbuf[64];
 
-	/* kmk: reinitialize globals */
-	pThis->b_length = 0;
-	pThis->b_fmt = NULL;
-	pThis->rval = 0;
-	pThis->gargv = NULL;
-	pThis->g_cchBuf = 0;
 	format = *argv;
 	pThis->gargv = ++argv;
@@ -338,5 +358,5 @@
 
 			case 'B': {
-				const char *p = conv_expand(getstr(pThis));
+				const char *p = conv_expand(pThis, getstr(pThis));
 				*fmt = 's';
 				PF(start, p);
@@ -347,25 +367,26 @@
 				 * but the string we generate might have
 				 * embedded nulls. */
-				static char *a, *t;
 				char *cp = getstr(pThis);
 				/* Free on entry in case shell longjumped out */
-				if (a != NULL)
-					free(a);
-				a = NULL;
-				if (t != NULL)
-					free(t);
-				t = NULL;
+				if (pThis->a != NULL) {
+					free(pThis->a);
+					pThis->a = NULL;
+				}
+				if (pThis->t != NULL) {
+					free(pThis->t);
+					pThis->t = NULL;
+				}
 				/* Count number of bytes we want to output */
 				pThis->b_length = 0;
 				conv_escape_str(pThis, cp, b_count);
-				t = malloc(pThis->b_length + 1);
-				if (t == NULL)
+				pThis->t = malloc(pThis->b_length + 1);
+				if (pThis->t == NULL)
 					break;
-				memset(t, 'x', pThis->b_length);
-				t[pThis->b_length] = 0;
+				memset(pThis->t, 'x', pThis->b_length);
+				pThis->t[pThis->b_length] = 0;
 				/* Get printf to calculate the lengths */
 				*fmt = 's';
-				APF(&a, start, t);
-				pThis->b_fmt = a;
+				APF(&pThis->a, start, pThis->t);
+				pThis->b_fmt = pThis->a;
 				/* Output leading spaces and data bytes */
 				conv_escape_str(pThis, cp, b_output);
@@ -710,19 +731,17 @@
 /* expand a string so that everything is printable */
 
-static char *
-conv_expand(const char *str)
-{
-	static char *conv_str;
-	static char no_memory[] = "<no memory>";
+static const char *
+conv_expand(PPRINTFINSTANCE pThis, const char *str)
+{
+	static const char no_memory[] = "<no memory>";
 	char *cp;
 	int ch;
 
-	if (conv_str)
-		free(conv_str);
+	if (pThis->conv_str)
+		free(pThis->conv_str);
 	/* get a buffer that is definitely large enough.... */
-	conv_str = malloc(4 * strlen(str) + 1);
-	if (!conv_str)
+	pThis->conv_str = cp = malloc(4 * strlen(str) + 1);
+	if (!cp)
 		return no_memory;
-	cp = conv_str;
 
 	while ((ch = *(const unsigned char *)str++) != '\0') {
@@ -771,5 +790,5 @@
 
 	*cp = 0;
-	return conv_str;
+	return pThis->conv_str;
 }
 
