aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_id/pg_id.c
blob: 7fa66d0c4c120ab3470cc67d83c8be8a96a35012 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * pg_id.c
 *
 * A crippled id utility for use in various shell scripts in use by PostgreSQL
 * (in particular initdb)
 *
 * Copyright (c) 2000-2003, PostgreSQL Global Development Group
 *
 * $Header: /cvsroot/pgsql/src/bin/pg_id/Attic/pg_id.c,v 1.25 2003/10/10 01:34:51 momjian Exp $
 */
#include "postgres_fe.h"

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

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

int
main(int argc, char *argv[])
{
	int			c;
	int			name_only_flag = 0,
				use_real_uid_flag = 0,
				limit_user_info = 0;
	const char *username = NULL;
	extern int	optind;
#ifndef WIN32
	struct passwd *pw;
#else
	struct passwd_win32
	{
		int pw_uid;
		char pw_name[128];
	} pass_win32;
	struct passwd_win32 *pw = &pass_win32;
	unsigned long pwname_size = sizeof(pass_win32.pw_name) - 1;

	pw->pw_uid = 1;
#endif

	while ((c = getopt(argc, argv, "nru")) != -1)
	{
		switch (c)
		{
			case 'n':
				name_only_flag = 1;
				break;
			case 'r':
				use_real_uid_flag = 1;
				break;
			case 'u':
				limit_user_info = 1;
				break;
			default:
#ifndef WIN32
				fprintf(stderr, "Usage: %s [-n] [-r] [-u] [username]\n", argv[0]);
#else
				fprintf(stderr, "Usage: %s [-n] [-r] [-u]\n", argv[0]);
#endif
				exit(1);
		}
	}

	if (argc - optind >= 1)
#ifndef WIN32
		username = argv[optind];
#else
	{
		fprintf(stderr, "%s: specifying a username is not supported on this platform\n", argv[0]);
		exit(1);
	}	
#endif

	if (name_only_flag && !limit_user_info)
	{
		fprintf(stderr, "%s: -n must be used together with -u\n", argv[0]);
		exit(1);
	}
	if (username && use_real_uid_flag)
	{
		fprintf(stderr, "%s: -r cannot be used when a user name is given\n", argv[0]);
		exit(1);
	}

#ifndef WIN32
	if (username)
	{
		pw = getpwnam(username);
		if (!pw)
		{
			fprintf(stderr, "%s: %s: no such user\n", argv[0], username);
			exit(1);
		}
	}
	else if (use_real_uid_flag)
		pw = getpwuid(getuid());
	else
		pw = getpwuid(geteuid());

	if (!pw)
	{
		perror(argv[0]);
		exit(1);
	}
#else
	if (!use_real_uid_flag)
	{
		fprintf(stderr, "%s: -r must be used on this platform\n", argv[0]);
		exit(1);
	}

	GetUserName(pw->pw_name, &pwname_size);
#endif

	if (!limit_user_info)
		printf("uid=%d(%s)\n", (int) pw->pw_uid, pw->pw_name);
	else if (name_only_flag)
		puts(pw->pw_name);
	else
#ifdef __BEOS__
	if (pw->pw_uid == 0)
		printf("1\n");
	else
#endif
		printf("%d\n", (int) pw->pw_uid);

	return 0;
}