Index: /trunk/src/kmk/kmkbuiltin/cp.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/cp.c	(revision 3220)
+++ /trunk/src/kmk/kmkbuiltin/cp.c	(revision 3221)
@@ -60,4 +60,9 @@
  */
 
+
+/*********************************************************************************************************************************
+*   Header Files                                                                                                                 *
+*********************************************************************************************************************************/
+#define FAKES_NO_GETOPT_H /* bird */
 #include "config.h"
 #include <sys/types.h>
@@ -73,5 +78,5 @@
 #include <string.h>
 #include <unistd.h>
-#include "getopt.h"
+#include "getopt_r.h"
 #include "k/kDefs.h"
 #ifdef _MSC_VER
@@ -87,4 +92,7 @@
 
 
+/*********************************************************************************************************************************
+*   Defined Constants And Macros                                                                                                 *
+*********************************************************************************************************************************/
 #ifndef S_IFWHT
 #define S_IFWHT 0
@@ -116,19 +124,24 @@
 }
 
+/*********************************************************************************************************************************
+*   Structures and Typedefs                                                                                                      *
+*********************************************************************************************************************************/
+typedef struct CPINSTANCE
+{
+	CPUTILSINSTANCE Utils;
+	int Rflag, rflag;
+	int cp_ignore_non_existing, cp_changed_only;
+	KBUILDPROTECTION g_ProtData;
+} CPINSTANCE;
+
 /* have wrappers for globals in cp_extern! */
 
-static KBUILDPROTECTION g_ProtData;
-const char *cp_argv0;
-static char emptystring[] = "";
-
-PATH_T to = { to.p_path, emptystring, "" };
-
-int fflag, iflag, nflag, pflag, vflag;
-static int Rflag, rflag;
-volatile sig_atomic_t info;
-static int cp_ignore_non_existing, cp_changed_only;
 
 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
 
+
+/*********************************************************************************************************************************
+*   Global Variables                                                                                                             *
+*********************************************************************************************************************************/
 enum cp_arg {
     CP_OPT_HELP = 261,
@@ -142,4 +155,5 @@
     CP_OPT_PROTECTION_DEPTH
 };
+
 static struct option long_options[] =
 {
@@ -156,8 +170,21 @@
 };
 
-
-static int copy(PKMKBUILTINCTX pCtx, char *[], enum op, int);
+static char emptystring[] = "";
+
+#if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE)
+volatile sig_atomic_t g_cp_info;
+#endif
+
+
+/*********************************************************************************************************************************
+*   Internal Functions                                                                                                           *
+*********************************************************************************************************************************/
+static int copy(CPINSTANCE *pThis, char * const *, enum op, int);
+#ifdef FTSCALL
+static int FTSCALL mastercmp(const FTSENT * const *, const FTSENT * const *);
+#else
 static int mastercmp(const FTSENT **, const FTSENT **);
-#ifdef SIGINFO
+#endif
+#if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE)
 static void siginfo(int __unused);
 #endif
