]>
diplodocus.org Git - nmh/blob - test/getfullname.c
2 * getfullname.c - Extract a user's name out of the GECOS field in
5 * This code is Copyright (c) 2012, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 #include <sys/types.h>
18 main(int argc
, char *argv
[])
21 char name
[BUFSIZ
], *p
;
24 fprintf (stderr
, "usage: %s\n", argv
[0]);
27 pwd
= getpwuid(getuid());
30 fprintf(stderr
, "Unable to retrieve user info for "
31 "userid %ld\n", (long) getuid());
36 * Perform the same processing that getuserinfo() does.
39 strncpy(name
, pwd
->pw_gecos
, sizeof(name
));
41 name
[sizeof(name
) - 1] = '\0';
44 * Stop at the first comma
47 if ((p
= strchr(name
, ',')))
51 * Quote the entire string if it has a "." in it
54 if (strchr(name
, '.')) {
57 snprintf(tmp
, sizeof(tmp
), "\"%s\"", name
);
58 strncpy(name
, tmp
, sizeof(name
));
60 name
[sizeof(name
) - 2] = '"';
61 name
[sizeof(name
) - 1] = '\0';