X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=test%2Fgetfullname.c;h=3dc221e530866139f5c58f3bb89c04784cf0ad06;hb=8eeb81b97870153a57c9dff79540c70d89ef87a4;hp=3afa00f87ea685df8801a8e17a5eab60681da4bd;hpb=23af631076209b31fb28feb90a779ff8dcc153a2;p=mmh diff --git a/test/getfullname.c b/test/getfullname.c index 3afa00f..3dc221e 100644 --- a/test/getfullname.c +++ b/test/getfullname.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,11 @@ 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()); @@ -26,7 +32,36 @@ main(int argc, char *argv[]) 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); }