@@ -167,4 +194,6 @@
 kmk_builtin_cp(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
 {
+	CPINSTANCE This;
+	struct getopt_state_r gos;
 	struct stat to_stat, tmp_stat;
 	enum op type;
@@ -173,21 +202,21 @@
 
         /* init globals */
-        cp_argv0 = argv[0];
-        to.p_end = to.p_path;
-        to.target_end = emptystring;
-        memset(to.p_path, 0, sizeof(to.p_path));
-        fflag = iflag = nflag = pflag = vflag = Rflag = rflag = 0;
-        info = 0;
-	cp_ignore_non_existing = cp_changed_only = 0;
-	kBuildProtectionInit(&g_ProtData, pCtx);
-
-        /* reset getopt and set progname. */
-        opterr = 1;
-        optarg = NULL;
-        optopt = 0;
-        optind = 0; /* init */
+	This.Utils.pCtx = pCtx;
+        This.Utils.to.p_end = This.Utils.to.p_path;
+        This.Utils.to.target_end = emptystring;
+        memset(This.Utils.to.p_path, 0, sizeof(This.Utils.to.p_path));
+        This.Utils.fflag = 0;
+	This.Utils.iflag = 0;
+	This.Utils.nflag = 0;
+	This.Utils.pflag = 0;
+	This.Utils.vflag = 0;
+	This.Rflag = 0;
+	This.rflag = 0;
+	This.cp_ignore_non_existing = This.cp_changed_only = 0;
+	kBuildProtectionInit(&This.g_ProtData, pCtx);
 
 	Hflag = Lflag = Pflag = 0;
-	while ((ch = getopt_long(argc, argv, "HLPRfinprv", long_options, NULL)) != -1)
+	getopt_initialize_r(&gos, argc, argv, "HLPRfinprv", long_options, envp, pCtx);
+	while ((ch = getopt_long_r(&gos, NULL)) != -1)
 		switch (ch) {
 		case 'H':
@@ -204,74 +233,74 @@
 			break;
 		case 'R':
-			Rflag = 1;
+			This.Rflag = 1;
 			break;
 		case 'f':
-			fflag = 1;
-			iflag = nflag = 0;
+			This.Utils.fflag = 1;
+			This.Utils.iflag = This.Utils.nflag = 0;
 			break;
 		case 'i':
-			iflag = 1;
-			fflag = nflag = 0;
+			This.Utils.iflag = 1;
+			This.Utils.fflag = This.Utils.nflag = 0;
 			break;
 		case 'n':
-			nflag = 1;
-			fflag = iflag = 0;
+			This.Utils.nflag = 1;
+			This.Utils.fflag = This.Utils.iflag = 0;
 			break;
 		case 'p':
-			pflag = 1;
+			This.Utils.pflag = 1;
 			break;
 		case 'r':
-			rflag = 1;
+			This.rflag = 1;
 			break;
 		case 'v':
-			vflag = 1;
+			This.Utils.vflag = 1;
 			break;
 		case CP_OPT_HELP:
 			usage(pCtx, 0);
-			kBuildProtectionTerm(&g_ProtData);
+			kBuildProtectionTerm(&This.g_ProtData);
 			return 0;
 		case CP_OPT_VERSION:
-			kBuildProtectionTerm(&g_ProtData);
+			kBuildProtectionTerm(&This.g_ProtData);
 			return kbuild_version(argv[0]);
 		case CP_OPT_IGNORE_NON_EXISTING:
-			cp_ignore_non_existing = 1;
+			This.cp_ignore_non_existing = 1;
 			break;
 		case CP_OPT_CHANGED:
-			cp_changed_only = 1;
+			This.cp_changed_only = 1;
 			break;
 		case CP_OPT_DISABLE_PROTECTION:
-			kBuildProtectionDisable(&g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
+			kBuildProtectionDisable(&This.g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
 			break;
 		case CP_OPT_ENABLE_PROTECTION:
-			kBuildProtectionEnable(&g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
+			kBuildProtectionEnable(&This.g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
 			break;
 		case CP_OPT_ENABLE_FULL_PROTECTION:
-			kBuildProtectionEnable(&g_ProtData, KBUILDPROTECTIONTYPE_FULL);
+			kBuildProtectionEnable(&This.g_ProtData, KBUILDPROTECTIONTYPE_FULL);
 			break;
 		case CP_OPT_DISABLE_FULL_PROTECTION:
-			kBuildProtectionDisable(&g_ProtData, KBUILDPROTECTIONTYPE_FULL);
+			kBuildProtectionDisable(&This.g_ProtData, KBUILDPROTECTIONTYPE_FULL);
 			break;
 		case CP_OPT_PROTECTION_DEPTH:
-			if (kBuildProtectionSetDepth(&g_ProtData, optarg)) {
-				kBuildProtectionTerm(&g_ProtData);
+			if (kBuildProtectionSetDepth(&This.g_ProtData, gos.optarg)) {
+				kBuildProtectionTerm(&This.g_ProtData);
 				return 1;
 			}
 			break;
 		default:
-			kBuildProtectionTerm(&g_ProtData);
+			kBuildProtectionTerm(&This.g_ProtData);
 		        return usage(pCtx, 1);
 		}
-	argc -= optind;
-	argv += optind;
+	argc -= gos.optind;
+	argv += gos.optind;
 
 	if (argc < 2) {
-		kBuildProtectionTerm(&g_ProtData);
+		kBuildProtectionTerm(&This.g_ProtData);
 		return usage(pCtx, 1);
 	}
 
 	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
-	if (rflag) {
-		if (Rflag) {
-			kBuildProtectionTerm(&g_ProtData);
+	if (This.rflag) {
+		if (This.Rflag) {
+			kBuildProtectionTerm(&This.g_ProtData);
 			return errx(pCtx, 1,
 		    "the -R and -r options may not be specified together.");
@@ -283,5 +312,5 @@
 		fts_options |= FTS_LOGICAL;
         }
-	if (Rflag) {
+	if (This.Rflag) {
 		if (Hflag)
 			fts_options |= FTS_COMFOLLOW;
@@ -294,5 +323,5 @@
 		fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
 	}
-#ifdef SIGINFO
+#if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE)
 	(void)signal(SIGINFO, siginfo);
 #endif
@@ -300,17 +329,17 @@
 	/* Save the target base in "to". */
 	target = argv[--argc];
-	if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path)) {
-		kBuildProtectionTerm(&g_ProtData);
+	if (strlcpy(This.Utils.to.p_path, target, sizeof(This.Utils.to.p_path)) >= sizeof(This.Utils.to.p_path)) {
+		kBuildProtectionTerm(&This.g_ProtData);
 		return errx(pCtx, 1, "%s: name too long", target);
 	}
-	to.p_end = to.p_path + strlen(to.p_path);
-        if (to.p_path == to.p_end) {
-		*to.p_end++ = '.';
-		*to.p_end = 0;
+	This.Utils.to.p_end = This.Utils.to.p_path + strlen(This.Utils.to.p_path);
+        if (This.Utils.to.p_path == This.Utils.to.p_end) {
+		*This.Utils.to.p_end++ = '.';
+		*This.Utils.to.p_end = 0;
 	}
-	have_trailing_slash = IS_SLASH(to.p_end[-1]);
+	have_trailing_slash = IS_SLASH(This.Utils.to.p_end[-1]);
 	if (have_trailing_slash)
-		STRIP_TRAILING_SLASH(to);
-	to.target_end = to.p_end;
+		STRIP_TRAILING_SLASH(This.Utils.to);
+	This.Utils.to.target_end = This.Utils.to.p_end;
 
 	/* Set end of argument list for fts(3). */
@@ -331,8 +360,8 @@
 	 * In (2), the real target is not directory, but "directory/source".
 	 */
-	r = stat(to.p_path, &to_stat);
+	r = stat(This.Utils.to.p_path, &to_stat);
 	if (r == -1 && errno != ENOENT) {
-		kBuildProtectionTerm(&g_ProtData);
-		return err(pCtx, 1, "stat: %s", to.p_path);
+		kBuildProtectionTerm(&This.g_ProtData);
+		return err(pCtx, 1, "stat: %s", This.Utils.to.p_path);
 	}
 	if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
@@ -341,5 +370,5 @@
 		 */
 		if (argc > 1) {
-			kBuildProtectionTerm(&g_ProtData);
+			kBuildProtectionTerm(&This.g_ProtData);
 			return usage(pCtx, 1);
 		}
@@ -352,10 +381,10 @@
 		 */
 		if (r == -1) {
-			if (rflag || (Rflag && (Lflag || Hflag)))
+			if (This.rflag || (This.Rflag && (Lflag || Hflag)))
 				stat(*argv, &tmp_stat);
 			else
 				lstat(*argv, &tmp_stat);
 
-			if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
+			if (S_ISDIR(tmp_stat.st_mode) && (This.Rflag || This.rflag))
 				type = DIR_TO_DNE;
 			else
@@ -365,10 +394,10 @@
 
 		if (have_trailing_slash && type == FILE_TO_FILE) {
-			kBuildProtectionTerm(&g_ProtData);
+			kBuildProtectionTerm(&This.g_ProtData);
 			if (r == -1)
 				return errx(pCtx, 1, "directory %s does not exist",
-				            to.p_path);
+				            This.Utils.to.p_path);
 			else
-				return errx(pCtx, 1, "%s is not a directory", to.p_path);
+				return errx(pCtx, 1, "%s is not a directory", This.Utils.to.p_path);
 		}
 	} else
@@ -380,14 +409,14 @@
 	/* Finally, check that the "to" directory isn't protected. */
 	rc = 1;
-	if (!kBuildProtectionScanEnv(&g_ProtData, envp, "KMK_CP_")
-	 && !kBuildProtectionEnforce(&g_ProtData,
-				     Rflag || rflag
+	if (!kBuildProtectionScanEnv(&This.g_ProtData, envp, "KMK_CP_")
+	 && !kBuildProtectionEnforce(&This.g_ProtData,
+				     This.Rflag || This.rflag
 				     ? KBUILDPROTECTIONTYPE_RECURSIVE
 				     : KBUILDPROTECTIONTYPE_FULL,
-				     to.p_path)) {
-	    rc = copy(pCtx, argv, type, fts_options);
+				     This.Utils.to.p_path)) {
+	    rc = copy(&This, argv, type, fts_options);
 	}
 
-	kBuildProtectionTerm(&g_ProtData);
+	kBuildProtectionTerm(&This.g_ProtData);
 	return rc;
 }
@@ -402,5 +431,5 @@
 
 static int
-copy(PKMKBUILTINCTX pCtx, char *argv[], enum op type, int fts_options)
+copy(CPINSTANCE *pThis, char * const *argv, enum op type, int fts_options)
 {
 	struct stat to_stat;
@@ -420,5 +449,5 @@
 
 	if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
-		return err(pCtx, 1, "fts_open");
+		return err(pThis->Utils.pCtx, 1, "fts_open");
 	for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
                 int copied = 0;
@@ -426,8 +455,8 @@
 		switch (curr->fts_info) {
 		case FTS_NS:
-			if (   cp_ignore_non_existing
+			if (   pThis->cp_ignore_non_existing
 			    && curr->fts_errno == ENOENT) {
-				if (vflag) {
-					warnx(pCtx, "fts: %s: %s", curr->fts_path,
+				if (pThis->Utils.vflag) {
+					warnx(pThis->Utils.pCtx, "fts: %s: %s", curr->fts_path,
 					      strerror(curr->fts_errno));
 				}
@@ -437,10 +466,10 @@
 		case FTS_DNR:
 		case FTS_ERR:
-			warnx(pCtx, "fts: %s: %s",
+			warnx(pThis->Utils.pCtx, "fts: %s: %s",
 			    curr->fts_path, strerror(curr->fts_errno));
 			badcp = rval = 1;
 			continue;
 		case FTS_DC:			/* Warn, continue. */
-			warnx(pCtx, "%s: directory causes a cycle", curr->fts_path);
+			warnx(pThis->Utils.pCtx, "%s: directory causes a cycle", curr->fts_path);
 			badcp = rval = 1;
 			continue;
@@ -492,18 +521,18 @@
 			p = &curr->fts_path[base];
 			nlen = curr->fts_pathlen - base;
-			target_mid = to.target_end;
+			target_mid = pThis->Utils.to.target_end;
 			if (!IS_SLASH(*p) && !IS_SLASH(target_mid[-1]))
 				*target_mid++ = '/';
 			*target_mid = 0;
-			if (target_mid - to.p_path + nlen >= PATH_MAX) {
-				warnx(pCtx, "%s%s: name too long (not copied)",
-				    to.p_path, p);
+			if (target_mid - pThis->Utils.to.p_path + nlen >= PATH_MAX) {
+				warnx(pThis->Utils.pCtx, "%s%s: name too long (not copied)",
+				    pThis->Utils.to.p_path, p);
 				badcp = rval = 1;
 				continue;
 			}
 			(void)strncat(target_mid, p, nlen);
-			to.p_end = target_mid + nlen;
-			*to.p_end = 0;
-			STRIP_TRAILING_SLASH(to);
+			pThis->Utils.to.p_end = target_mid + nlen;
+			*pThis->Utils.to.p_end = 0;
+			STRIP_TRAILING_SLASH(pThis->Utils.to);
 		}
 
@@ -525,6 +554,6 @@
 			 * normally want to preserve them on directories.
 			 */
-			if (pflag) {
-				if (setfile(pCtx, curr->fts_statp, -1))
+			if (pThis->Utils.pflag) {
+				if (copy_file_attribs(&pThis->Utils, curr->fts_statp, -1))
 				    rval = 1;
 			} else {
@@ -532,6 +561,6 @@
 				if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) ||
 				    ((mode | S_IRWXU) & mask) != (mode & mask))
-					if (chmod(to.p_path, mode & mask) != 0){
-						warn(pCtx, "chmod: %s", to.p_path);
+					if (chmod(pThis->Utils.to.p_path, mode & mask) != 0){
+						warn(pThis->Utils.pCtx, "chmod: %s", pThis->Utils.to.p_path);
 						rval = 1;
 					}
@@ -541,5 +570,5 @@
 
 		/* Not an error but need to remember it happened */
-		if (stat(to.p_path, &to_stat) == -1)
+		if (stat(pThis->Utils.to.p_path, &to_stat) == -1)
 			dne = 1;
 		else {
@@ -548,6 +577,6 @@
 			    to_stat.st_ino == curr->fts_statp->st_ino &&
 			    to_stat.st_ino != 0) {
-				warnx(pCtx, "%s and %s are identical (not copied).",
-				    to.p_path, curr->fts_path);
+				warnx(pThis->Utils.pCtx, "%s and %s are identical (not copied).",
+				    pThis->Utils.to.p_path, curr->fts_path);
 				badcp = rval = 1;
 				if (S_ISDIR(curr->fts_statp->st_mode))
@@ -557,7 +586,7 @@
 			if (!S_ISDIR(curr->fts_statp->st_mode) &&
 			    S_ISDIR(to_stat.st_mode)) {
-				warnx(pCtx, "cannot overwrite directory %s with "
+				warnx(pThis->Utils.pCtx, "cannot overwrite directory %s with "
 				    "non-directory %s",
-				    to.p_path, curr->fts_path);
+				    pThis->Utils.to.p_path, curr->fts_path);
 				badcp = rval = 1;
 				continue;
@@ -573,8 +602,8 @@
 			    ((fts_options & FTS_COMFOLLOW) &&
 			    curr->fts_level == 0)) {
-				if (copy_file(pCtx, curr, dne, cp_changed_only, &copied))
+				if (copy_file(&pThis->Utils, curr, dne, pThis->cp_changed_only, &copied))
 					badcp = rval = 1;
 			} else {
-				if (copy_link(pCtx, curr, !dne))
+				if (copy_link(&pThis->Utils, curr, !dne))
 					badcp = rval = 1;
 			}
@@ -582,6 +611,6 @@
 #endif
 		case S_IFDIR:
-			if (!Rflag && !rflag) {
-				warnx(pCtx, "%s is a directory (not copied).",
+			if (!pThis->Rflag && !pThis->rflag) {
+				warnx(pThis->Utils.pCtx, "%s is a directory (not copied).",
 				    curr->fts_path);
 				(void)fts_set(ftsp, curr, FTS_SKIP);
@@ -598,10 +627,10 @@
 			 */
 			if (dne) {
-				if (mkdir(to.p_path,
+				if (mkdir(pThis->Utils.to.p_path,
 				    curr->fts_statp->st_mode | S_IRWXU) < 0)
-					return err(pCtx, 1, "mkdir: %s", to.p_path);
+					return err(pThis->Utils.pCtx, 1, "mkdir: %s", pThis->Utils.to.p_path);
 			} else if (!S_ISDIR(to_stat.st_mode)) {
 				errno = ENOTDIR;
-				return err(pCtx, 1, "to-mode: %s", to.p_path);
+				return err(pThis->Utils.pCtx, 1, "to-mode: %s", pThis->Utils.to.p_path);
 			}
 			/*
@@ -610,5 +639,5 @@
 			 * directory, or if the -p flag is in effect.
 			 */
-			curr->fts_number = pflag || dne;
+			curr->fts_number = pThis->Utils.pflag || dne;
 			break;
 #ifdef S_IFBLK
@@ -616,9 +645,9 @@
 #endif
 		case S_IFCHR:
-			if (Rflag) {
-				if (copy_special(pCtx, curr->fts_statp, !dne))
+			if (pThis->Rflag) {
+				if (copy_special(&pThis->Utils, curr->fts_statp, !dne))
 					badcp = rval = 1;
 			} else {
-				if (copy_file(pCtx, curr, dne, cp_changed_only, &copied))
+				if (copy_file(&pThis->Utils, curr, dne, pThis->cp_changed_only, &copied))
 					badcp = rval = 1;
 			}
@@ -627,23 +656,23 @@
 		case S_IFIFO:
 #endif
-			if (Rflag) {
-				if (copy_fifo(pCtx, curr->fts_statp, !dne))
+			if (pThis->Rflag) {
+				if (copy_fifo(&pThis->Utils, curr->fts_statp, !dne))
 					badcp = rval = 1;
 			} else {
-				if (copy_file(pCtx, curr, dne, cp_changed_only, &copied))
+				if (copy_file(&pThis->Utils, curr, dne, pThis->cp_changed_only, &copied))
 					badcp = rval = 1;
 			}
 			break;
 		default:
-			if (copy_file(pCtx, curr, dne, cp_changed_only, &copied))
+			if (copy_file(&pThis->Utils, curr, dne, pThis->cp_changed_only, &copied))
 				badcp = rval = 1;
 			break;
 		}
-		if (vflag && !badcp)
-			kmk_builtin_ctx_printf(pCtx, 0, copied ? "%s -> %s\n" : "%s matches %s - not copied\n",
-					       curr->fts_path, to.p_path);
+		if (pThis->Utils.vflag && !badcp)
+			kmk_builtin_ctx_printf(pThis->Utils.pCtx, 0, copied ? "%s -> %s\n" : "%s matches %s - not copied\n",
+					       curr->fts_path, pThis->Utils.to.p_path);
 	}
 	if (errno)
-		return err(pCtx, 1, "fts_read");
+		return err(pThis->Utils.pCtx, 1, "fts_read");
 	return (rval);
 }
@@ -657,6 +686,9 @@
  *	files first reduces seeking.
  */
-static int
-mastercmp(const FTSENT **a, const FTSENT **b)
+#ifdef FTSCALL
+static int FTSCALL mastercmp(const FTSENT * const *a, const FTSENT * const *b)
+#else
+static int mastercmp(const FTSENT **a, const FTSENT **b)
+#endif
 {
 	int a_info, b_info;
@@ -675,10 +707,10 @@
 }
 
-#ifdef SIGINFO
+#if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE)
 static void
 siginfo(int sig __unused)
 {
 
-	info = 1;
+	g_cp_info = 1;
 }
 #endif
Index: /trunk/src/kmk/kmkbuiltin/cp_extern.h
===================================================================
--- /trunk/src/kmk/kmkbuiltin/cp_extern.h	(revision 3220)
+++ /trunk/src/kmk/kmkbuiltin/cp_extern.h	(revision 3221)
@@ -39,23 +39,17 @@
 } PATH_T;
 
-#define argv0   cp_argv0
-#define to      cp_to
-#define fflag   cp_fflag
-#define iflag   cp_iflag
-#define nflag   cp_nflag
-#define pflag   cp_pflag
-#define vflag   cp_vflag
-#define info    cp_info
-#define usage   cp_usage
-#define setfile cp_setfile
+typedef struct CPUTILSINSTANCE {
+    PKMKBUILTINCTX pCtx;
+    /*extern*/ PATH_T to;
+    /*extern*/ int fflag, iflag, nflag, pflag, vflag;
+} CPUTILSINSTANCE;
 
-extern const char *argv0;
-extern PATH_T to;
-extern int fflag, iflag, nflag, pflag, vflag;
+#if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE)
 extern volatile sig_atomic_t info;
+#endif
 
-int	copy_fifo(PKMKBUILTINCTX pCtx, struct stat *, int);
-int	copy_file(PKMKBUILTINCTX pCtx, const FTSENT *, int, int, int *);
-int	copy_link(PKMKBUILTINCTX pCtx, const FTSENT *, int);
-int	copy_special(PKMKBUILTINCTX pCtx, struct stat *, int);
-int	setfile(PKMKBUILTINCTX pCtx, struct stat *, int);
+int	copy_fifo(CPUTILSINSTANCE *pThis, struct stat *, int);
+int	copy_file(CPUTILSINSTANCE *pThis, const FTSENT *, int, int, int *);
+int	copy_link(CPUTILSINSTANCE *pThis, const FTSENT *, int);
+int	copy_special(CPUTILSINSTANCE *pThis, struct stat *, int);
+int	copy_file_attribs(CPUTILSINSTANCE *pThis, struct stat *, int);
Index: /trunk/src/kmk/kmkbuiltin/cp_utils.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/cp_utils.c	(revision 3220)
+++ /trunk/src/kmk/kmkbuiltin/cp_utils.c	(revision 3221)
@@ -36,4 +36,7 @@
 #endif /* not lint */
 
+/*********************************************************************************************************************************
+*   Header Files                                                                                                                 *
+*********************************************************************************************************************************/
 #define MSC_DO_64_BIT_IO
 #include "config.h"
@@ -72,8 +75,12 @@
 #include "cmp_extern.h"
 
+
+/*********************************************************************************************************************************
+*   Defined Constants And Macros                                                                                                 *
+*********************************************************************************************************************************/
 #define	cp_pct(x,y)	(int)(100.0 * (double)(x) / (double)(y))
 
 #ifndef MAXBSIZE
-# define MAXBSIZE 0x20000
+# define MAXBSIZE 0x10000
 #endif
 #ifndef O_BINARY
@@ -87,7 +94,7 @@
 
 int
-copy_file(PKMKBUILTINCTX pCtx, const FTSENT *entp, int dne, int changed_only, int *pcopied)
-{
-	static char buf[MAXBSIZE];
+copy_file(CPUTILSINSTANCE *pThis, const FTSENT *entp, int dne, int changed_only, int *pcopied)
+{
+	/*static*/ char buf[MAXBSIZE];
 	struct stat *fs;
 	int ch, checkch, from_fd, rcount, rval, to_fd;
@@ -103,5 +110,5 @@
 
 	if ((from_fd = open(entp->fts_path, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) == -1) {
-		warn(pCtx, "open: %s", entp->fts_path);
+		warn(pThis->pCtx, "open: %s", entp->fts_path);
 		return (1);
 	}
@@ -120,5 +127,5 @@
 		/* compare the files first if requested */
 		if (changed_only) {
-                        if (cmp_fd_and_file(pCtx, from_fd, entp->fts_path, to.p_path,
+                        if (cmp_fd_and_file(pThis->pCtx, from_fd, entp->fts_path, pThis->to.p_path,
 					    1 /* silent */, 0 /* lflag */,
 					    0 /* special */) == OK_EXIT) {
@@ -129,5 +136,5 @@
     				close(from_fd);
 				if ((from_fd = open(entp->fts_path, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) == -1) {
-					warn(pCtx, "open: %s", entp->fts_path);
+					warn(pThis->pCtx, "open: %s", entp->fts_path);
 					return (1);
 				}
@@ -136,11 +143,11 @@
 
 #define YESNO "(y/n [n]) "
-		if (nflag) {
-			if (vflag)
-				kmk_builtin_ctx_printf(pCtx, 0, "%s not overwritten\n", to.p_path);
+		if (pThis->nflag) {
+			if (pThis->vflag)
+				kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s not overwritten\n", pThis->to.p_path);
 			return (0);
-		} else if (iflag) {
+		} else if (pThis->iflag) {
 			(void)fprintf(stderr, "overwrite %s? %s",
-					to.p_path, YESNO);
+					pThis->to.p_path, YESNO);
 			checkch = ch = getchar();
 			while (ch != '\n' && ch != EOF)
@@ -148,24 +155,24 @@
 			if (checkch != 'y' && checkch != 'Y') {
 				(void)close(from_fd);
-				kmk_builtin_ctx_printf(pCtx, 1, "not overwritten\n");
+				kmk_builtin_ctx_printf(pThis->pCtx, 1, "not overwritten\n");
 				return (1);
 			}
 		}
 
-		if (fflag) {
+		if (pThis->fflag) {
 		    /* remove existing destination file name,
 		     * create a new file  */
-		    (void)unlink(to.p_path);
-		    to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,
+		    (void)unlink(pThis->to.p_path);
+		    to_fd = open(pThis->to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,
 				 fs->st_mode & ~(S_ISUID | S_ISGID));
 		} else
 		    /* overwrite existing destination file name */
-		    to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_BINARY | KMK_OPEN_NO_INHERIT, 0);
+		    to_fd = open(pThis->to.p_path, O_WRONLY | O_TRUNC | O_BINARY | KMK_OPEN_NO_INHERIT, 0);
 	} else
-		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,
+		to_fd = open(pThis->to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,
 		    fs->st_mode & ~(S_ISUID | S_ISGID));
 
 	if (to_fd == -1) {
-		warn(pCtx, "open: %s", to.p_path);
+		warn(pThis->pCtx, "open: %s", pThis->to.p_path);
 		(void)close(from_fd);
 		return (1);
@@ -185,5 +192,5 @@
 		if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
 		    MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
-			warn(pCtx, "mmap: %s", entp->fts_path);
+			warn(pThis->pCtx, "mmap: %s", entp->fts_path);
 			rval = 1;
 		} else {
@@ -193,22 +200,24 @@
 				wcount = write(to_fd, bufp, wresid);
 				wtotal += wcount;
-				if (info) {
-					info = 0;
-					kmk_builtin_ctx_printf(pCtx, 1,
+# if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE)
+				if (g_cp_info) {
+					g_cp_info = 0;
+					kmk_builtin_ctx_printf(pThis->pCtx, 1,
 						"%s -> %s %3d%%\n",
-						entp->fts_path, to.p_path,
+						entp->fts_path, pThis->to.p_path,
 						cp_pct(wtotal, fs->st_size));
 
 				}
+#endif
 				if (wcount >= (ssize_t)wresid || wcount <= 0)
 					break;
 			}
 			if (wcount != (ssize_t)wresid) {
-				warn(pCtx, "write[%zd != %zu]: %s", wcount, wresid, to.p_path);
+				warn(pThis->pCtx, "write[%zd != %zu]: %s", wcount, wresid, pThis->to.p_path);
 				rval = 1;
 			}
 			/* Some systems don't unmap on close(2). */
 			if (munmap(p, fs->st_size) < 0) {
-				warn(pCtx, "munmap: %s", entp->fts_path);
+				warn(pThis->pCtx, "munmap: %s", entp->fts_path);
 				rval = 1;
 			}
@@ -223,17 +232,19 @@
 				wcount = write(to_fd, bufp, wresid);
 				wtotal += wcount;
-				if (info) {
-					info = 0;
-					kmk_builtin_ctx_printf(pCtx, 1,
+#if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE)
+				if (g_cp_info) {
+					g_cp_info = 0;
+					kmk_builtin_ctx_printf(pThis->pCtx, 1,
 						"%s -> %s %3d%%\n",
-						entp->fts_path, to.p_path,
+						entp->fts_path, pThis->to.p_path,
 						cp_pct(wtotal, fs->st_size));
 
 				}
+#endif
 				if (wcount >= (ssize_t)wresid || wcount <= 0)
 					break;
 			}
 			if (wcount != (ssize_t)wresid) {
-				warn(pCtx, "write[%zd != %zu]: %s", wcount, wresid, to.p_path);
+				warn(pThis->pCtx, "write[%zd != %zu]: %s", wcount, wresid, pThis->to.p_path);
 				rval = 1;
 				break;
@@ -241,5 +252,5 @@
 		}
 		if (rcount < 0) {
-			warn(pCtx, "read: %s", entp->fts_path);
+			warn(pThis->pCtx, "read: %s", entp->fts_path);
 			rval = 1;
 		}
@@ -253,9 +264,9 @@
 	 */
 
-	if (pflag && setfile(pCtx, fs, to_fd))
+	if (pThis->pflag && copy_file_attribs(pThis, fs, to_fd))
 		rval = 1;
 	(void)close(from_fd);
 	if (close(to_fd)) {
-		warn(pCtx, "close: %s", to.p_path);
+		warn(pThis->pCtx, "close: %s", pThis->to.p_path);
 		rval = 1;
 	}
@@ -264,5 +275,5 @@
 
 int
-copy_link(PKMKBUILTINCTX pCtx, const FTSENT *p, int exists)
+copy_link(CPUTILSINSTANCE *pThis, const FTSENT *p, int exists)
 {
 	int len;
@@ -270,51 +281,51 @@
 
 	if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
-		warn(pCtx, "readlink: %s", p->fts_path);
+		warn(pThis->pCtx, "readlink: %s", p->fts_path);
 		return (1);
 	}
 	llink[len] = '\0';
-	if (exists && unlink(to.p_path)) {
-		warn(pCtx, "unlink: %s", to.p_path);
-		return (1);
-	}
-	if (symlink(llink, to.p_path)) {
-		warn(pCtx, "symlink: %s", llink);
-		return (1);
-	}
-	return (pflag ? setfile(pCtx, p->fts_statp, -1) : 0);
-}
-
-int
-copy_fifo(PKMKBUILTINCTX pCtx, struct stat *from_stat, int exists)
-{
-	if (exists && unlink(to.p_path)) {
-		warn(pCtx, "unlink: %s", to.p_path);
-		return (1);
-	}
-	if (mkfifo(to.p_path, from_stat->st_mode)) {
-		warn(pCtx, "mkfifo: %s", to.p_path);
-		return (1);
-	}
-	return (pflag ? setfile(pCtx, from_stat, -1) : 0);
-}
-
-int
-copy_special(PKMKBUILTINCTX pCtx, struct stat *from_stat, int exists)
-{
-	if (exists && unlink(to.p_path)) {
-		warn(pCtx, "unlink: %s", to.p_path);
-		return (1);
-	}
-	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
-		warn(pCtx, "mknod: %s", to.p_path);
-		return (1);
-	}
-	return (pflag ? setfile(pCtx, from_stat, -1) : 0);
-}
-
-int
-setfile(PKMKBUILTINCTX pCtx, struct stat *fs, int fd)
-{
-	static struct timeval tv[2];
+	if (exists && unlink(pThis->to.p_path)) {
+		warn(pThis->pCtx, "unlink: %s", pThis->to.p_path);
+		return (1);
+	}
+	if (symlink(llink, pThis->to.p_path)) {
+		warn(pThis->pCtx, "symlink: %s", llink);
+		return (1);
+	}
+	return (pThis->pflag ? copy_file_attribs(pThis, p->fts_statp, -1) : 0);
+}
+
+int
+copy_fifo(CPUTILSINSTANCE *pThis, struct stat *from_stat, int exists)
+{
+	if (exists && unlink(pThis->to.p_path)) {
+		warn(pThis->pCtx, "unlink: %s", pThis->to.p_path);
+		return (1);
+	}
+	if (mkfifo(pThis->to.p_path, from_stat->st_mode)) {
+		warn(pThis->pCtx, "mkfifo: %s", pThis->to.p_path);
+		return (1);
+	}
+	return (pThis->pflag ? copy_file_attribs(pThis, from_stat, -1) : 0);
+}
+
+int
+copy_special(CPUTILSINSTANCE *pThis, struct stat *from_stat, int exists)
+{
+	if (exists && unlink(pThis->to.p_path)) {
+		warn(pThis->pCtx, "unlink: %s", pThis->to.p_path);
+		return (1);
+	}
+	if (mknod(pThis->to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
+		warn(pThis->pCtx, "mknod: %s", pThis->to.p_path);
+		return (1);
+	}
+	return (pThis->pflag ? copy_file_attribs(pThis, from_stat, -1) : 0);
+}
+
+int
+copy_file_attribs(CPUTILSINSTANCE *pThis, struct stat *fs, int fd)
+{
+	/*static*/ struct timeval tv[2];
 	struct stat ts;
 	int rval, gotstat, islink, fdval;
@@ -334,10 +345,10 @@
         tv[0].tv_usec = tv[1].tv_usec = 0;
 #endif
-	if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
-		warn(pCtx, "%sutimes: %s", islink ? "l" : "", to.p_path);
+	if (islink ? lutimes(pThis->to.p_path, tv) : utimes(pThis->to.p_path, tv)) {
+		warn(pThis->pCtx, "%sutimes: %s", islink ? "l" : "", pThis->to.p_path);
 		rval = 1;
 	}
 	if (fdval ? fstat(fd, &ts) :
-	    (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
+	    (islink ? lstat(pThis->to.p_path, &ts) : stat(pThis->to.p_path, &ts)))
 		gotstat = 0;
 	else {
@@ -354,8 +365,8 @@
 	if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
 		if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
-		    (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
-		    chown(to.p_path, fs->st_uid, fs->st_gid))) {
+		    (islink ? lchown(pThis->to.p_path, fs->st_uid, fs->st_gid) :
+		    chown(pThis->to.p_path, fs->st_uid, fs->st_gid))) {
 			if (errno != EPERM) {
-				warn(pCtx, "chown: %s", to.p_path);
+				warn(pThis->pCtx, "chown: %s", pThis->to.p_path);
 				rval = 1;
 			}
@@ -365,7 +376,7 @@
 	if (!gotstat || fs->st_mode != ts.st_mode)
 		if (fdval ? fchmod(fd, fs->st_mode) :
-		    (islink ? lchmod(to.p_path, fs->st_mode) :
-		    chmod(to.p_path, fs->st_mode))) {
-			warn(pCtx, "chmod: %s", to.p_path);
+		    (islink ? lchmod(pThis->to.p_path, fs->st_mode) :
+		    chmod(pThis->to.p_path, fs->st_mode))) {
+			warn(pThis->pCtx, "chmod: %s", pThis->to.p_path);
 			rval = 1;
 		}
@@ -376,6 +387,6 @@
 		    fchflags(fd, fs->st_flags) :
 		    (islink ? (errno = ENOSYS) :
-		    chflags(to.p_path, fs->st_flags))) {
-			warn(pCtx, "chflags: %s", to.p_path);
+		    chflags(pThis->to.p_path, fs->st_flags))) {
+			warn(pThis->pCtx, "chflags: %s", pThis->to.p_path);
 			rval = 1;
 		}
