X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/265bf7d31f31962e9b72227d253d278c4da6cd58..86c5a525f242b0a0464d1fa9a5a53029401d093a:/test/getfullname.c diff --git a/test/getfullname.c b/test/getfullname.c index f0c8a2d8..d8ff1015 100644 --- a/test/getfullname.c +++ b/test/getfullname.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -16,21 +17,51 @@ 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) { fprintf(stderr, "Unable to retrieve user info for " - "userid %d\n", getuid()); + "userid %ld\n", (long) getuid()); 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); }