ls: fix spurious output with -Z
[coreutils.git] / src / tac-pipe.c
blobd36b785fbbf1380c2b1e81ee222fd386d35266f5
1 /* tac from a pipe.
3 Copyright (C) 1997-2024 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* FIXME */
19 #include "assure.h"
21 /* FIXME: this is small for testing */
22 #define BUFFER_SIZE (8)
24 #define LEN(X, I) ((X)->p[(I)].one_past_end - (X)->p[(I)].start)
25 #define EMPTY(X) ((X)->n_bufs == 1 && LEN (X, 0) == 0)
27 #define ONE_PAST_END(X, I) ((X)->p[(I)].one_past_end)
29 struct Line_ptr
31 size_t i;
32 char *ptr;
34 typedef struct Line_ptr Line_ptr;
36 struct B_pair
38 char *start;
39 char *one_past_end;
42 struct Buf
44 size_t n_bufs;
45 struct obstack obs;
46 struct B_pair *p;
48 typedef struct Buf Buf;
50 static bool
51 buf_init_from_stdin (Buf *x, char eol_byte)
53 bool last_byte_is_eol_byte = true;
54 bool ok = true;
56 #define OBS (&(x->obs))
57 obstack_init (OBS);
59 while (true)
61 char *buf = malloc (BUFFER_SIZE);
63 if (buf == nullptr)
65 /* Fall back on the code that relies on a temporary file.
66 Write all buffers to that file and free them. */
67 /* FIXME */
68 ok = false;
69 break;
71 idx_t bytes_read = full_read (STDIN_FILENO, buf, BUFFER_SIZE);
72 if (bytes_read != BUFFER_SIZE && errno != 0)
73 error (EXIT_FAILURE, errno, _("read error"));
76 struct B_pair bp;
77 bp.start = buf;
78 bp.one_past_end = buf + bytes_read;
79 obstack_grow (OBS, &bp, sizeof (bp));
82 if (bytes_read != 0)
83 last_byte_is_eol_byte = (buf[bytes_read - 1] == eol_byte);
85 if (bytes_read < BUFFER_SIZE)
86 break;
89 if (ok)
91 /* If the file was non-empty and lacked an EOL_BYTE at its end,
92 then add a buffer containing just that one byte. */
93 if (!last_byte_is_eol_byte)
95 char *buf = malloc (1);
96 if (buf == nullptr)
98 /* FIXME: just like above */
99 ok = false;
101 else
103 struct B_pair bp;
104 *buf = eol_byte;
105 bp.start = buf;
106 bp.one_past_end = buf + 1;
107 obstack_grow (OBS, &bp, sizeof (bp));
112 x->n_bufs = obstack_object_size (OBS) / sizeof (x->p[0]);
113 x->p = (struct B_pair *) obstack_finish (OBS);
115 /* If there are two or more buffers and the last buffer is empty,
116 then free the last one and decrement the buffer count. */
117 if (x->n_bufs >= 2
118 && x->p[x->n_bufs - 1].start == x->p[x->n_bufs - 1].one_past_end)
119 free (x->p[--(x->n_bufs)].start);
121 return ok;
124 static void
125 buf_free (Buf *x)
127 for (size_t i = 0; i < x->n_bufs; i++)
128 free (x->p[i].start);
129 obstack_free (OBS, nullptr);
132 Line_ptr
133 line_ptr_decrement (const Buf *x, const Line_ptr *lp)
135 Line_ptr lp_new;
137 if (lp->ptr > x->p[lp->i].start)
139 lp_new.i = lp->i;
140 lp_new.ptr = lp->ptr - 1;
142 else
144 affirm (lp->i > 0);
145 lp_new.i = lp->i - 1;
146 lp_new.ptr = ONE_PAST_END (x, lp->i - 1) - 1;
148 return lp_new;
151 Line_ptr
152 line_ptr_increment (const Buf *x, const Line_ptr *lp)
154 Line_ptr lp_new;
156 affirm (lp->ptr <= ONE_PAST_END (x, lp->i) - 1);
157 if (lp->ptr < ONE_PAST_END (x, lp->i) - 1)
159 lp_new.i = lp->i;
160 lp_new.ptr = lp->ptr + 1;
162 else
164 affirm (lp->i < x->n_bufs - 1);
165 lp_new.i = lp->i + 1;
166 lp_new.ptr = x->p[lp->i + 1].start;
168 return lp_new;
171 static bool
172 find_bol (const Buf *x,
173 const Line_ptr *last_bol, Line_ptr *new_bol, char eol_byte)
175 size_t i;
176 Line_ptr tmp;
177 char *last_bol_ptr;
179 if (last_bol->ptr == x->p[0].start)
180 return false;
182 tmp = line_ptr_decrement (x, last_bol);
183 last_bol_ptr = tmp.ptr;
184 i = tmp.i;
185 while (true)
187 char *nl = memrchr (x->p[i].start, last_bol_ptr, eol_byte);
188 if (nl)
190 Line_ptr nl_pos;
191 nl_pos.i = i;
192 nl_pos.ptr = nl;
193 *new_bol = line_ptr_increment (x, &nl_pos);
194 return true;
197 if (i == 0)
198 break;
200 --i;
201 last_bol_ptr = ONE_PAST_END (x, i);
204 /* If last_bol->ptr didn't point at the first byte of X, then reaching
205 this point means that we're about to return the line that is at the
206 beginning of X. */
207 if (last_bol->ptr != x->p[0].start)
209 new_bol->i = 0;
210 new_bol->ptr = x->p[0].start;
211 return true;
214 return false;
217 static void
218 print_line (FILE *out_stream, const Buf *x,
219 const Line_ptr *bol, const Line_ptr *bol_next)
221 for (size_t i = bol->i; i <= bol_next->i; i++)
223 char *a = (i == bol->i ? bol->ptr : x->p[i].start);
224 char *b = (i == bol_next->i ? bol_next->ptr : ONE_PAST_END (x, i));
225 fwrite (a, 1, b - a, out_stream);
229 static bool
230 tac_mem ()
232 Buf x;
233 Line_ptr bol;
234 char eol_byte = '\n';
236 if (! buf_init_from_stdin (&x, eol_byte))
238 buf_free (&x);
239 return false;
242 /* Special case the empty file. */
243 if (EMPTY (&x))
244 return true;
246 /* Initially, point at one past the last byte of the file. */
247 bol.i = x.n_bufs - 1;
248 bol.ptr = ONE_PAST_END (&x, bol.i);
250 while (true)
252 Line_ptr new_bol;
253 if (! find_bol (&x, &bol, &new_bol, eol_byte))
254 break;
255 print_line (stdout, &x, &new_bol, &bol);
256 bol = new_bol;
258 return true;