File: getfullname.c

package info (click to toggle)
nmh 1.8-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,860 kB
  • sloc: ansic: 50,445; sh: 22,697; makefile: 1,138; lex: 740; perl: 509; yacc: 265
file content (58 lines) | stat: -rw-r--r-- 1,197 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* getfullname.c - Extract a user's name out of the GECOS field in
 *		   the password file.
 *
 * This code is Copyright (c) 2012, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

extern void escape_display_name (char *, size_t);

int
main(int argc, char *argv[])
{
	struct passwd *pwd;
	char buf[BUFSIZ], *p;

        if (argc > 2) {
		fprintf (stderr, "usage: %s [name]\n", argv[0]);
		return 1;
	}
	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
		strncpy(buf, argv[1], sizeof(buf) - 1);

	/*
	 * Perform the same processing that getuserinfo() does.
	 */

	/*
	 * Stop at the first comma.
	 */
	if ((p = strchr(buf, ',')))
		*p = '\0';

	/*
	 * Quote the entire string if it has a special character in it.
	 */
	escape_display_name (buf, sizeof(buf));
	puts(buf);

	exit(0);
}