summaryrefslogtreecommitdiff
path: root/usr.bin/sort
diff options
context:
space:
mode:
authorPedro F. Giffuni <pfg@FreeBSD.org>2015-04-07 01:17:49 +0000
committerPedro F. Giffuni <pfg@FreeBSD.org>2015-04-07 01:17:49 +0000
commit0f4b9a9057a905ed5716a4185d74b80ee8b9e299 (patch)
treefad73a2732f7b2db0af2cb353c477aae6e4cf85e /usr.bin/sort
parentb904ab99bc3a9be6772d6af0d1bf97e625e93a4c (diff)
downloadsrc-test-0f4b9a9057a905ed5716a4185d74b80ee8b9e299.tar.gz
src-test-0f4b9a9057a905ed5716a4185d74b80ee8b9e299.zip
Remove custom getdelim(3) and fix a small memory leak.
Originally from Andre Smagin. Obtained from: OpenBSD MFC after: 1 week
Notes
Notes: svn path=/head/; revision=281182
Diffstat (limited to 'usr.bin/sort')
-rw-r--r--usr.bin/sort/file.c36
-rw-r--r--usr.bin/sort/file.h12
-rw-r--r--usr.bin/sort/sort.c52
3 files changed, 28 insertions, 72 deletions
diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c
index 7a35a1be023ce..0a0088947deef 100644
--- a/usr.bin/sort/file.c
+++ b/usr.bin/sort/file.c
@@ -188,42 +188,6 @@ file_is_tmp(const char* fn)
}
/*
- * Read zero-terminated line from a file
- */
-char *
-read_file0_line(struct file0_reader *f0r)
-{
- size_t pos = 0;
- int c;
-
- if ((f0r->f == NULL) || feof(f0r->f))
- return (NULL);
-
- if (f0r->current_line && f0r->current_sz > 0)
- f0r->current_line[0] = 0;
-
- while (!feof(f0r->f)) {
- c = fgetc(f0r->f);
- if (feof(f0r->f) || (c == -1))
- break;
- if ((pos + 1) >= f0r->current_sz) {
- size_t newsz = (f0r->current_sz + 2) * 2;
- f0r->current_line = sort_realloc(f0r->current_line,
- newsz);
- f0r->current_sz = newsz;
- }
- f0r->current_line[pos] = (char)c;
- if (c == 0)
- break;
- else
- f0r->current_line[pos + 1] = 0;
- ++pos;
- }
-
- return f0r->current_line;
-}
-
-/*
* Generate new temporary file name
*/
char *
diff --git a/usr.bin/sort/file.h b/usr.bin/sort/file.h
index dbb03609585ef..e87fafda1c851 100644
--- a/usr.bin/sort/file.h
+++ b/usr.bin/sort/file.h
@@ -70,16 +70,6 @@ struct file_list
bool tmp;
};
-/*
- * Structure for zero-separated file reading (for input files list)
- */
-struct file0_reader
-{
- char *current_line;
- FILE *f;
- size_t current_sz;
-};
-
/* memory */
/**/
@@ -110,8 +100,6 @@ struct file_reader *file_reader_init(const char *fsrc);
struct bwstring *file_reader_readline(struct file_reader *fr);
void file_reader_free(struct file_reader *fr);
-char *read_file0_line(struct file0_reader *f0r);
-
void init_tmp_files(void);
void clear_tmp_files(void);
char *new_tmp_file_name(void);
diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c
index 0d725d622891e..43ae43ce4650a 100644
--- a/usr.bin/sort/sort.c
+++ b/usr.bin/sort/sort.c
@@ -229,34 +229,38 @@ usage(bool opt_err)
static void
read_fns_from_file0(const char *fn)
{
- if (fn) {
- struct file0_reader f0r;
- FILE *f;
+ FILE *f;
+ char *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen;
- f = fopen(fn, "r");
- if (f == NULL)
- err(2, NULL);
-
- memset(&f0r, 0, sizeof(f0r));
- f0r.f = f;
-
- while (!feof(f)) {
- char *line = read_file0_line(&f0r);
+ if (fn == NULL)
+ return;
- if (line && *line) {
- if (argc_from_file0 == (size_t)-1)
- argc_from_file0 = 0;
- ++argc_from_file0;
- argv_from_file0 = sort_realloc(argv_from_file0,
- argc_from_file0 * sizeof(char *));
- if (argv_from_file0 == NULL)
- err(2, NULL);
- argv_from_file0[argc_from_file0 - 1] =
- sort_strdup(line);
- }
+ f = fopen(fn, "r");
+ if (f == NULL)
+ err(2, "%s", fn);
+
+ while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) {
+ if (*line != '\0') {
+ if (argc_from_file0 == (size_t) - 1)
+ argc_from_file0 = 0;
+ ++argc_from_file0;
+ argv_from_file0 = sort_realloc(argv_from_file0,
+ argc_from_file0 * sizeof(char *));
+ if (argv_from_file0 == NULL)
+ err(2, NULL);
+ argv_from_file0[argc_from_file0 - 1] = line;
+ } else {
+ free(line);
}
- closefile(f, fn);
+ line = NULL;
+ linesize = 0;
}
+ if (ferror(f))
+ err(2, "%s: getdelim", fn);
+
+ closefile(f, fn);
}
/*