sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit d12e953f18e9746c83d4bed532fa2c51cc3c5d48
parent daad071b31a94ac86d5dd501211fde8e6984e48c
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun,  1 Jun 2014 13:57:22 +0200

add agetline, separate estrtod to util

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>

Diffstat:
MMakefile | 2++
Mseq.c | 26++++++--------------------
Mtext.h | 1+
Mutil.h | 1+
Autil/agetline.c | 13+++++++++++++
Autil/estrtod.c | 18++++++++++++++++++
6 files changed, 41 insertions(+), 20 deletions(-)

diff --git a/Makefile b/Makefile @@ -7,12 +7,14 @@ HDR = crypt.h fs.h text.h md5.h sha1.h sha256.h sha512.h util.h arg.h LIB = \ util/afgets.o \ util/agetcwd.o \ + util/agetline.o \ util/apathmax.o \ util/concat.o \ util/cp.o \ util/crypt.o \ util/enmasse.o \ util/eprintf.o \ + util/estrtod.o \ util/estrtol.o \ util/fnck.o \ util/getlines.o \ diff --git a/seq.c b/seq.c @@ -8,7 +8,6 @@ static int digitsleft(const char *); static int digitsright(const char *); -static double estrtod(const char *); static bool validfmt(const char *); static void @@ -75,7 +74,7 @@ main(int argc, char *argv[]) left = MAX(digitsleft(starts), digitsleft(ends)); snprintf(ftmp, sizeof ftmp, "%%0%d.%df", - right+left+(right != 0), right); + right + left + (right != 0), right); } else snprintf(ftmp, sizeof ftmp, "%%.%df", right); } @@ -89,7 +88,7 @@ main(int argc, char *argv[]) return EXIT_SUCCESS; } -int +static int digitsleft(const char *d) { char *exp; @@ -100,10 +99,10 @@ digitsleft(const char *d) exp = strpbrk(d, "eE"); shift = exp ? estrtol(&exp[1], 10) : 0; - return MAX(0, strspn(d, "-0123456789")+shift); + return MAX(0, strspn(d, "-0123456789") + shift); } -int +static int digitsright(const char *d) { char *exp; @@ -113,22 +112,10 @@ digitsright(const char *d) shift = exp ? estrtol(&exp[1], 10) : 0; after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0; - return MAX(0, after-shift); + return MAX(0, after - shift); } -double -estrtod(const char *s) -{ - char *end; - double d; - - d = strtod(s, &end); - if(end == s || *end != '\0') - eprintf("%s: not a real number\n", s); - return d; -} - -bool +static bool validfmt(const char *fmt) { int occur = 0; @@ -164,4 +151,3 @@ format: return false; } } - diff --git a/text.h b/text.h @@ -9,4 +9,5 @@ struct linebuf { void getlines(FILE *, struct linebuf *); char *afgets(char **, size_t *, FILE *); +ssize_t agetline(char **, size_t *, FILE *); void concat(FILE *, const char *, FILE *, const char *); diff --git a/util.h b/util.h @@ -17,6 +17,7 @@ void apathmax(char **, long *); void enmasse(int, char **, int (*)(const char *, const char *)); void eprintf(const char *, ...); void enprintf(int, const char *, ...); +double estrtod(const char *); long estrtol(const char *, int); void fnck(const char *, const char *, int (*)(const char *, const char *)); void putword(const char *); diff --git a/util/agetline.c b/util/agetline.c @@ -0,0 +1,13 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../text.h" +#include "../util.h" + +ssize_t +agetline(char **p, size_t *size, FILE *fp) +{ + return getline(p, size, fp); +} diff --git a/util/estrtod.c b/util/estrtod.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> + +#include "../util.h" + +double +estrtod(const char *s) +{ + char *end; + double d; + + d = strtod(s, &end); + if(end == s || *end != '\0') + eprintf("%s: not a real number\n", s); + return d; +}