X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=test%2Fgetfullname.c;h=ec83939f75ab537411194d269bc2ed98f26b406e;hb=f423b06bf5ab4a966b17857f027a46e74447f438;hp=d8ff10152649080443d1cbeb9f0ae466ca76ed71;hpb=393c9290adc27e6d85b780c2434f51f95ea6df5b;p=mmh diff --git a/test/getfullname.c b/test/getfullname.c index d8ff101..ec83939 100644 --- a/test/getfullname.c +++ b/test/getfullname.c @@ -14,52 +14,47 @@ #include #include +extern void escape_display_name (char *); + int main(int argc, char *argv[]) { struct passwd *pwd; - char name[BUFSIZ], *p; - - if (argc > 1) { - fprintf (stderr, "usage: %s\n", argv[0]); - } - - pwd = getpwuid(getuid()); - - if (! pwd) { - fprintf(stderr, "Unable to retrieve user info for " - "userid %ld\n", (long) getuid()); - exit(1); + char buf[BUFSIZ], *p; + char *name = buf; + + if (argc < 2) { + pwd = getpwuid(getuid()); + + if (! pwd) { + fprintf(stderr, "Unable to retrieve user info for " + "userid %ld\n", (long) getuid()); + exit(1); + } + + strncpy(buf, pwd->pw_gecos, sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + } else if (argc == 2) { + name = argv[1]; + } else if (argc > 2) { + fprintf (stderr, "usage: %s [name]\n", argv[0]); + return 1; } /* * Perform the same processing that getuserinfo() does. */ - strncpy(name, pwd->pw_gecos, sizeof(name)); - - name[sizeof(name) - 1] = '\0'; - /* - * Stop at the first comma + * Stop at the first comma. */ - if ((p = strchr(name, ','))) *p = '\0'; /* - * Quote the entire string if it has a "." in it + * Quote the entire string if it has a special character 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'; - } + escape_display_name (name); printf("%s\n", name);