GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
gis/whoami.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/whoami.c
3  *
4  * \brief GIS Library - Login name functions.
5  *
6  * (C) 2001-2009 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author Original author CERL
12  */
13 
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 
23 /*!
24  * \brief Gets user's name.
25  *
26  * Returns a pointer to a string containing the user's login name.
27  *
28  * Tries getlogin() first, then goes to the password file.
29  * However, some masscomp getlogin() fails in ucb universe
30  * because the ttyname(0) rotuine fails in ucb universe.
31  * So we check for this, too.
32  *
33  * \return pointer to string ("anonymous" by default)
34  */
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 }
void G_initialize_done(int *)
Definition: counter.c:76
const char * G_whoami(void)
Gets user&#39;s name.
Definition: gis/whoami.c:35
int G_is_initialized(int *)
Definition: counter.c:59
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
const char * name
Definition: named_colr.c:7
char * getenv()