1 /*-------------------------------------------------------------------------
6 * Portions Copyright (c) 1996-2014, 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, or NULL on error and
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(_("failed to look up effective user id %ld: %s"),
47 errno ? strerror(errno) : _("user does not exist"));
53 /* UNLEN = 256, 'static' variable remains after function exit */
54 static char username[256 + 1];
55 DWORD len = sizeof(username) - 1;
57 if (!GetUserName(username, &len))
59 *errstr = psprintf(_("user name lookup failure: %s"), strerror(errno));
69 * Returns the current user name in a static buffer or exits
72 get_user_name_or_exit(const char *progname)
74 const char *user_name;
77 user_name = get_user_name(&errstr);
81 fprintf(stderr, "%s: %s\n", progname, errstr);