X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=test%2Fgetfullname.c;h=0e12fe9389fde896a80b53a7cfb6ce0087254a7b;hb=d8c7d4f2c785d35cebd5ea64213a335561a57f7d;hp=3afa00f87ea685df8801a8e17a5eab60681da4bd;hpb=23af631076209b31fb28feb90a779ff8dcc153a2;p=mmh diff --git a/test/getfullname.c b/test/getfullname.c index 3afa00f..0e12fe9 100644 --- a/test/getfullname.c +++ b/test/getfullname.c @@ -9,24 +9,53 @@ #include #include +#include #include #include #include +extern void escape_display_name (char *, size_t); + int main(int argc, char *argv[]) { struct passwd *pwd; + char buf[BUFSIZ], *p; + + if (argc < 2) { + pwd = getpwuid(getuid()); - pwd = getpwuid(getuid()); + if (! pwd) { + fprintf(stderr, "Unable to retrieve user info for " + "userid %ld\n", (long) getuid()); + exit(1); + } - if (! pwd) { - fprintf(stderr, "Unable to retrieve user info for " - "userid %d\n", getuid()); - exit(1); + strncpy(buf, pwd->pw_gecos, sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + } else if (argc == 2) { + strncpy(buf, argv[1], sizeof(buf)); + } else if (argc > 2) { + fprintf (stderr, "usage: %s [name]\n", argv[0]); + return 1; } - printf("%s\n", pwd->pw_gecos); + /* + * Perform the same processing that getuserinfo() does. + */ + + /* + * Stop at the first comma. + */ + if ((p = strchr(buf, ','))) + *p = '\0'; + + /* + * Quote the entire string if it has a special character in it. + */ + escape_display_name (buf, sizeof(buf)); + + printf("%s\n", buf); exit(0); }