From 15e948bdabe8fc69d821a20dfe2bf00b56eb99f3 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 23 Apr 2012 15:05:26 +0100 Subject: win32: support fancy prompts and (limited) tilde expansion --- Makefile.flags | 1 + configs/mingw32_defconfig | 6 +++--- coreutils/whoami.c | 10 ---------- libbb/lineedit.c | 10 ++++++++++ win32/mingw.c | 30 +++++++++++++++++++++++++++++- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/Makefile.flags b/Makefile.flags index 0eb495c60..4cbef0597 100644 --- a/Makefile.flags +++ b/Makefile.flags @@ -108,6 +108,7 @@ ifeq ($(CONFIG_PLATFORM_MINGW32),y) # These defintions are not strictly needed, but they help shut up fnmatch.c warnings CFLAGS += -Iwin32 -DHAVE_STRING_H=1 -DHAVE_CONFIG_H=0 -fno-builtin-stpcpy EXEEXT = .exe +LDLIBS += userenv ifeq ($(CONFIG_WIN32_NET),y) LDLIBS += ws2_32 endif diff --git a/configs/mingw32_defconfig b/configs/mingw32_defconfig index d014db4ae..aeb07276f 100644 --- a/configs/mingw32_defconfig +++ b/configs/mingw32_defconfig @@ -1,7 +1,7 @@ # # Automatically generated make config: don't edit # Busybox version: 1.20.0 -# Mon Apr 23 11:02:02 2012 +# Mon Apr 23 14:39:03 2012 # CONFIG_HAVE_DOT_CONFIG=y # CONFIG_PLATFORM_POSIX is not set @@ -110,8 +110,8 @@ CONFIG_FEATURE_EDITING_HISTORY=255 # CONFIG_FEATURE_EDITING_SAVE_ON_EXIT is not set # CONFIG_FEATURE_REVERSE_SEARCH is not set CONFIG_FEATURE_TAB_COMPLETION=y -# CONFIG_FEATURE_USERNAME_COMPLETION is not set -# CONFIG_FEATURE_EDITING_FANCY_PROMPT is not set +CONFIG_FEATURE_USERNAME_COMPLETION=y +CONFIG_FEATURE_EDITING_FANCY_PROMPT=y # CONFIG_FEATURE_EDITING_ASK_TERMINAL is not set CONFIG_FEATURE_NON_POSIX_CP=y # CONFIG_FEATURE_VERBOSE_CP_MESSAGE is not set diff --git a/coreutils/whoami.c b/coreutils/whoami.c index 500e07166..30b17cab3 100644 --- a/coreutils/whoami.c +++ b/coreutils/whoami.c @@ -21,21 +21,11 @@ int whoami_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int whoami_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) { -#if ENABLE_PLATFORM_MINGW32 - char buf[64]; - DWORD len = 64; -#endif - if (argv[1]) bb_show_usage(); -#if ENABLE_PLATFORM_MINGW32 - GetUserName(buf, &len); - puts(buf); -#else /* Will complain and die if username not found */ puts(xuid2uname(geteuid())); -#endif return fflush_all(); } diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 77dc1c607..b3ae21510 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -647,6 +647,7 @@ static char *username_path_completion(char *ud) if (*ud == '/') { /* "~/..." */ home = home_pwd_buf; } else { +#if !ENABLE_PLATFORM_MINGW32 /* "~user/..." */ ud = strchr(ud, '/'); *ud = '\0'; /* "~user" */ @@ -654,6 +655,7 @@ static char *username_path_completion(char *ud) *ud = '/'; /* restore "~user/..." */ if (entry) home = entry->pw_dir; +#endif } if (home) { ud = concat_path_file(home, ud); @@ -668,15 +670,18 @@ static char *username_path_completion(char *ud) */ static NOINLINE unsigned complete_username(const char *ud) { +#if !ENABLE_PLATFORM_MINGW32 /* Using _r function to avoid pulling in static buffers */ char line_buff[256]; struct passwd pwd; struct passwd *result; +#endif unsigned userlen; ud++; /* skip ~ */ userlen = strlen(ud); +#if !ENABLE_PLATFORM_MINGW32 setpwent(); while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) { /* Null usernames should result in all users as possible completions. */ @@ -685,6 +690,7 @@ static NOINLINE unsigned complete_username(const char *ud) } } endpwent(); +#endif return 1 + userlen; } @@ -1824,7 +1830,11 @@ static void parse_and_put_prompt(const char *prmt_ptr) pbuf = cwd_buf; l = strlen(home_pwd_buf); if (l != 0 +#if !ENABLE_PLATFORM_MINGW32 && strncmp(home_pwd_buf, cwd_buf, l) == 0 +#else + && strncasecmp(home_pwd_buf, cwd_buf, l) == 0 +#endif && (cwd_buf[l]=='/' || cwd_buf[l]=='\0') && strlen(cwd_buf + l) < PATH_MAX ) { diff --git a/win32/mingw.c b/win32/mingw.c index b99c88444..657d106ed 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1,4 +1,5 @@ #include "libbb.h" +#include unsigned int _CRT_fmode = _O_BINARY; @@ -495,6 +496,33 @@ int mingw_rename(const char *pold, const char *pnew) return -1; } +static char *gethomedir(void) +{ + static char buf[PATH_MAX]; + DWORD len = sizeof(buf); + HANDLE h; + char *s; + + buf[0] = '\0'; + if ( !OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &h) ) + return buf; + + if ( !GetUserProfileDirectory(h, buf, &len) ) { + CloseHandle(h); + return buf; + } + + CloseHandle(h); + + for ( s=buf; *s; ++s ) { + if ( *s == '\\' ) { + *s = '/'; + } + } + + return buf; +} + struct passwd *getpwuid(int uid UNUSED_PARAM) { static char user_name[100]; @@ -505,7 +533,7 @@ struct passwd *getpwuid(int uid UNUSED_PARAM) return NULL; p.pw_name = user_name; p.pw_gecos = "unknown"; - p.pw_dir = NULL; + p.pw_dir = gethomedir(); return &p; } -- cgit v1.2.3-55-g6feb