From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jason Wang Subject: [PATCH kvm-unit-tests v2 6/8] Introduce atol() Date: Tue, 31 Aug 2010 16:37:32 +0800 Message-ID: <20100831083732.10672.98099.stgit@FreeLancer> References: <20100831083216.10672.20413.stgit@FreeLancer> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit To: zamsden@redhat.com, glommer@redhat.com, mtosatti@redhat.com, avi@redhat.com, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:31122 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753502Ab0HaIhf (ORCPT ); Tue, 31 Aug 2010 04:37:35 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o7V8bZ53006963 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 31 Aug 2010 04:37:35 -0400 In-Reply-To: <20100831083216.10672.20413.stgit@FreeLancer> Sender: kvm-owner@vger.kernel.org List-ID: Signed-off-by: Jason Wang --- lib/libcflat.h | 1 + lib/string.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 0 deletions(-) diff --git a/lib/libcflat.h b/lib/libcflat.h index 9cb90cf..d4ee2e0 100644 --- a/lib/libcflat.h +++ b/lib/libcflat.h @@ -50,6 +50,7 @@ extern void puts(const char *s); extern void *memset(void *s, int c, size_t n); +extern long atol(const char *ptr); #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0])) #endif diff --git a/lib/string.c b/lib/string.c index acac3c0..1f19f5c 100644 --- a/lib/string.c +++ b/lib/string.c @@ -30,3 +30,34 @@ void *memset(void *s, int c, size_t n) return s; } + +long atol(const char *ptr) +{ + long acc = 0; + const char *s = ptr; + int neg, c; + + while (*s == ' ' || *s == '\t') + s++; + if (*s == '-'){ + neg = 1; + s++; + } else { + neg = 0; + if (*s == '+') + s++; + } + + while (*s) { + if (*s < '0' || *s > '9') + break; + c = *s - '0'; + acc = acc * 10 + c; + s++; + } + + if (neg) + acc = -acc; + + return acc; +}