GRASS GIS 7 Programmer's Manual  7.0.5(2016)-r00000
whoami.c
Go to the documentation of this file.
1 
14 #include <unistd.h>
15 #include <stdlib.h>
16 
17 #ifndef __MINGW32__
18 #include <pwd.h>
19 #endif
20 
21 #include <grass/gis.h>
22 
35 const char *G_whoami(void)
36 {
37  static int initialized;
38  static const char *name;
39 
40  if (G_is_initialized(&initialized))
41  return name;
42 
43 #ifdef __MINGW32__
44  name = getenv("USERNAME");
45 #endif
46  if (!name || !*name)
47  name = getenv("LOGNAME");
48 
49  if (!name || !*name)
50  name = getenv("USER");
51 
52 #ifndef __MINGW32__
53  if (!name || !*name) {
54  struct passwd *p = getpwuid(getuid());
55  if (p && p->pw_name && *p->pw_name)
56  name = G_store(p->pw_name);
57  }
58 #endif
59 
60  if (!name || !*name)
61  name = "anonymous";
62 
63  G_initialize_done(&initialized);
64 
65  return name;
66 }
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
int G_is_initialized(int *p)
Definition: counter.c:59
void G_initialize_done(int *p)
Definition: counter.c:76
const char * G_whoami(void)
Gets user&#39;s name.
Definition: whoami.c:35
const char * name
Definition: named_colr.c:7