PostgreSQL Source Code git master
username.c File Reference
#include "postgres.h"
#include <pwd.h>
#include <unistd.h>
#include "common/username.h"
Include dependency graph for username.c:

Go to the source code of this file.

Functions

const char * get_user_name (char **errstr)
 
const char * get_user_name_or_exit (const char *progname)
 

Function Documentation

◆ get_user_name()

const char * get_user_name ( char **  errstr)

Definition at line 31 of file username.c.

32{
33#ifndef WIN32
34 struct passwd *pw;
35 uid_t user_id = geteuid();
36
37 *errstr = NULL;
38
39 errno = 0; /* clear errno before call */
40 pw = getpwuid(user_id);
41 if (!pw)
42 {
43 *errstr = psprintf(_("could not look up effective user ID %ld: %s"),
44 (long) user_id,
45 errno ? strerror(errno) : _("user does not exist"));
46 return NULL;
47 }
48
49 return pw->pw_name;
50#else
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);
55
56 *errstr = NULL;
57
58 if (!GetUserName(username, &len))
59 {
60 *errstr = psprintf(_("user name lookup failure: error code %lu"),
61 GetLastError());
62 return NULL;
63 }
64
65 return username;
66#endif
67}
#define _(x)
Definition: elog.c:90
static char * username
Definition: initdb.c:153
const void size_t len
#define strerror
Definition: port.h:251
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
int uid_t
Definition: win32_port.h:234

References _, len, psprintf(), strerror, and username.

Referenced by get_user_info(), and get_user_name_or_exit().

◆ get_user_name_or_exit()

const char * get_user_name_or_exit ( const char *  progname)

Definition at line 74 of file username.c.

75{
76 const char *user_name;
77 char *errstr;
78
79 user_name = get_user_name(&errstr);
80
81 if (!user_name)
82 {
83 fprintf(stderr, "%s: %s\n", progname, errstr);
84 exit(1);
85 }
86 return user_name;
87}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
exit(1)
const char * progname
Definition: main.c:44
const char * get_user_name(char **errstr)
Definition: username.c:31

References exit(), fprintf, get_user_name(), and progname.

Referenced by get_id(), and main().