GRASS GIS 7 Programmer's Manual  7.7.svn(2018)-r73574
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 }
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: gis/whoami.c:35
const char * name
Definition: named_colr.c:7
char * getenv()