qh_str_username_to_uid()

Updated: April 19, 2023

Safely converts a string into a uid_t

Synopsis:

#include <qh/string.h>
int qh_str_username_to_uid(const char *str,
                           uid_t *uid)

Arguments:

str
The string to convert into a uid_t.
uid
Pointer to the location where the uid_t can be stored.

Library:

qh

Description:

The qh_str_username_to_uid() function first checks the password database to determine if the string corresponds to a user name. If it does, the user ID for that account is returned. Otherwise, the function attempts to interpret the string as a decimal number (base 10) similar to strtoll(), but does more error checking on the result of the conversion and ensures that the result fits in a uid_t. This simplifies the work the caller is required to do, which is only to validate that the return value from qh_str_username_to_uid() is EOK.

For more information on using this function, see the documentation for strtoll().

Returns:

EOK on success or a standard errno on failure (errno is also set, and uid isn't updated).