aboutsummaryrefslogtreecommitdiff
path: root/coreutils/paste.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/paste.c')
-rw-r--r--coreutils/paste.c36
1 files changed, 24 insertions, 12 deletions
diff --git a/coreutils/paste.c b/coreutils/paste.c
index 3e5f20158..363340e3d 100644
--- a/coreutils/paste.c
+++ b/coreutils/paste.c
@@ -34,27 +34,38 @@
34 34
35static void paste_files(FILE** files, int file_cnt, char* delims, int del_cnt) 35static void paste_files(FILE** files, int file_cnt, char* delims, int del_cnt)
36{ 36{
37 char *line; 37 char **line = xmalloc(file_cnt * sizeof(char *));
38 char delim; 38 char delim;
39 int active_files = file_cnt; 39 int active_files = file_cnt;
40 int i; 40 int i;
41 41
42 while (active_files > 0) { 42 while (active_files > 0) {
43 int del_idx = 0; 43 int del_idx = 0;
44 int got_line = FALSE;
44 45
45 for (i = 0; i < file_cnt; ++i) { 46 for (i = 0; i < file_cnt; ++i) {
46 if (files[i] == NULL) 47 if (files[i]) {
47 continue; 48 line[i] = xmalloc_fgetline(files[i]);
48 49 if (!line[i]) {
49 line = xmalloc_fgetline(files[i]); 50 fclose_if_not_stdin(files[i]);
50 if (!line) { 51 files[i] = NULL;
51 fclose_if_not_stdin(files[i]); 52 --active_files;
52 files[i] = NULL; 53 } else {
53 --active_files; 54 got_line = TRUE;
54 continue; 55 }
56 } else {
57 line[i] = NULL;
58 }
59 }
60
61 if (!got_line)
62 break;
63
64 for (i = 0; i < file_cnt; ++i) {
65 if (line[i]) {
66 fputs_stdout(line[i]);
67 free(line[i]);
55 } 68 }
56 fputs_stdout(line);
57 free(line);
58 delim = '\n'; 69 delim = '\n';
59 if (i != file_cnt - 1) { 70 if (i != file_cnt - 1) {
60 delim = delims[del_idx++]; 71 delim = delims[del_idx++];
@@ -65,6 +76,7 @@ static void paste_files(FILE** files, int file_cnt, char* delims, int del_cnt)
65 fputc(delim, stdout); 76 fputc(delim, stdout);
66 } 77 }
67 } 78 }
79 free(line);
68} 80}
69 81
70static void paste_files_separate(FILE** files, char* delims, int del_cnt) 82static void paste_files_separate(FILE** files, char* delims, int del_cnt)