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.