X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=test%2Fgetfullname.c;h=d8ff10152649080443d1cbeb9f0ae466ca76ed71;hb=deceb473fdbaf526c46ff696a8d7afc3a81e4d90;hp=f0c8a2d8f57687330777183168cd10bf11eeeb32;hpb=265bf7d31f31962e9b72227d253d278c4da6cd58;p=mmh diff --git a/test/getfullname.c b/test/getfullname.c index f0c8a2d..d8ff101 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); }