Skip to content

Commit

Permalink
win32: revert special treatment of Unix-style absolute paths
Browse files Browse the repository at this point in the history
Commit 6059723 (win32: handle Unix-style absolute paths for
executables) added special treatment of paths for executables
starting with a slash.  Such paths are absolute on Unix but are
relative to the current drive on Windows.  On reflection this
commit did more than necessary.  Later commits provided special
treatment only for paths starting with locations traditionally
used to contain binaries on Unix.  This is probably sufficient.

Problems introduced by commit 6059723 include:

- If the current drive isn't the system drive tab completion of a
  command starting with a slash confusingly references the system
  drive.

- Building busybox-w32 with w64devkit fails on drives other than
  the system drive.

Revert the changes introduced by commit 6059723.

This saves 192 bytes.

(GitHub issue rmyorston#239)
  • Loading branch information
rmyorston committed Oct 26, 2022
1 parent 0b5bd6e commit d71cb67
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 49 deletions.
6 changes: 3 additions & 3 deletions debianutils/which.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
//config: which is used to find programs in your PATH and
//config: print out their pathnames.

// NOTE: For WIN32 this applet is NOEXEC as alloc_system_drive() and
// NOTE: For WIN32 this applet is NOEXEC as alloc_ext_space() and
// find_executable() both allocate memory. And find_executable()
// calls alloc_system_drive().
// calls alloc_ext_space().

//applet:IF_PLATFORM_MINGW32(IF_WHICH(APPLET_NOEXEC(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which)))
//applet:IF_PLATFORM_POSIX(IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which)))
Expand Down Expand Up @@ -87,7 +87,7 @@ int which_main(int argc UNUSED_PARAM, char **argv)
}
#else
if (has_path(*argv)) {
char *path = alloc_system_drive(*argv);
char *path = alloc_ext_space(*argv);

if (add_win32_extension(path) || file_is_executable(path)) {
missing = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/mingw.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ int unc_root_len(const char *dir);
int root_len(const char *path);
const char *get_system_drive(void);
const char *need_system_drive(const char *path);
char *alloc_system_drive(const char *path);
char *alloc_ext_space(const char *path);
int chdir_system_drive(void);
char *xabsolute_path(char *path);
char *get_drive_cwd(const char *path, char *buffer, int size);
Expand Down
2 changes: 1 addition & 1 deletion libbb/executable.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ char* FAST_FUNC find_executable(const char *filename, char **PATHp)
);
#if ENABLE_PLATFORM_MINGW32
{
char *w = alloc_system_drive(p);
char *w = alloc_ext_space(p);
ex = add_win32_extension(w) || file_is_executable(w);
free(p);
p = w;
Expand Down
3 changes: 0 additions & 3 deletions libbb/lineedit.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,6 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
}

lpath = *paths[i] ? paths[i] : ".";
#if ENABLE_PLATFORM_MINGW32
lpath = auto_string(alloc_system_drive(lpath));
#endif
dir = opendir(lpath);
if (!dir)
continue; /* don't print an error */
Expand Down
45 changes: 12 additions & 33 deletions shell/ash.c
Original file line number Diff line number Diff line change
Expand Up @@ -2158,17 +2158,13 @@ maybe_single_quote(const char *s)
}

#if ENABLE_PLATFORM_MINGW32
/*
* Place 'path' in a string on the stack, adding the system drive prefix
* if necessary and leaving room for an optional extension.
*/
/* Copy path to a string on the stack long enough to allow a file extension
* to be added. */
static char *
stack_add_system_drive(const char *path)
stack_add_ext_space(const char *path)
{
const char *sd = need_system_drive(path);
char *p = growstackto(strlen(path) + 5 + (sd ? strlen(sd) : 0));

strcpy(stpcpy(p, sd ?: ""), path);
char *p = growstackto(strlen(path) + 5);
strcpy(p, path);
return p;
}
#endif
Expand Down Expand Up @@ -2947,10 +2943,6 @@ padvance_magic(const char **path, const char *name, int magic)
const char *start;
size_t qlen;
size_t len;
#if ENABLE_PLATFORM_MINGW32
size_t sdlen = 0;
const char *sd;
#endif

if (*path == NULL)
return -1;
Expand Down Expand Up @@ -2982,20 +2974,11 @@ padvance_magic(const char **path, const char *name, int magic)
*path = *p == PATH_SEP ? p + 1 : NULL;

