diff --git a/src/system/libroot/posix/unistd/Jamfile b/src/system/libroot/posix/unistd/Jamfile index 522f4c5767..d7edefe59a 100644 --- a/src/system/libroot/posix/unistd/Jamfile +++ b/src/system/libroot/posix/unistd/Jamfile @@ -22,7 +22,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { exec.cpp _exit.c fork.c - getlogin.c + getlogin.cpp getpagesize.c hostname.cpp ioctl.c diff --git a/src/system/libroot/posix/unistd/getlogin.c b/src/system/libroot/posix/unistd/getlogin.c deleted file mode 100644 index 9d809de4db..0000000000 --- a/src/system/libroot/posix/unistd/getlogin.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2002-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved. - * Distributed under the terms of the MIT License. - */ - - -#include - -#include -#include -#include -#include -#include - -#include - - -char *getlogin() -{ - struct passwd *pw; - pw = getpwuid(getuid()); - if (pw) - return pw->pw_name; - __set_errno(ENOMEM); - return NULL; -} - - -int getlogin_r(char *name, size_t nameSize) -{ - struct passwd *pw; - pw = getpwuid(getuid()); - if (pw && (nameSize >= LOGIN_NAME_MAX)) { - memset(name, 0, nameSize); - strlcpy(name, pw->pw_name, LOGIN_NAME_MAX); - return B_OK; - } - return ENOMEM; -} - - -char * -cuserid(char *s) -{ - if (s != NULL && getlogin_r(s, L_cuserid)) - return s; - - return getlogin(); -} - diff --git a/src/system/libroot/posix/unistd/getlogin.cpp b/src/system/libroot/posix/unistd/getlogin.cpp new file mode 100644 index 0000000000..03df6fe241 --- /dev/null +++ b/src/system/libroot/posix/unistd/getlogin.cpp @@ -0,0 +1,61 @@ +/* + * Copyright 2002-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Copyright 2023, Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + */ + + +#include + +#include +#include +#include +#include +#include + +#include +#include + + +char* +getlogin() +{ + struct passwd *pw; + pw = getpwuid(getuid()); + if (pw) + return pw->pw_name; + __set_errno(ENOMEM); + return NULL; +} + + +int +getlogin_r(char *name, size_t nameSize) +{ + struct passwd* pw = NULL; + struct passwd passwdBuffer; + char passwdStringBuffer[MAX_PASSWD_BUFFER_SIZE]; + + int status = getpwuid_r(getuid(), &passwdBuffer, passwdStringBuffer, + sizeof(passwdStringBuffer), &pw); + if (status != B_OK) + return status; + + if (strnlen(pw->pw_name, LOGIN_NAME_MAX) >= nameSize) + return ERANGE; + + memset(name, 0, nameSize); + strlcpy(name, pw->pw_name, LOGIN_NAME_MAX); + return B_OK; +} + + +char * +cuserid(char *s) +{ + if (s != NULL && getlogin_r(s, L_cuserid)) + return s; + + return getlogin(); +} +