sort: Librarify getlines()
This commit is contained in:
parent
6a86755fee
commit
07b702d9a1
1
Makefile
1
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
|
||||
|
24
sort.c
24
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)
|
||||
{
|
||||
|
8
text.h
8
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 *);
|
||||
|
Loading…
Reference in New Issue
Block a user