xref: /netbsd/lib/libcompat/4.4/cuserid.c (revision a767f5ec)
174c187c2Scgd /*-
274c187c2Scgd  * Copyright (c) 1992, 1993
374c187c2Scgd  *	The Regents of the University of California.  All rights reserved.
474c187c2Scgd  *
574c187c2Scgd  * Redistribution and use in source and binary forms, with or without
674c187c2Scgd  * modification, are permitted provided that the following conditions
774c187c2Scgd  * are met:
874c187c2Scgd  * 1. Redistributions of source code must retain the above copyright
974c187c2Scgd  *    notice, this list of conditions and the following disclaimer.
1074c187c2Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1174c187c2Scgd  *    notice, this list of conditions and the following disclaimer in the
1274c187c2Scgd  *    documentation and/or other materials provided with the distribution.
13eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1474c187c2Scgd  *    may be used to endorse or promote products derived from this software
1574c187c2Scgd  *    without specific prior written permission.
1674c187c2Scgd  *
1774c187c2Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1874c187c2Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1974c187c2Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2074c187c2Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2174c187c2Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2274c187c2Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2374c187c2Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2474c187c2Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2574c187c2Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2674c187c2Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2774c187c2Scgd  * SUCH DAMAGE.
2874c187c2Scgd  */
2974c187c2Scgd 
30e37aec67Slukem #include <sys/cdefs.h>
3174c187c2Scgd #if defined(LIBC_SCCS) && !defined(lint)
32e37aec67Slukem #if 0
3374c187c2Scgd static char sccsid[] = "@(#)cuserid.c	8.1 (Berkeley) 6/4/93";
34e37aec67Slukem #else
35*a767f5ecSlukem __RCSID("$NetBSD: cuserid.c,v 1.8 2005/04/19 03:38:08 lukem Exp $");
36e37aec67Slukem #endif
3774c187c2Scgd #endif /* LIBC_SCCS and not lint */
3874c187c2Scgd 
3974c187c2Scgd #include <pwd.h>
4074c187c2Scgd #include <stdio.h>
4174c187c2Scgd #include <string.h>
4274c187c2Scgd #include <unistd.h>
4374c187c2Scgd 
4474c187c2Scgd char *
cuserid(char * s)45fffc26d4Schristos cuserid(char *s)
4674c187c2Scgd {
47fffc26d4Schristos 	struct passwd *pw, pwres;
48965a9562Smycroft 	static char buf[L_cuserid];
49fffc26d4Schristos 	char pwbuf[1024];
5074c187c2Scgd 
51b48252f3Slukem 	/* s may be NULL */
52b48252f3Slukem 
53*a767f5ecSlukem 	if (getpwuid_r(geteuid(), &pwres, pwbuf, sizeof(pwbuf), &pw) != 0
54*a767f5ecSlukem 	    || pw == NULL) {
55fffc26d4Schristos 		if (s != NULL)
5674c187c2Scgd 			*s = '\0';
57fffc26d4Schristos 		return s;
5874c187c2Scgd 	}
59fffc26d4Schristos 	if (s == NULL)
60965a9562Smycroft 		s = buf;
61965a9562Smycroft 	(void)strncpy(s, pw->pw_name, L_cuserid);
62fffc26d4Schristos 	return s;
6374c187c2Scgd }
64