PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

const char* get_user_name ( char **  errstr)

Definition at line 31 of file username.c.

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

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

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 }
int uid_t
Definition: win32.h:250
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static char * username
Definition: initdb.c:131
#define NULL
Definition: c.h:229
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 74 of file username.c.

References get_user_name().

Referenced by get_id(), and main().

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 }
const char * progname
Definition: pg_standby.c:37
const char * get_user_name(char **errstr)
Definition: username.c:31