qh_str_to_signal()

Updated: April 19, 2023

Safely converts a string that represents a signal name or number into an int

Synopsis:

#include <qh/string.h>
int qh_str_to_signal(const char *str,
                     int *number,
                     size_t *end_offset)

Arguments:

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

Library:

qh

Description:

The qh_str_to_signal() function converts a string that represents a signal number or name (e.g., term, sigterm, SigTerm, etc.) to a signal number. The string is interpreted as a decimal (base 10). Passing it "SIGHUP" is equivalent to passing it "1", "HUP", or "sigHUP" (the case does not matter).

It is similar to strtoll(), but does more error checking on the result of the conversion and ensures that the result fits in an int. This simplifies the work the caller is required to do, which is only to validate that the return value from qh_str_to_signal() is EOK.

To convert strings that are part of a longer one (e.g., a comma-separated list), you can specify the optional end_offset parameter. 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 strtoll().

Returns:

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