From 8eeb81b97870153a57c9dff79540c70d89ef87a4 Mon Sep 17 00:00:00 2001 From: Ken Hornstein Date: Tue, 13 Mar 2012 15:25:41 -0400 Subject: [PATCH] Modify getfullname so it performs the same processing that mts.c:getuserinfo() does. --- test/getfullname.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/test/getfullname.c b/test/getfullname.c index f0c8a2d..3dc221e 100644 --- a/test/getfullname.c +++ b/test/getfullname.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -16,12 +17,13 @@ int main(int argc, char *argv[]) { + struct passwd *pwd; + char name[BUFSIZ], *p; + if (argc > 1) { fprintf (stderr, "usage: %s\n", argv[0]); } - struct passwd *pwd; - pwd = getpwuid(getuid()); if (! pwd) { @@ -30,7 +32,36 @@ main(int argc, char *argv[]) exit(1); } - printf("%s\n", pwd->pw_gecos); + /* + * Perform the same processing that getuserinfo() does. + */ + + strncpy(name, pwd->pw_gecos, sizeof(name)); + + name[sizeof(name) - 1] = '\0'; + + /* + * Stop at the first comma + */ + + if ((p = strchr(name, ','))) + *p = '\0'; + + /* + * Quote the entire string if it has a "." in it + */ + + if (strchr(name, '.')) { + char tmp[BUFSIZ]; + + snprintf(tmp, sizeof(tmp), "\"%s\"", name); + strncpy(name, tmp, sizeof(name)); + + name[sizeof(name) - 2] = '"'; + name[sizeof(name) - 1] = '\0'; + } + + printf("%s\n", name); exit(0); } -- 1.7.10.4