]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
chsh: rewrite function interacting with user to get path to new shell
authorSami Kerola <kerolasa@iki.fi>
Fri, 19 Dec 2014 19:28:19 +0000 (19:28 +0000)
committerSami Kerola <kerolasa@iki.fi>
Mon, 5 Jan 2015 22:52:51 +0000 (22:52 +0000)
Rename prompt() to ask_new_shell().  Remove fixed size buffer and
allocate path to new shell, that should make Hurd people happy.  Use
strutils.h for white space trimming.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
login-utils/chsh.c

index 5339e5508fac93b72a204d98dc2dc9a4e524550f..bcd0995ba4250d88ef0c45c46017782d6c786c6e 100644 (file)
@@ -40,6 +40,7 @@
 #include "nls.h"
 #include "pathnames.h"
 #include "setpwnam.h"
+#include "strutils.h"
 #include "xalloc.h"
 
 #include "ch-common.h"
@@ -166,34 +167,28 @@ static void parse_argv(int argc, char **argv, struct sinfo *pinfo)
 }
 
 /*
- *  prompt () --
- *     ask the user for a given field and return it.
+ *  ask_new_shell () --
+ *     ask the user for a shell and return it.
  */
-static char *prompt(char *question, char *def_val)
+static char *ask_new_shell(char *question, char *oldshell)
 {
        int len;
-       char *ans, *cp;
-       char buf[BUFSIZ];
+       char *ans = NULL;
+       size_t dummy = 0;
+       ssize_t sz;
 
-       if (!def_val)
-               def_val = "";
-       printf("%s [%s]: ", question, def_val);
-       *buf = 0;
-       if (fgets(buf, sizeof(buf), stdin) == NULL)
-               errx(EXIT_FAILURE, _("Aborted."));
-       /* remove the newline at the end of buf. */
-       ans = buf;
-       while (isspace(*ans))
-               ans++;
-       len = strlen(ans);
-       while (len > 0 && isspace(ans[len - 1]))
-               len--;
-       if (len <= 0)
+       if (!oldshell)
+               oldshell = "";
+       printf("%s [%s]: ", question, oldshell);
+       sz = getline(&ans, &dummy, stdin);
+       if (sz == -1)
                return NULL;
-       ans[len] = 0;
-       cp = (char *)xmalloc(len + 1);
-       strcpy(cp, ans);
-       return cp;
+       /* remove the newline at the end of ans. */
+       ltrim_whitespace((unsigned char *) ans);
+       len = rtrim_whitespace((unsigned char *) ans);
+       if (len == 0)
+               return NULL;
+       return ans;
 }
 
 /*
@@ -329,7 +324,7 @@ int main(int argc, char **argv)
        }
 #endif
        if (!info.shell) {
-               info.shell = prompt(_("New shell"), oldshell);
+               info.shell = ask_new_shell(_("New shell"), oldshell);
                if (!info.shell)
                        return EXIT_SUCCESS;
        }