1152830Sdavidxu/*-
2152830Sdavidxu * Copyright (c) 1992, 1993
3152830Sdavidxu *	The Regents of the University of California.  All rights reserved.
4152830Sdavidxu *
5152830Sdavidxu * Redistribution and use in source and binary forms, with or without
6152830Sdavidxu * modification, are permitted provided that the following conditions
7152830Sdavidxu * are met:
8152830Sdavidxu * 1. Redistributions of source code must retain the above copyright
9152830Sdavidxu *    notice, this list of conditions and the following disclaimer.
10152830Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright
11152830Sdavidxu *    notice, this list of conditions and the following disclaimer in the
12152830Sdavidxu *    documentation and/or other materials provided with the distribution.
13152830Sdavidxu * 3. Neither the name of the University nor the names of its contributors
14152830Sdavidxu *    may be used to endorse or promote products derived from this software
15152830Sdavidxu *    without specific prior written permission.
16152830Sdavidxu *
17152830Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18152830Sdavidxu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19152830Sdavidxu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20152830Sdavidxu * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21152830Sdavidxu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22152830Sdavidxu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23152830Sdavidxu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24152830Sdavidxu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25152830Sdavidxu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26152830Sdavidxu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27152830Sdavidxu * SUCH DAMAGE.
28152830Sdavidxu */
29152830Sdavidxu
30152830Sdavidxu#include <sys/cdefs.h>
31152830Sdavidxu#if defined(LIBC_SCCS) && !defined(lint)
32152950Sdavidxu#if 0
33152830Sdavidxustatic char sccsid[] = "@(#)cuserid.c	8.1 (Berkeley) 6/4/93";
34152830Sdavidxu#else
35240297Sdavidxu__RCSID("$NetBSD: cuserid.c,v 1.7 2005/04/01 12:51:07 christos Exp $");
36152830Sdavidxu#endif
37152830Sdavidxu#endif /* LIBC_SCCS and not lint */
38152830Sdavidxu
39152950Sdavidxu#include <pwd.h>
40152830Sdavidxu#include <stdio.h>
41152830Sdavidxu#include <string.h>
42152830Sdavidxu#include <unistd.h>
43152830Sdavidxu
44152830Sdavidxuchar *
45152830Sdavidxucuserid(char *s)
46152830Sdavidxu{
47152830Sdavidxu	struct passwd *pw, pwres;
48152830Sdavidxu	static char buf[L_cuserid];
49152830Sdavidxu	char pwbuf[1024];
50152830Sdavidxu
51152830Sdavidxu	/* s may be NULL */
52152830Sdavidxu
53165828Sdavidxu	if (getpwuid_r(geteuid(), &pwres, pwbuf, sizeof(pwbuf), &pw) != 0
54165828Sdavidxu	    || pw == NULL) {
55152950Sdavidxu		if (s != NULL)
56152830Sdavidxu			*s = '\0';
57		return s;
58	}
59	if (s == NULL)
60		s = buf;
61	(void)strncpy(s, pw->pw_name, L_cuserid);
62	return s;
63}
64