From 996f93243dda87de3140c497624312722f47ffa1 Mon Sep 17 00:00:00 2001
From: Ron Yorston <rmy@pobox.com>
Date: Fri, 15 Mar 2019 10:27:19 +0000
Subject: win32: add function to convert slashes to backslashes

There are now two places where slashes are converted to backslashes
throughout a string so it makes sense to create a function to do
this.

To avoid confusion rename convert_slashes() to bs_to_slash() and
call the new function slash_to_bs().
---
 debianutils/which.c    |  6 +++---
 include/mingw.h        |  3 ++-
 libbb/appletlib.c      |  2 +-
 libbb/lineedit.c       |  2 +-
 libbb/make_directory.c |  2 +-
 loginutils/suw32.c     |  9 +++------
 shell/ash.c            |  4 ++--
 win32/mingw.c          | 19 ++++++++++++++-----
 win32/process.c        |  7 ++-----
 9 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/debianutils/which.c b/debianutils/which.c
index b26f2f0f5..61f5770c5 100644
--- a/debianutils/which.c
+++ b/debianutils/which.c
@@ -69,7 +69,7 @@ int which_main(int argc UNUSED_PARAM, char **argv)
 #if ENABLE_PLATFORM_MINGW32
 			if ((p=auto_win32_extension(*argv)) != NULL) {
 				missing = 0;
-				convert_slashes(p);
+				bs_to_slash(p);
 				puts(p);
 			}
 			else
@@ -78,7 +78,7 @@ int which_main(int argc UNUSED_PARAM, char **argv)
 				missing = 0;
 #if ENABLE_PLATFORM_MINGW32
 				p = auto_string(xstrdup(*argv));
-				convert_slashes(p);
+				bs_to_slash(p);
 				puts(p);
 #else
 				puts(*argv);
@@ -92,7 +92,7 @@ int which_main(int argc UNUSED_PARAM, char **argv)
 			while ((p = find_executable(*argv, &path)) != NULL) {
 				missing = 0;
 #if ENABLE_PLATFORM_MINGW32
-				convert_slashes(p);
+				bs_to_slash(p);
 #endif
 				puts(p);
 				free(p);
diff --git a/include/mingw.h b/include/mingw.h
index 7aa2d5cc9..36c2f6805 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -510,7 +510,8 @@ static inline char *auto_win32_extension(const char *p)
 	return s ? auto_string(s) : NULL;
 }
 
-void convert_slashes(char *p) FAST_FUNC;
+void bs_to_slash(char *p) FAST_FUNC;
+void slash_to_bs(char *p) FAST_FUNC;
 size_t remove_cr(char *p, size_t len) FAST_FUNC;
 
 int err_win_to_posix(void);
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 49ae35874..35aabe758 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -1287,7 +1287,7 @@ int main(int argc UNUSED_PARAM, char **argv)
 		char *s;
 
 		str_tolower(argv[0]);
-		convert_slashes(argv[0]);
+		bs_to_slash(argv[0]);
 		if (has_exe_suffix_or_dot(argv[0]) && (s=strrchr(argv[0], '.'))) {
 			*s = '\0';
 		}
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index abe2857b2..3725dba0d 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -2611,7 +2611,7 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman
 #if ENABLE_PLATFORM_MINGW32
 		case CTRL('Z'):
 			command_ps[command_len] = '\0';
-			convert_slashes(command_ps);
+			bs_to_slash(command_ps);
 			redraw(cmdedit_y, 0);
 			break;
 #endif
diff --git a/libbb/make_directory.c b/libbb/make_directory.c
index ef0a8acd8..9af5552d5 100644
--- a/libbb/make_directory.c
+++ b/libbb/make_directory.c
@@ -51,7 +51,7 @@ int FAST_FUNC bb_make_directory(char *path, long mode, int flags)
 	org_mask = cur_mask = (mode_t)-1L;
 #if ENABLE_PLATFORM_MINGW32
 	/* normalise path separators, path is already assumed writable */
