diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2015-03-20 00:26:39 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2015-03-20 00:26:39 +0000 |
commit | 7c7e2f8637af51e471ba6a8d4f4b5590199611e4 (patch) | |
tree | 68a6b3eec564ceb9a9ccce7823d0834caf7b06dd /usr.bin/sort/sort.c | |
parent | dffee829e88c5a4510cd98e45d03993b694f842c (diff) |
Remove custom getdelim(3) and fix a small memory leak. From Andre Smagin.
Diffstat (limited to 'usr.bin/sort/sort.c')
-rw-r--r-- | usr.bin/sort/sort.c | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c index cff82e4d139..d7010e82525 100644 --- a/usr.bin/sort/sort.c +++ b/usr.bin/sort/sort.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sort.c,v 1.46 2015/03/19 13:11:05 jmc Exp $ */ +/* $OpenBSD: sort.c,v 1.47 2015/03/20 00:26:38 millert Exp $ */ /*- * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> @@ -167,32 +167,36 @@ usage(int exit_val) static void read_fns_from_file0(const char *fn) { - if (fn) { - struct file0_reader f0r; - FILE *f; - - f = fopen(fn, "r"); - if (f == NULL) - err(2, "%s", fn); - - memset(&f0r, 0, sizeof(f0r)); - f0r.f = f; - - while (!feof(f)) { - char *line = read_file0_line(&f0r); - - if (line && *line) { - if (argc_from_file0 == (size_t)-1) - argc_from_file0 = 0; - ++argc_from_file0; - argv_from_file0 = sort_reallocarray(argv_from_file0, - argc_from_file0, sizeof(char *)); - argv_from_file0[argc_from_file0 - 1] = - sort_strdup(line); - } + FILE *f; + char *line = NULL; + size_t linesize = 0; + ssize_t linelen; + + if (fn == NULL) + return; + + 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_reallocarray(argv_from_file0, + argc_from_file0, sizeof(char *)); + 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); } /* |