add agetline, separate estrtod to util

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
This commit is contained in:
Hiltjo Posthuma 2014-06-01 13:57:22 +02:00 committed by sin
parent daad071b31
commit d12e953f18
6 changed files with 41 additions and 20 deletions

View File

@ -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 \

26
seq.c
View File

@ -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;
}
}

1
text.h
View File

@ -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 *);

1
util.h
View File

@ -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 *);

13
util/agetline.c Normal file
View File

@ -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);
}

18
util/estrtod.c Normal file
View File

@ -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;
}