-	convert_slashes(path);
+	bs_to_slash(path);
 #endif
 	s = path;
 	while (1) {
diff --git a/loginutils/suw32.c b/loginutils/suw32.c
index e3c6d8744..de29f423a 100644
--- a/loginutils/suw32.c
+++ b/loginutils/suw32.c
@@ -28,18 +28,15 @@ int suw32_main(int argc UNUSED_PARAM, char **argv)
 {
 	char *opt_command = NULL;
 	SHELLEXECUTEINFO info;
-	char *bb_path, *cwd, *s;
+	char *bb_path, *cwd;
 
 	getopt32(argv, "c:", &opt_command);
 	if (argv[optind])
 		bb_show_usage();
 
 	/* ShellExecuteEx() needs backslash as separator in UNC paths. */
-	bb_path = s = xstrdup(bb_busybox_exec_path);
-	for ( ; *s; ++s) {
-		if (*s == '/')
-			*s = '\\';
-	}
+	bb_path = xstrdup(bb_busybox_exec_path);
+	slash_to_bs(bb_path);
 
 	memset(&info, 0, sizeof(SHELLEXECUTEINFO));
 	info.cbSize = sizeof(SHELLEXECUTEINFO);
diff --git a/shell/ash.c b/shell/ash.c
index 8daf263dc..4adad65aa 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -14696,7 +14696,7 @@ init(void)
 				 * cause problems */
 				if ( !winxp && strncmp(*envp, "SYSTEMROOT=", 11) != 0 &&
 						strncmp(*envp, "COMSPEC=", 8) != 0 ) {
-					convert_slashes(end+1);
+					bs_to_slash(end+1);
 				}
 
 				/* check for invalid characters in name */
@@ -14838,7 +14838,7 @@ procargs(char **argv)
 	} else if (!sflag) {
 		setinputfile(*xargv, 0);
 #if ENABLE_PLATFORM_MINGW32
-		convert_slashes(*xargv);
+		bs_to_slash(*xargv);
 #endif
  setarg0:
 		arg0 = *xargv++;
diff --git a/win32/mingw.c b/win32/mingw.c
index 3788e8a06..7552d19da 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -778,7 +778,7 @@ char *mingw_getcwd(char *pointer, int len)
 	char *ret = getcwd(pointer, len);
 	if (!ret)
 		return ret;
-	convert_slashes(ret);
+	bs_to_slash(ret);
 	return ret;
 }
 
@@ -833,7 +833,7 @@ static char *gethomedir(void)
 
 	GetUserProfileDirectory(h, buf, &len);
 	CloseHandle(h);
-	convert_slashes(buf);
+	bs_to_slash(buf);
 
 	return buf;
 }
@@ -1097,7 +1097,7 @@ char *realpath(const char *path, char *resolved_path)
 	if (_fullpath(buffer, path, MAX_PATH) &&
 			(real_path=resolve_symlinks(buffer))) {
 		strcpy(resolved_path, real_path);
-		convert_slashes(resolved_path);
+		bs_to_slash(resolved_path);
 		p = last_char_is(resolved_path, '/');
 		if (p && p > resolved_path && p[-1] != ':')
 			*p = '\0';
@@ -1178,7 +1178,7 @@ const char *get_busybox_exec_path(void)
 
 	if (!*path) {
 		GetModuleFileName(NULL, path, PATH_MAX);
-		convert_slashes(path);
+		bs_to_slash(path);
 	}
 	return path;
 }
@@ -1483,7 +1483,7 @@ char *alloc_win32_extension(const char *p)
 	return NULL;
 }
 
-void FAST_FUNC convert_slashes(char *p)
+void FAST_FUNC bs_to_slash(char *p)
 {
 	for (; *p; ++p) {
 		if ( *p == '\\' ) {
@@ -1492,6 +1492,15 @@ void FAST_FUNC convert_slashes(char *p)
 	}
 }
 
+void FAST_FUNC slash_to_bs(char *p)
+{
+	for (; *p; ++p) {
+		if ( *p == '/' ) {
+			*p = '\\';
+		}
+	}
+}
+
 size_t FAST_FUNC remove_cr(char *p, size_t len)
 {
 	ssize_t i, j;
diff --git a/win32/process.c b/win32/process.c
index d0fcc1c80..99db6f79f 100644
--- a/win32/process.c
+++ b/win32/process.c
@@ -190,7 +190,7 @@ static intptr_t
 spawnveq(int mode, const char *path, char *const *argv, char *const *env)
 {
 	char **new_argv;
-	char *s, *new_path = NULL;
+	char *new_path = NULL;
 	int i, argc;
 	intptr_t ret;
 	struct stat st;
@@ -219,10 +219,7 @@ spawnveq(int mode, const char *path, char *const *argv, char *const *env)
 	 * argument is a relative path containing forward slashes.  Absolute
 	 * paths are fine but there's no harm in converting them too. */
 	if (has_bat_suffix(path)) {
-		for (s=new_argv[0]; *s; ++s) {
-			if (*s == '/')
-				*s = '\\';
-		}
+		slash_to_bs(new_argv[0]);
 
 		/* Another special case:  spawnve returns ENOEXEC when passed an
 		 * empty batch file.  Pretend it worked. */
-- 
cgit v1.2.3-55-g6feb