X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/265bf7d31f31962e9b72227d253d278c4da6cd58..9291a5f82480f2458e04cb9ea7e6749bc952b308:/test/getfullname.c diff --git a/test/getfullname.c b/test/getfullname.c index f0c8a2d8..14f45423 100644 --- a/test/getfullname.c +++ b/test/getfullname.c @@ -1,5 +1,4 @@ -/* - * getfullname.c - Extract a user's name out of the GECOS field in +/* getfullname.c - Extract a user's name out of the GECOS field in * the password file. * * This code is Copyright (c) 2012, by the authors of nmh. See the @@ -9,28 +8,51 @@ #include #include +#include #include #include #include +extern void escape_display_name (char *, size_t); + int main(int argc, char *argv[]) { - if (argc > 1) { - fprintf (stderr, "usage: %s\n", argv[0]); + struct passwd *pwd; + char buf[BUFSIZ], *p; + + if (argc > 2) { + fprintf (stderr, "usage: %s [name]\n", argv[0]); + return 1; } + if (argc < 2) { + pwd = getpwuid(getuid()); + if (! pwd) { + fprintf(stderr, "Unable to retrieve user info for " + "userid %ld\n", (long) getuid()); + exit(1); + } - struct passwd *pwd; + strncpy(buf, pwd->pw_gecos, sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + } else + strncpy(buf, argv[1], sizeof(buf)); - pwd = getpwuid(getuid()); + /* + * Perform the same processing that getuserinfo() does. + */ - if (! pwd) { - fprintf(stderr, "Unable to retrieve user info for " - "userid %d\n", getuid()); - exit(1); - } + /* + * Stop at the first comma. + */ + if ((p = strchr(buf, ','))) + *p = '\0'; - printf("%s\n", pwd->pw_gecos); + /* + * Quote the entire string if it has a special character in it. + */ + escape_display_name (buf, sizeof(buf)); + puts(buf); exit(0); }