GRASS 8 Programmer's Manual 8.6.0dev(2026)-c83afef6d3
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 * SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11#include <unistd.h>
12#include <stdlib.h>
13
14#ifndef _WIN32
15#include <pwd.h>
16#endif
17
18#include <grass/gis.h>
19
20/*!
21 * \brief Gets user's name.
22 *
23 * Returns a pointer to a string containing the user's login name.
24 *
25 * Tries getlogin() first, then goes to the password file.
26 * However, some masscomp getlogin() fails in ucb universe
27 * because the ttyname(0) rotuine fails in ucb universe.
28 * So we check for this, too.
29 *
30 * \return pointer to string ("anonymous" by default)
31 */
32const char *G_whoami(void)
33{
34 static int initialized;
35 static const char *name;
36
37 if (G_is_initialized(&initialized))
38 return name;
39
40#ifdef _WIN32
41 name = getenv("USERNAME");
42#endif
43 if (!name || !*name)
44 name = getenv("LOGNAME");
45
46 if (!name || !*name)
47 name = getenv("USER");
48
49#ifndef _WIN32
50 if (!name || !*name) {
51 struct passwd *p = getpwuid(getuid());
52
53 if (p && p->pw_name && *p->pw_name)
54 name = G_store(p->pw_name);
55 }
56#endif
57
58 if (!name || !*name)
59 name = "anonymous";
60
61 G_initialize_done(&initialized);
62
63 return name;
64}
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:85
const char * G_whoami(void)
Gets user's name.
Definition gis/whoami.c:32
const char * name
Definition named_colr.c:6