[PATCH 07/14] chsh: rewrite function interacting with user to get path to new shell

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxx>
---
 login-utils/chsh.c | 43 +++++++++++++++++++------------------------
 1 file changed, 19 insertions(+), 24 deletions(-)

diff --git a/login-utils/chsh.c b/login-utils/chsh.c
index 5339e55..bcd0995 100644
--- a/login-utils/chsh.c
+++ b/login-utils/chsh.c
@@ -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;
 	}
-- 
2.2.1

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux