GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
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#include <unistd.h>
14#include <stdlib.h>
15
16#ifndef _WIN32
17#include <pwd.h>
18#endif
19
20#include <grass/gis.h>
21
22/*!
23 * \brief Gets user's name.
24 *
25 * Returns a pointer to a string containing the user's login name.
26 *
27 * Tries getlogin() first, then goes to the password file.
28 * However, some masscomp getlogin() fails in ucb universe
29 * because the ttyname(0) rotuine fails in ucb universe.
30 * So we check for this, too.
31 *
32 * \return pointer to string ("anonymous" by default)
33 */
34const char *G_whoami(void)
35{
36 static int initialized;
37 static const char *name;
38
39 if (G_is_initialized(&initialized))
40 return name;
41
42#ifdef _WIN32
43 name = getenv("USERNAME");
44#endif
45 if (!name || !*name)
46 name = getenv("LOGNAME");
47
48 if (!name || !*name)
49 name = getenv("USER");
50
51#ifndef _WIN32
52 if (!name || !*name) {
53 struct passwd *p = getpwuid(getuid());
54
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}
int G_is_initialized(int *)
Definition counter.c:60
void G_initialize_done(int *)
Definition counter.c:77
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:87
const char * G_whoami(void)
Gets user's name.
Definition gis/whoami.c:34
const char * name
Definition named_colr.c:6