[hackers] [sbase] sort: Librarify getlines() || Robert Ransom

From: <hg_AT_suckless.org>
Date: Tue, 22 May 2012 13:08:48 +0200 (CEST)

changeset: 127:05d5ed29caa8
user: Robert Ransom <rransom.8774_AT_gmail.com>
date: Mon May 21 21:09:38 2012 +0000
files: Makefile sort.c text.h
description:
sort: Librarify getlines()


diff -r 9c2ed93ea5ed -r 05d5ed29caa8 Makefile
--- a/Makefile Mon May 21 20:27:03 2012 +0000
+++ b/Makefile Mon May 21 21:09:38 2012 +0000
_AT_@ -14,6 +14,7 @@
         util/eprintf.o \
         util/estrtol.o \
         util/fnck.o \
+ util/getlines.o \
         util/putword.o \
         util/recurse.o \
         util/rm.o
diff -r 9c2ed93ea5ed -r 05d5ed29caa8 sort.c
--- a/sort.c Mon May 21 20:27:03 2012 +0000
+++ b/sort.c Mon May 21 21:09:38 2012 +0000
_AT_@ -12,16 +12,8 @@
 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[])
 {
_AT_@ -56,22 +48,6 @@
         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 -r 9c2ed93ea5ed -r 05d5ed29caa8 text.h
--- a/text.h Mon May 21 20:27:03 2012 +0000
+++ b/text.h Mon May 21 21:09:38 2012 +0000
_AT_@ -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 *);
Received on Tue May 22 2012 - 13:08:48 CEST

This archive was generated by hypermail 2.3.0 : Tue May 22 2012 - 13:12:11 CEST