On 15.01.2018 18:23, Collin L. Walling wrote:
> On 01/15/2018 12:05 PM, Eric Blake wrote:
>> On 01/15/2018 10:44 AM, Collin L. Walling wrote:
[...]
>>> +/**
>>> + * atoi:
>>> + * @str: the string to be converted.
>>> + *
>>> + * Given a string @str, convert it to an integer. Any non-numerical
>>> value
>>> + * will terminate the conversion.
>>> + *
>>> + * Returns: an integer converted from the string @str.
>>> + */
>>> +int atoi(const char *str)
>>> +{
>>> + int i;
>>> + int val = 0;
>>> +
>>> + for (i = 0; str[i]; i++) {
>>> + char c = str[i];
>>> + if (!isdigit(c)) {
>>> + break;
>>> + }
>>> + val *= 10;
>>> + val += c - '0';
>> Silently gives garbage on integer overflow, but matches the fact that
>> POSIX atoi() can't flag errors. However, it does not handle leading
>> whitespace nor '-', which means it is NOT doing a POSIX-compatible
>> atoi() implementation; naming it atoi() is perhaps thus a disservice to
>> end users.
>
> Fair enough. Perhaps the "strtoi" convention suits this better.
Or maybe simply add an assert(str[0] != '-') for now. If we ever hit the
assert, we can still add the support for negative numbers if necessary.
>>> +static inline size_t strlen(const char *str)
>>> +{
>>> + size_t i;
>>> + for (i = 0; *str; i++) {
>>> + str++;
>>> + }
>>> + return i;
>> Again, not the fastest implementation, but that shouldn't matter.
Yes, indeed, speed does not really matter here for the some few bytes
that are handled during the life-time of the s390-ccw bios.
>>> +}
>>> +
>>> +static inline int isdigit(int c)
>>> +{
>>> + return (c >= '0') && (c <= '9');
>>> +}
>>> +
>>> +int atoi(const char *str);
>>> +char *itostr(int num, char *str, size_t len);
>>> +
>>> #endif
>
>
Thomas