1 /*-------------------------------------------------------------------------
6 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/common/username.c
12 *-------------------------------------------------------------------------
18 #include "postgres_fe.h"
24 #include <sys/types.h>
26 #include "common/username.h"
29 * Returns the current user name in a static buffer
30 * On error, returns NULL and sets *errstr to point to a palloc'd message
33 get_user_name(char **errstr)
37 uid_t user_id = geteuid();
41 errno = 0; /* clear errno before call */
42 pw = getpwuid(user_id);
45 *errstr = psprintf(_("could not look up effective user ID %ld: %s"),
47 errno ? strerror(errno) : _("user does not exist"));
53 /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
54 /* "static" variable remains after function exit */
55 static char username[256 + 1];
56 DWORD len = sizeof(username);
60 if (!GetUserName(username, &len))
62 *errstr = psprintf(_("user name lookup failure: error code %lu"),
73 * Returns the current user name in a static buffer or exits
76 get_user_name_or_exit(const char *progname)
78 const char *user_name;
81 user_name = get_user_name(&errstr);
85 fprintf(stderr, "%s: %s\n", progname, errstr);