aboutsummaryrefslogblamecommitdiff
path: root/src/legacy/cuserid.c
blob: 07866acf138dd232beaa5500724704977a4d5968 (plain) (tree)
1
2
3
4
5
6
7
8
9
                   


                   
                   


                        
                                        

                               

                                                                  
                         


                                                    
                                 
                                       

                   
#define _GNU_SOURCE
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

char *cuserid(char *buf)
{
	static char usridbuf[L_cuserid];
	struct passwd pw, *ppw;
	long pwb[256];
	getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw);
	if (!ppw)
		return 0;
	size_t len = strnlen(pw.pw_name, L_cuserid);
	if (len == L_cuserid)
		return 0;
	if (!buf) buf = usridbuf;
	memcpy(buf, pw.pw_name, len+1);
	return buf;
}