PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
username.h File Reference
This graph shows which files directly or indirectly include this file:

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

const char* get_user_name ( char **  errstr)

Definition at line 33 of file username.c.

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

Referenced by get_user_info(), get_user_name_or_exit(), and usage().

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

Definition at line 76 of file username.c.

References get_user_name().

Referenced by get_id(), and main().

77 {
78  const char *user_name;
79  char *errstr;
80 
81  user_name = get_user_name(&errstr);
82 
83  if (!user_name)
84  {
85  fprintf(stderr, "%s: %s\n", progname, errstr);
86  exit(1);
87  }
88  return user_name;
89 }
const char * progname
Definition: pg_standby.c:37
const char * get_user_name(char **errstr)
Definition: username.c:33