From 6fd1b08628a8f331e1a344f8b535f30994ac4e7a Mon Sep 17 00:00:00 2001 From: Herbert Xu Date: Sat, 29 Oct 2005 16:01:07 +1000 Subject: [EXPAND] Added getpwhome as a wrapper for getpwnam klibc doesn't have and doesn't need getpwnam. This change creates getpwhome which always returns NULL if getpwnam doesn't exist. --- ChangeLog | 1 + configure.ac | 3 ++- src/expand.c | 23 ++++++++++++++++------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 59f81ff..72c9bfd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,7 @@ * Stop using sysexits.h in commandcmd. * Use stat if stat64 does not exist. * Added default implementation of bsearch. + * Added getpwhome as a wrapper for getpwnam. 2005-10-26 Herbert Xu diff --git a/configure.ac b/configure.ac index bb0e815..bdce1b6 100644 --- a/configure.ac +++ b/configure.ac @@ -23,7 +23,8 @@ dnl Checks for libraries. dnl Checks for header files. dnl Checks for library functions. -AC_CHECK_FUNCS(bsearch mempcpy sigsetmask stpcpy strchrnul strtoimax strtoumax) +AC_CHECK_FUNCS(bsearch mempcpy getpwnam sigsetmask stpcpy strchrnul strtoimax \ + strtoumax) dnl Check for klibc signal. AC_CHECK_FUNC(signal) diff --git a/src/expand.c b/src/expand.c index d6efdd8..7fdc492 100644 --- a/src/expand.c +++ b/src/expand.c @@ -37,7 +37,9 @@ #include #include #include +#ifdef HAVE_GETPWNAM #include +#endif #include #include #include @@ -175,6 +177,17 @@ esclen(const char *start, const char *p) { } +static inline const char *getpwhome(const char *name) +{ +#ifdef HAVE_GETPWNAM + struct passwd *pw = getpwnam(name); + return pw ? pw->pw_dir : 0; +#else + return 0; +#endif +} + + /* * Expand shell variables and backquotes inside a here document. */ @@ -382,7 +395,6 @@ exptilde(char *startp, char *p, int flag) { char c; char *name; - struct passwd *pw; const char *home; int quotes = flag & QUOTES_ESC; int startloc; @@ -407,14 +419,11 @@ exptilde(char *startp, char *p, int flag) done: *p = '\0'; if (*name == '\0') { - if ((home = lookupvar(homestr)) == NULL) - goto lose; + home = lookupvar(homestr); } else { - if ((pw = getpwnam(name)) == NULL) - goto lose; - home = pw->pw_dir; + home = getpwhome(name); } - if (*home == '\0') + if (!home || !*home) goto lose; *p = c; startloc = expdest - (char *)stackblock(); -- cgit 1.4.1