1/* 2 * Copyright 2002-2006, Axel D��rfler, axeld@pinc-software.de. All rights reserved. 3 * Copyright 2023, Haiku, Inc. All rights reserved. 4 * Distributed under the terms of the MIT License. 5 */ 6 7 8#include <syscalls.h> 9 10#include <errno.h> 11#include <stdio.h> 12#include <string.h> 13#include <unistd.h> 14#include <pwd.h> 15 16#include <errno_private.h> 17#include <user_group.h> 18 19 20char* 21getlogin() 22{ 23 struct passwd *pw; 24 pw = getpwuid(getuid()); 25 if (pw) 26 return pw->pw_name; 27 __set_errno(ENOMEM); 28 return NULL; 29} 30 31 32int 33getlogin_r(char *name, size_t nameSize) 34{ 35 struct passwd* pw = NULL; 36 struct passwd passwdBuffer; 37 char passwdStringBuffer[MAX_PASSWD_BUFFER_SIZE]; 38 39 int status = getpwuid_r(getuid(), &passwdBuffer, passwdStringBuffer, 40 sizeof(passwdStringBuffer), &pw); 41 if (status != B_OK) 42 return status; 43 44 if (strnlen(pw->pw_name, LOGIN_NAME_MAX) >= nameSize) 45 return ERANGE; 46 47 memset(name, 0, nameSize); 48 strlcpy(name, pw->pw_name, LOGIN_NAME_MAX); 49 return B_OK; 50} 51 52 53char * 54cuserid(char *s) 55{ 56 if (s != NULL && getlogin_r(s, L_cuserid)) 57 return s; 58 59 return getlogin(); 60} 61 62