X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=test%2Fgetfullname.c;h=0e12fe9389fde896a80b53a7cfb6ce0087254a7b;hb=75e27e3157b8023906ac68ea43b47555ddf5cd51;hp=3dc221e530866139f5c58f3bb89c04784cf0ad06;hpb=8eeb81b97870153a57c9dff79540c70d89ef87a4;p=mmh diff --git a/test/getfullname.c b/test/getfullname.c index 3dc221e..0e12fe9 100644 --- a/test/getfullname.c +++ b/test/getfullname.c @@ -14,54 +14,48 @@ #include #include +extern void escape_display_name (char *, size_t); + 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 %d\n", getuid()); - exit(1); + char buf[BUFSIZ], *p; + + 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) { + strncpy(buf, argv[1], sizeof(buf)); + } 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, ','))) + if ((p = strchr(buf, ','))) *p = '\0'; /* - * Quote the entire string if it has a "." in it + * Quote the entire string if it has a special character in it. */ + escape_display_name (buf, sizeof(buf)); - 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); + printf("%s\n", buf); exit(0); }