/* "2" is for '/' and '\0' */
qlen = len + strlen(name) + 2;
#if ENABLE_PLATFORM_MINGW32
/* reserve space for system drive prefix and extension */
sd = need_system_drive(start);
if (sd != NULL)
sdlen = strlen(sd);
qlen += 4 + sdlen;
#endif
/* reserve space for suffix on WIN32 */
qlen = len + strlen(name) + 2 IF_PLATFORM_MINGW32(+ 4);
q = growstackto(qlen);

if (len) {
#if ENABLE_PLATFORM_MINGW32
q = mempcpy(q, sd, sdlen);
#endif
q = mempcpy(q, start, len);
#if ENABLE_PLATFORM_MINGW32
if (q[-1] != '/' && q[-1] != '\\')
Expand Down Expand Up @@ -9058,10 +9041,6 @@ static void shellexec(char *prog, char **argv, const char *path, int idx)
|| (applet_no = find_applet_by_name(prog)) >= 0
#endif
) {
#if ENABLE_PLATFORM_MINGW32
char *oldprog = prog;
prog = stack_add_system_drive(prog);
#endif
tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp);
if (applet_no >= 0) {
/* We tried execing ourself, but it didn't work.
Expand All @@ -9072,15 +9051,15 @@ static void shellexec(char *prog, char **argv, const char *path, int idx)
}
e = errno;
#if ENABLE_PLATFORM_MINGW32
if (unix_path(oldprog) && !find_builtin(bb_basename(oldprog))) {
if (unix_path(prog) && !find_builtin(bb_basename(prog))) {
# if ENABLE_FEATURE_SH_STANDALONE
const char *name = bb_basename(oldprog);
const char *name = bb_basename(prog);
if ((applet_no = find_applet_by_name(name)) >= 0) {
tryexec(applet_no, name, argv, envp);
e = errno;
}
# endif
argv[0] = (char *)bb_basename(oldprog);
argv[0] = (char *)bb_basename(prog);
goto try_PATH;
}
#endif
Expand Down Expand Up @@ -9523,7 +9502,7 @@ describe_command(char *command, const char *path, int describe_command_verbose)
#endif
if (j < 0) {
#if ENABLE_PLATFORM_MINGW32
p = stack_add_system_drive(command);
p = stack_add_ext_space(command);
#else
p = command;
#endif
Expand Down Expand Up @@ -14720,7 +14699,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
if (has_path(name)) {
entry->u.index = -1;
entry->cmdtype = CMDNORMAL;
fullname = stack_add_system_drive(name);
fullname = stack_add_ext_space(name);
if (add_win32_extension(fullname) || file_is_executable(fullname)) {
return;
} else if (unix_path(name) && !find_builtin(bb_basename(name))) {
Expand Down
11 changes: 5 additions & 6 deletions win32/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -2142,13 +2142,12 @@ const char *need_system_drive(const char *path)
return NULL;
}

/* Allocate a string long enough to allow a system drive prefix and
* file extension to be added to path. Add the prefix if necessary. */
char *alloc_system_drive(const char *path)
/* Copy path to an allocated string long enough to allow a file extension
* to be added. */
char *alloc_ext_space(const char *path)
{
const char *sd = need_system_drive(path);
char *s = xmalloc(strlen(path) + 5 + (sd ? strlen(sd) : 0));
strcpy(stpcpy(s, sd ?: ""), path);
char *s = xmalloc(strlen(path) + 5);
strcpy(s, path);
return s;
}

Expand Down
4 changes: 2 additions & 2 deletions win32/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv,
}
#endif

path = alloc_system_drive(interp.path);
path = alloc_ext_space(interp.path);
if ((add_win32_extension(path) || file_is_executable(path))) {
new_argv[0] = path;
ret = mingw_spawn_interpreter(mode, path, new_argv, envp, level);
Expand Down Expand Up @@ -363,7 +363,7 @@ mingw_spawnvp(int mode, const char *cmd, char *const *argv)
return mingw_spawn_applet(mode, argv, NULL);
#endif
if (has_path(cmd)) {
path = alloc_system_drive(cmd);
path = alloc_ext_space(cmd);
if (add_win32_extension(path) || file_is_executable(path)) {
ret = mingw_spawn_interpreter(mode, path, argv, NULL, 0);
free(path);
Expand Down

0 comments on commit d71cb67

Please sign in to comment.