GRASS GIS 8 Programmer's Manual 8.4.1(2025)-45ca3179ab
Loading...
Searching...
No Matches
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 */
35const 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
56 if (p && p->pw_name && *p->pw_name)
57 name = G_store(p->pw_name);
58 }
59#endif
60
61 if (!name || !*name)
62 name = "anonymous";
63
64 G_initialize_done(&initialized);
65
66 return name;
67}
void G_initialize_done(int *p)
Definition counter.c:77
int G_is_initialized(int *p)
Definition counter.c:60
const char * name
Definition named_colr.c:6
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
const char * G_whoami(void)
Gets user's name.
Definition whoami.c:35