sbase

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

commit 07b702d9a1dc86292296726f8d79d2e0979e9924
parent 6a86755fee8b7879f10aeb43b29d35c839a624b2
Author: Robert Ransom <rransom.8774@gmail.com>
Date:   Mon May 21 21:09:38 +0000

sort: Librarify getlines()
Diffstat:
Makefile | 1+
sort.c | 24------------------------
text.h | 8++++++++
3 files changed, 9 insertions(+), 24 deletions(-)
diff --git a/Makefile b/Makefile @@ -14,6 +14,7 @@ LIB = \ util/eprintf.o \ util/estrtol.o \ util/fnck.o \ + util/getlines.o \ util/putword.o \ util/recurse.o \ util/rm.o diff --git a/sort.c b/sort.c @@ -12,16 +12,8 @@ static int linecmp(const char **, const char **); static bool rflag = false; static bool uflag = false; -struct linebuf { - char **lines; - long nlines; - long capacity; -}; -#define EMPTY_LINEBUF {NULL, 0, 0,} static struct linebuf linebuf = EMPTY_LINEBUF; -static void getlines(FILE *, struct linebuf *); - int main(int argc, char *argv[]) { @@ -56,22 +48,6 @@ main(int argc, char *argv[]) return EXIT_SUCCESS; } -void -getlines(FILE *fp, struct linebuf *b) -{ - char *line = NULL; - size_t size = 0; - - while(afgets(&line, &size, fp)) { - if(++b->nlines > b->capacity && !(b->lines = realloc(b->lines, (b->capacity+=512) * sizeof *b->lines))) - eprintf("realloc:"); - if(!(b->lines[b->nlines-1] = malloc(strlen(line)+1))) - eprintf("malloc:"); - strcpy(b->lines[b->nlines-1], line); - } - free(line); -} - int linecmp(const char **a, const char **b) { diff --git a/text.h b/text.h @@ -1,4 +1,12 @@ /* See LICENSE file for copyright and license details. */ +struct linebuf { + char **lines; + long nlines; + long capacity; +}; +#define EMPTY_LINEBUF {NULL, 0, 0,} +void getlines(FILE *, struct linebuf *); + char *afgets(char **, size_t *, FILE *); void concat(FILE *, const char *, FILE *, const char *);