aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_id/pg_id.c
blob: b782b091a3e5c0e3e8cc06251682a6fc5693dd42 (plain)
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
59
60
61
/*-------------------------------------------------------------------------
 *
 * pg_id.c--
 *	  Print the user ID for the login name passed as argument,
 *	  or the real user ID of the caller if no argument.  If the
 *	  login name doesn't exist, print "NOUSER" and exit 1.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/bin/pg_id/Attic/pg_id.c,v 1.4 1997/09/07 04:54:46 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int
main(int argc, char **argv)
{
	struct passwd  *pw;
	int				ch;
	extern int		optind;

	while ((ch = getopt(argc, argv, "")) != EOF)
		switch (ch)
		{
		case '?':
		default:
			fprintf(stderr, "usage: pg_id [login]\n");
			exit(1);
		}
	argc -= optind;
	argv += optind;

	if (argc > 0)
	{
		if (argc > 1)
		{
			fprintf(stderr, "usage: pg_id [login]\n");
			exit(1);
		}
		if ((pw = getpwnam(argv[0])) == NULL)
		{
			printf("NOUSER\n");
			exit(1);
		}
		printf("%ld\n", (long) pw->pw_uid);
	}
	else
	{
		printf("%ld\n", (long) getuid());
	}

	exit(0);
}