qh_str_to_mode()

Updated: October 28, 2024

Safely converts a string into a mode_t

Synopsis:

#include <qh/string.h>
int qh_str_to_mode(const char *str,
                   mode_t *mode,
                   size_t *end_offset)

Arguments:

str
The string to convert into a mode.
mode
Pointer to the location where the mode can be stored.
end_offset
The offset where parsing stopped.

Library:

qh

Description:

The string is interpreted as an octal (base 8).

This function is similar to strtoull(), but does more error checking on the result of the conversion and ensures that the result fits in a mode_t. This simplifies the work the caller is required to do, which is only to validate that the return value from qh_str_to_mode() is EOK.

For conversion of strings that are part of a longer one (e.g., a comma-separated list), an optional end_offset parameter can be specified. When you specify end_offset, the function returns successfully even if there are trailing characters. When non-NULL, end_offset is always updated with the offset of the last character the function parsed. When NULL, the function treats trailing characters as invalid, except for characters recognized by isspace().

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

Unlike strtoull(), this function does not accept a negative sign.

Returns:

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

ERANGE
The number value is larger than the maximum allowed, but the returned mode is still set only with the valid mode bits that were set.
  翻译: