src/tac.c - coreutils-8.23
Global variables defined
Functions defined
Macros defined
Source code
#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include "system.h"
#include <regex.h>
#include "error.h"
#include "filenamecat.h"
#include "quote.h"
#include "quotearg.h"
#include "safe-read.h"
#include "stdlib--.h"
#include "xfreopen.h"
#define PROGRAM_NAME "tac"
#define AUTHORS \
proper_name ("Jay Lepreau"), \
proper_name ("David MacKenzie")
#if defined __MSDOS__ || defined _WIN32
# define DONT_UNLINK_WHILE_OPEN 1
#endif
#ifndef DEFAULT_TMPDIR
# define DEFAULT_TMPDIR "/tmp"
#endif
#define INITIAL_READSIZE 8192
#define WRITESIZE 8192
static char const *separator;
static bool have_read_stdin = false;
static bool separator_ends_record;
static size_t sentinel_length;
static size_t match_length;
static char *G_buffer;
static size_t read_size;
static size_t G_buffer_size;
static struct re_pattern_buffer compiled_separator;
static char compiled_separator_fastmap[UCHAR_MAX + 1];
static struct re_registers regs;
static struct option const longopts[] =
{
{"before", no_argument, NULL, 'b'},
{"regex", no_argument, NULL, 'r'},
{"separator", required_argument, NULL, 's'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
void
usage (int status)
{
if (status != EXIT_SUCCESS)
emit_try_help ();
else
{
printf (_("\
Usage: %s [OPTION]... [FILE]...\n\
"),
program_name);
fputs (_("\
Write each FILE to standard output, last line first.\n\
With no FILE, or when FILE is -, read standard input.\n\
"), stdout);
emit_mandatory_arg_note ();
fputs (_("\
-b, --before attach the separator before instead of after\n\
-r, --regex interpret the separator as a regular expression\n\
-s, --separator=STRING use STRING as the separator instead of newline\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
emit_ancillary_info ();
}
exit (status);
}
static void
output (const char *start, const char *past_end)
{
static char buffer[WRITESIZE];
static size_t bytes_in_buffer = 0;
size_t bytes_to_add = past_end - start;
size_t bytes_available = WRITESIZE - bytes_in_buffer;
if (start == 0)
{
fwrite (buffer, 1, bytes_in_buffer, stdout);
bytes_in_buffer = 0;
return;
}
while (bytes_to_add >= bytes_available)
{
memcpy (buffer + bytes_in_buffer, start, bytes_available);
bytes_to_add -= bytes_available;
start += bytes_available;
fwrite (buffer, 1, WRITESIZE, stdout);
bytes_in_buffer = 0;
bytes_available = WRITESIZE;
}
memcpy (buffer + bytes_in_buffer, start, bytes_to_add);
bytes_in_buffer += bytes_to_add;
}
static bool
tac_seekable (int input_fd, const char *file)
{
char *match_start;
char *past_end;
size_t saved_record_size;
off_t file_pos;
bool first_time = true;
char first_char = *separator; char const *separator1 = separator + 1; size_t match_length1 = match_length - 1;
file_pos = lseek (input_fd, 0, SEEK_END);
if (file_pos < 1)
return true;
saved_record_size = file_pos % read_size;
if (saved_record_size == 0)
saved_record_size = read_size;
file_pos -= saved_record_size;
if (lseek (input_fd, file_pos, SEEK_SET) < 0)
error (0, errno, _("%s: seek failed"), quotearg_colon (file));
if (safe_read (input_fd, G_buffer, saved_record_size) != saved_record_size)
{
error (0, errno, _("%s: read error"), quotearg_colon (file));
return false;
}
match_start = past_end = G_buffer + saved_record_size;
if (sentinel_length)
match_start -= match_length1;
while (true)
{
if (sentinel_length == 0)
{
size_t i = match_start - G_buffer;
regoff_t ri = i;
regoff_t range = 1 - ri;
regoff_t ret;
if (1 < range)
error (EXIT_FAILURE, 0, _("record too large"));
if (range == 1
|| ((ret = re_search (&compiled_separator, G_buffer,
i, i - 1, range, ®s))
== -1))
match_start = G_buffer - 1;
else if (ret == -2)
{
error (EXIT_FAILURE, 0,
_("error in regular expression search"));
}
else
{
match_start = G_buffer + regs.start[0];
match_length = regs.end[0] - regs.start[0];
}
}
else
{
while (*--match_start != first_char
|| (match_length1 && strncmp (match_start + 1, separator1,
match_length1)))
;
}
if (match_start < G_buffer)
{
if (file_pos == 0)
{
output (G_buffer, past_end);
return true;
}
saved_record_size = past_end - G_buffer;
if (saved_record_size > read_size)
{
char *newbuffer;
size_t offset = sentinel_length ? sentinel_length : 1;
ptrdiff_t match_start_offset = match_start - G_buffer;
ptrdiff_t past_end_offset = past_end - G_buffer;
size_t old_G_buffer_size = G_buffer_size;
read_size *= 2;
G_buffer_size = read_size * 2 + sentinel_length + 2;
if (G_buffer_size < old_G_buffer_size)
xalloc_die ();
newbuffer = xrealloc (G_buffer - offset, G_buffer_size);
newbuffer += offset;
match_start = newbuffer + match_start_offset;
past_end = newbuffer + past_end_offset;
G_buffer = newbuffer;
}
if (file_pos >= read_size)
file_pos -= read_size;
else
{
read_size = file_pos;
file_pos = 0;
}
if (lseek (input_fd, file_pos, SEEK_SET) < 0)
error (0, errno, _("%s: seek failed"), quotearg_colon (file));
memmove (G_buffer + read_size, G_buffer, saved_record_size);
past_end = G_buffer + read_size + saved_record_size;
if (sentinel_length)
match_start = G_buffer + read_size;
else
match_start = past_end;
if (safe_read (input_fd, G_buffer, read_size) != read_size)
{
error (0, errno, _("%s: read error"), quotearg_colon (file));
return false;
}
}
else
{
if (separator_ends_record)
{
char *match_end = match_start + match_length;
if (!first_time || match_end != past_end)
output (match_end, past_end);
past_end = match_end;
first_time = false;
}
else
{
output (match_start, past_end);
past_end = match_start;
}
if (sentinel_length > 0)
match_start -= match_length - 1;
}
}
}
#if DONT_UNLINK_WHILE_OPEN
FIXME
static const char *file_to_remove;
static FILE *fp_to_close;
static void
unlink_tempfile (void)
{
fclose (fp_to_close);
unlink (file_to_remove);
}
static void
record_or_unlink_tempfile (char const *fn, FILE *fp)
{
if (!file_to_remove)
{
file_to_remove = fn;
fp_to_close = fp;
atexit (unlink_tempfile);
}
}
#else
static void
record_or_unlink_tempfile (char const *fn, FILE *fp _GL_UNUSED)
{
unlink (fn);
}
#endif
static bool
temp_stream (FILE **fp, char **file_name)
{
static char *tempfile = NULL;
static FILE *tmp_fp;
if (tempfile == NULL)
{
char const *t = getenv ("TMPDIR");
char const *tempdir = t ? t : DEFAULT_TMPDIR;
tempfile = mfile_name_concat (tempdir, "tacXXXXXX", NULL);
if (tempdir == NULL)
{
error (0, 0, _("memory exhausted"));
return false;
}
FIXMEFIXME
int fd = mkstemp (tempfile);
if (fd < 0)
{
error (0, errno, _("failed to create temporary file in %s"),
quote (tempdir));
goto Reset;
}
tmp_fp = fdopen (fd, (O_BINARY ? "w+b" : "w+"));
if (! tmp_fp)
{
error (0, errno, _("failed to open %s for writing"),
quote (tempfile));
close (fd);
unlink (tempfile);
Reset:
free (tempfile);
tempfile = NULL;
return false;
}
record_or_unlink_tempfile (tempfile, tmp_fp);
}
else
{
if (fseeko (tmp_fp, 0, SEEK_SET) < 0
|| ftruncate (fileno (tmp_fp), 0) < 0)
{
error (0, errno, _("failed to rewind stream for %s"),
quote (tempfile));
return false;
}
}
*fp = tmp_fp;
*file_name = tempfile;
return true;
}
static bool
copy_to_temp (FILE **g_tmp, char **g_tempfile, int input_fd, char const *file)
{
FILE *fp;
char *file_name;
if (!temp_stream (&fp, &file_name))
return false;
while (1)
{
size_t bytes_read = safe_read (input_fd, G_buffer, read_size);
if (bytes_read == 0)
break;
if (bytes_read == SAFE_READ_ERROR)
{
error (0, errno, _("%s: read error"), quotearg_colon (file));
goto Fail;
}
if (fwrite (G_buffer, 1, bytes_read, fp) != bytes_read)
{
error (0, errno, _("%s: write error"), quotearg_colon (file_name));
goto Fail;
}
}
if (fflush (fp) != 0)
{
error (0, errno, _("%s: write error"), quotearg_colon (file_name));
goto Fail;
}
*g_tmp = fp;
*g_tempfile = file_name;
return true;
Fail:
fclose (fp);
return false;
}
static bool
tac_nonseekable (int input_fd, const char *file)
{
FILE *tmp_stream;
char *tmp_file;
if (!copy_to_temp (&tmp_stream, &tmp_file, input_fd, file))
return false;
bool ok = tac_seekable (fileno (tmp_stream), tmp_file);
return ok;
}
static bool
tac_file (const char *filename)
{
bool ok;
off_t file_size;
int fd;
bool is_stdin = STREQ (filename, "-");
if (is_stdin)
{
have_read_stdin = true;
fd = STDIN_FILENO;
filename = _("standard input");
if (O_BINARY && ! isatty (STDIN_FILENO))
xfreopen (NULL, "rb", stdin);
}
else
{
fd = open (filename, O_RDONLY | O_BINARY);
if (fd < 0)
{
error (0, errno, _("failed to open %s for reading"),
quote (filename));
return false;
}
}
file_size = lseek (fd, 0, SEEK_END);
ok = (file_size < 0 || isatty (fd)
? tac_nonseekable (fd, filename)
: tac_seekable (fd, filename));
if (!is_stdin && close (fd) != 0)
{
error (0, errno, _("%s: read error"), quotearg_colon (filename));
ok = false;
}
return ok;
}
int
main (int argc, char **argv)
{
const char *error_message; int optc;
bool ok;
size_t half_buffer_size;
static char const *const default_file_list[] = {"-", NULL};
char const *const *file;
initialize_main (&argc, &argv);
set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
separator = "\n";
sentinel_length = 1;
separator_ends_record = true;
while ((optc = getopt_long (argc, argv, "brs:", longopts, NULL)) != -1)
{
switch (optc)
{
case 'b':
separator_ends_record = false;
break;
case 'r':
sentinel_length = 0;
break;
case 's':
separator = optarg;
if (*separator == 0)
error (EXIT_FAILURE, 0, _("separator cannot be empty"));
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
}
}
if (sentinel_length == 0)
{
compiled_separator.buffer = NULL;
compiled_separator.allocated = 0;
compiled_separator.fastmap = compiled_separator_fastmap;
compiled_separator.translate = NULL;
error_message = re_compile_pattern (separator, strlen (separator),
&compiled_separator);
if (error_message)
error (EXIT_FAILURE, 0, "%s", error_message);
}
else
match_length = sentinel_length = strlen (separator);
read_size = INITIAL_READSIZE;
while (sentinel_length >= read_size / 2)
{
if (SIZE_MAX / 2 < read_size)
xalloc_die ();
read_size *= 2;
}
half_buffer_size = read_size + sentinel_length + 1;
G_buffer_size = 2 * half_buffer_size;
if (! (read_size < half_buffer_size && half_buffer_size < G_buffer_size))
xalloc_die ();
G_buffer = xmalloc (G_buffer_size);
if (sentinel_length)
{
memcpy (G_buffer, separator, sentinel_length + 1);
G_buffer += sentinel_length;
}
else
{
++G_buffer;
}
file = (optind < argc
? (char const *const *) &argv[optind]
: default_file_list);
if (O_BINARY && ! isatty (STDOUT_FILENO))
xfreopen (NULL, "wb", stdout);
{
size_t i;
ok = true;
for (i = 0; file[i]; ++i)
ok &= tac_file (file[i]);
}
output ((char *) NULL, (char *) NULL);
if (have_read_stdin && close (STDIN_FILENO) < 0)
{
error (0, errno, "-");
ok = false;
}
#ifdef lint
size_t offset = sentinel_length ? sentinel_length : 1;
free (G_buffer - offset);
#endif
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
}