//! @file getusername.cpp //! @author Andrew Schwartzmeyer //! @brief Implements GetUserName for Linux #include #include #include #include #include #include "getusername.h" //! @brief GetUserName retrieves the name of the user associated with //! the current thread. //! //! @exception errno Passes these errors via errno to GetLastError: //! - ERROR_INVALID_PARAMETER: parameter is not valid //! - ERROR_NO_SUCH_USER: there was no corresponding user //! - ERROR_GEN_FAILURE: sysconf() or getpwuid() failed for unknown reasons //! //! @retval username as UTF-8 string, or null if unsuccessful char* GetUserName() { errno = 0; struct passwd pwd; struct passwd* result; // gets the initial suggested size for buf int buflen = sysconf(_SC_GETPW_R_SIZE_MAX); if (buflen == -1) { errno = ERROR_GEN_FAILURE; return NULL; } std::string buf(buflen, 0); // geteuid() gets the effective user ID of the calling process, and is always successful int ret = getpwuid_r(geteuid(), &pwd, &buf[0], buflen, &result); // Map errno to Win32 Error Codes if (ret) { switch (errno) { case ENOENT: case ESRCH: case EBADF: case EPERM: errno = ERROR_NO_SUCH_USER; break; default: errno = ERROR_GEN_FAILURE; } return NULL; } // Check if no user matched if (result == NULL) { errno = ERROR_NO_SUCH_USER; return NULL; } // allocate copy on heap so CLR can free it return strdup(result->pw_name); }