aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2019-08-10 09:44:58 -0700
committerRob Landley <rob@landley.net>2019-08-12 04:44:00 -0500
commit30cb3aa0e33daa20d6adb0825979f79e615c9c47 (patch)
tree8134f1634c5640ffd196895fa4e4f11e8af10364
parent9dc24d994efc3837e248ef8db9b3976e8775235e (diff)
downloadtoybox-30cb3aa0e33daa20d6adb0825979f79e615c9c47.tar.gz
sort: move off get_rawline.
This was the last user of get_rawline, which lets us remove it.
-rw-r--r--lib/lib.c21
-rw-r--r--lib/pending.h3
-rw-r--r--toys/posix/sort.c47
3 files changed, 34 insertions, 37 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 53962a59..282e16d3 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -699,8 +699,7 @@ void loopfiles_lines(char **argv, void (*function)(char **pline, long len))
}
// Slow, but small.
-
-char *get_rawline(int fd, long *plen, char end)
+char *get_line(int fd)
{
char c, *buf = NULL;
long len = 0;
@@ -708,20 +707,12 @@ char *get_rawline(int fd, long *plen, char end)
for (;;) {
if (1>read(fd, &c, 1)) break;
if (!(len & 63)) buf=xrealloc(buf, len+65);
- if ((buf[len++]=c) == end) break;
+ if ((buf[len++]=c) == '\n') break;
+ }
+ if (buf) {
+ buf[len]=0;
+ if (buf[--len]=='\n') buf[len]=0;
}
- if (buf) buf[len]=0;
- if (plen) *plen = len;
-
- return buf;
-}
-
-char *get_line(int fd)
-{
- long len;
- char *buf = get_rawline(fd, &len, '\n');
-
- if (buf && buf[--len]=='\n') buf[len]=0;
return buf;
}
diff --git a/lib/pending.h b/lib/pending.h
index fbcc47c8..3c563947 100644
--- a/lib/pending.h
+++ b/lib/pending.h
@@ -6,8 +6,7 @@ int read_password(char * buff, int buflen, char* mesg);
int update_password(char *filename, char* username, char* encrypted);
// lib.c
-// These should be switched to posix-2008 getline() and getdelim()
-char *get_rawline(int fd, long *plen, char end);
+// This should be switched to posix-2008 getline()
char *get_line(int fd);
diff --git a/toys/posix/sort.c b/toys/posix/sort.c
index 9d2f2276..4b3fe24d 100644
--- a/toys/posix/sort.c
+++ b/toys/posix/sort.c
@@ -63,6 +63,7 @@ GLOBALS(
void *key_list;
int linecount;
char **lines;
+ char *name;
)
// The sort types are n, g, and M.
@@ -275,31 +276,37 @@ static int compare_keys(const void *xarg, const void *yarg)
return retval * ((flags&FLAG_r) ? -1 : 1);
}
-// Callback from loopfiles to handle input files.
-static void sort_read(int fd, char *name)
+// Read each line from file, appending to a big array.
+static void sort_lines(char **pline, long len)
{
- // Read each line from file, appending to a big array.
-
- for (;;) {
- char * line = FLAG(z) ? get_rawline(fd, NULL, 0) : get_line(fd);
+ char * line;
- if (!line) break;
+ if (!pline) return;
+ line = *pline;
+ if (!FLAG(z) && len && line[len-1]=='\n') line[--len] = 0;
+ *pline = NULL;
- // handle -c here so we don't allocate more memory than necessary.
- if (FLAG(c)) {
- int j = FLAG(u) ? -1 : 0;
+ // handle -c here so we don't allocate more memory than necessary.
+ if (FLAG(c)) {
+ int j = FLAG(u) ? -1 : 0;
- if (TT.lines && compare_keys((void *)&TT.lines, &line)>j)
- error_exit("%s: Check line %d\n", name, TT.linecount);
- free(TT.lines);
- TT.lines = (char **)line;
- } else {
- if (!(TT.linecount&63))
- TT.lines = xrealloc(TT.lines, sizeof(char *)*(TT.linecount+64));
- TT.lines[TT.linecount] = line;
- }
- TT.linecount++;
+ if (TT.lines && compare_keys((void *)&TT.lines, &line)>j)
+ error_exit("%s: Check line %d\n", TT.name, TT.linecount);
+ free(TT.lines);
+ TT.lines = (char **)line;
+ } else {
+ if (!(TT.linecount&63))
+ TT.lines = xrealloc(TT.lines, sizeof(char *)*(TT.linecount+64));
+ TT.lines[TT.linecount] = line;
}
+ TT.linecount++;
+}
+
+// Callback from loopfiles to handle input files.
+static void sort_read(int fd, char *name)
+{
+ TT.name = name;
+ do_lines(fd, FLAG(z) ? '\0' : '\n', sort_lines);
}
void sort_main(void)