Index: /trunk/src/kmk/kmkbuiltin/rm.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/rm.c	(revision 3215)
+++ /trunk/src/kmk/kmkbuiltin/rm.c	(revision 3216)
@@ -46,4 +46,5 @@
 *   Header Files                                                                                                                 *
 *********************************************************************************************************************************/
+#define FAKES_NO_GETOPT_H /* bird */
 #include "config.h"
 #include <sys/stat.h>
@@ -69,5 +70,5 @@
 #include <unistd.h>
 #include <ctype.h>
-#include "getopt.h"
+#include "getopt_r.h"
 #ifdef __HAIKU__
 # include "haikufakes.h"
@@ -187,4 +188,5 @@
 {
 	RMINSTANCE This;
+	struct getopt_state_r gos;
 	int ch, rflag;
 
@@ -205,12 +207,7 @@
 	kBuildProtectionInit(&This.g_ProtData, pCtx);
 
-	/* kmk: reset getopt and set program name. */
-	opterr = 1;
-	optarg = NULL;
-	optopt = 0;
-	optind = 0; /* init */
-
 	rflag = 0;
-	while ((ch = getopt_long(argc, argv, "dfiPRvW", long_options, NULL)) != -1)
+	getopt_initialize_r(&gos, argc, argv, "dfiPRvW", long_options, envp, pCtx);
+	while ((ch = getopt_long_r(&gos, NULL)) != -1)
 		switch (ch) {
 		case 'd':
@@ -262,5 +259,5 @@
 			break;
 		case 267:
-			if (kBuildProtectionSetDepth(&This.g_ProtData, optarg)) {
+			if (kBuildProtectionSetDepth(&This.g_ProtData, gos.optarg)) {
 			    kBuildProtectionTerm(&This.g_ProtData);
 			    return 1;
@@ -277,6 +274,6 @@
 			return usage(pCtx, 1);
 		}
-	argc -= optind;
-	argv += optind;
+	argc -= gos.optind;
+	argv += gos.optind;
 
 	if (argc < 1) {
@@ -347,4 +344,7 @@
 
 	flags = FTS_PHYSICAL;
+#ifndef KMK_BUILTIN_STANDALONE
+	flags |= FTS_NOCHDIR; /* Must not change the directory from inside kmk! */
+#endif
 	if (!needstat)
 		flags |= FTS_NOSTAT;
Index: /trunk/src/kmk/kmkbuiltin/rmdir.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/rmdir.c	(revision 3215)
+++ /trunk/src/kmk/kmkbuiltin/rmdir.c	(revision 3216)
@@ -48,4 +48,5 @@
 *   Header Files                                                                                                                 *
 *********************************************************************************************************************************/
+#define FAKES_NO_GETOPT_H /* bird */
 #include "config.h"
 #include "err.h"
@@ -58,5 +59,5 @@
 # include <alloca.h>
 #endif
-#include "getopt.h"
+#include "getopt_r.h"
 #include "kmkbuiltin.h"
 
@@ -110,4 +111,5 @@
 {
 	RMDIRINSTANCE This;
+	struct getopt_state_r gos;
 	int ch, errors;
 
@@ -119,10 +121,6 @@
 	This.pflag = 0;
 
-	/* kmk: reset getopt and set progname */
-	opterr = 1;
-	optarg = NULL;
-	optopt = 0;
-	optind = 0; /* init */
-	while ((ch = getopt_long(argc, argv, "pv", long_options, NULL)) != -1)
+	getopt_initialize_r(&gos, argc, argv, "pv", long_options, envp, pCtx);
+	while ((ch = getopt_long_r(&gos, NULL)) != -1)
 		switch(ch) {
 		case 'p':
@@ -147,6 +145,6 @@
 			return usage(pCtx, 1);
 		}
-	argc -= optind;
-	argv += optind;
+	argc -= gos.optind;
+	argv += gos.optind;
 
 	if (argc == 0)
