[tyndur-devel] [PATCH] libc: Vorzeichen beachten in atol
Andreas Freimuth ·
! atol beachted jetzt auch das gespeicherte Vorzeichen Signed-off-by: Andreas Freimuth <m.nemo at gmx.net> --- src/lib/string.c | 20 ++++++++++++-------- 1 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/lib/string.c b/src/lib/string.c index d6d662f..4b4c9dd 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -28,6 +28,7 @@ #include <lost/config.h> #include <string.h> #include <stdlib.h> +#include <stdbool.h> #include <ctype.h> #include <errno.h> @@ -165,12 +166,12 @@ long atol(const char *str) if (!*str) { return 0; } - int positive = 1; + bool positive = true; long result = 0; int length = 0; //Vorzeichen if (*str == '-') { - positive = 0; + positive = false; str++; } else if (*str == '+') { str++; @@ -184,17 +185,20 @@ long atol(const char *str) //Ziffer hinzufügen result = result * 10 + c - '0'; } else { - if (!length) { - return 0; - } - return result; - } + break; + } str++; } - + if (!length) { return 0; } + + // Vorzeichen anwenden + if (!positive) { + result = -result; + } + return result; } -- 1.7.4.2