1 /*-------------------------------------------------------------------------
6 * Portions Copyright (c) 1996-2024, 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 "common/username.h"
27 * Returns the current user name in a static buffer
28 * On error, returns NULL and sets *errstr to point to a palloc'd message
31 get_user_name(char **errstr
)
35 uid_t user_id
= geteuid();
39 errno
= 0; /* clear errno before call */
40 pw
= getpwuid(user_id
);
43 *errstr
= psprintf(_("could not look up effective user ID %ld: %s"),
45 errno
? strerror(errno
) : _("user does not exist"));
51 /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
52 /* "static" variable remains after function exit */
53 static char username
[256 + 1];
54 DWORD len
= sizeof(username
);
58 if (!GetUserName(username
, &len
))
60 *errstr
= psprintf(_("user name lookup failure: error code %lu"),
71 * Returns the current user name in a static buffer or exits
74 get_user_name_or_exit(const char *progname
)
76 const char *user_name
;
79 user_name
= get_user_name(&errstr
);
83 fprintf(stderr
, "%s: %s\n", progname
, errstr
);