1a7c91847Schristos /* Provide a working getlogin_r for systems which lack it.
2a7c91847Schristos
3a7c91847Schristos Copyright (C) 2005 Free Software Foundation, Inc.
4a7c91847Schristos
5a7c91847Schristos This program is free software; you can redistribute it and/or modify
6a7c91847Schristos it under the terms of the GNU General Public License as published by
7a7c91847Schristos the Free Software Foundation; either version 2, or (at your option)
8a7c91847Schristos any later version.
9a7c91847Schristos
10a7c91847Schristos This program is distributed in the hope that it will be useful,
11a7c91847Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12a7c91847Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a7c91847Schristos GNU General Public License for more details.
14a7c91847Schristos
15a7c91847Schristos You should have received a copy of the GNU General Public License
16a7c91847Schristos along with this program; if not, write to the Free Software Foundation,
17a7c91847Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18*5a6c14c8Schristos #include <sys/cdefs.h>
19*5a6c14c8Schristos __RCSID("$NetBSD: getlogin_r.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
20*5a6c14c8Schristos
21a7c91847Schristos
22a7c91847Schristos /* written by Paul Eggert and Derek Price */
23a7c91847Schristos
24a7c91847Schristos #ifdef HAVE_CONFIG_H
25a7c91847Schristos # include <config.h>
26a7c91847Schristos #endif
27a7c91847Schristos
28a7c91847Schristos #include "getlogin_r.h"
29a7c91847Schristos
30a7c91847Schristos #include <errno.h>
31a7c91847Schristos #include <string.h>
32a7c91847Schristos
33a7c91847Schristos #if HAVE_UNISTD_H
34a7c91847Schristos # include <unistd.h>
35a7c91847Schristos #endif
36a7c91847Schristos
37a7c91847Schristos #if !HAVE_DECL_GETLOGIN
38a7c91847Schristos char *getlogin (void);
39a7c91847Schristos #endif
40a7c91847Schristos
41a7c91847Schristos /* See getlogin_r.h for documentation. */
42a7c91847Schristos int
getlogin_r(char * name,size_t size)43a7c91847Schristos getlogin_r (char *name, size_t size)
44a7c91847Schristos {
45a7c91847Schristos char *n;
46a7c91847Schristos size_t nlen;
47a7c91847Schristos
48a7c91847Schristos errno = 0;
49a7c91847Schristos n = getlogin ();
50a7c91847Schristos
51a7c91847Schristos /* A system function like getlogin_r is never supposed to set errno
52a7c91847Schristos to zero, so make sure errno is nonzero here. ENOENT is a
53a7c91847Schristos reasonable errno value if getlogin returns NULL. */
54a7c91847Schristos if (!errno)
55a7c91847Schristos errno = ENOENT;
56a7c91847Schristos
57a7c91847Schristos if (!n)
58a7c91847Schristos return errno;
59a7c91847Schristos nlen = strlen (n);
60a7c91847Schristos if (size <= nlen)
61a7c91847Schristos return ERANGE;
62a7c91847Schristos memcpy (name, n, nlen + 1);
63a7c91847Schristos return 0;
64a7c91847Schristos }
65