summaryrefslogtreecommitdiff
path: root/src/split.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/split.c')
-rw-r--r--src/split.c135
1 files changed, 68 insertions, 67 deletions
diff --git a/src/split.c b/src/split.c
index 676aa2e02..2b7a6264b 100644
--- a/src/split.c
+++ b/src/split.c
@@ -29,6 +29,7 @@
#include <sys/wait.h>
#include "system.h"
+#include "die.h"
#include "error.h"
#include "fd-reopen.h"
#include "fcntl--.h"
@@ -203,9 +204,9 @@ set_suffix_length (uintmax_t n_units, enum Split_type split_type)
{
if (suffix_length < suffix_needed)
{
- error (EXIT_FAILURE, 0,
- _("the suffix length needs to be at least %"PRIuMAX),
- suffix_needed);
+ die (EXIT_FAILURE, 0,
+ _("the suffix length needs to be at least %"PRIuMAX),
+ suffix_needed);
}
suffix_auto = false;
return;
@@ -422,7 +423,7 @@ new_name:
char *dir = dir_name (outfile);
long name_max = pathconf (dir, _PC_NAME_MAX);
if (0 <= name_max && name_max < base_len (last_component (outfile)))
- error (EXIT_FAILURE, ENAMETOOLONG, "%s", quotef (outfile));
+ die (EXIT_FAILURE, ENAMETOOLONG, "%s", quotef (outfile));
free (dir);
}
#endif
@@ -443,7 +444,7 @@ new_name:
sufindex[i] = 0;
outfile_mid[i] = suffix_alphabet[sufindex[i]];
}
- error (EXIT_FAILURE, 0, _("output file suffixes exhausted"));
+ die (EXIT_FAILURE, 0, _("output file suffixes exhausted"));
}
}
@@ -462,12 +463,12 @@ create (const char *name)
return fd;
struct stat out_stat_buf;
if (fstat (fd, &out_stat_buf) != 0)
- error (EXIT_FAILURE, errno, _("failed to stat %s"), quoteaf (name));
+ die (EXIT_FAILURE, errno, _("failed to stat %s"), quoteaf (name));
if (SAME_INODE (in_stat_buf, out_stat_buf))
- error (EXIT_FAILURE, 0, _("%s would overwrite input; aborting"),
- quoteaf (name));
+ die (EXIT_FAILURE, 0, _("%s would overwrite input; aborting"),
+ quoteaf (name));
if (ftruncate (fd, 0) != 0)
- error (EXIT_FAILURE, errno, _("%s: error truncating"), quotef (name));
+ die (EXIT_FAILURE, errno, _("%s: error truncating"), quotef (name));
return fd;
}
@@ -479,12 +480,12 @@ create (const char *name)
if (shell_prog == NULL)
shell_prog = "/bin/sh";
if (setenv ("FILE", name, 1) != 0)
- error (EXIT_FAILURE, errno,
- _("failed to set FILE environment variable"));
+ die (EXIT_FAILURE, errno,
+ _("failed to set FILE environment variable"));
if (verbose)
fprintf (stdout, _("executing with FILE=%s\n"), quotef (name));
if (pipe (fd_pair) != 0)
- error (EXIT_FAILURE, errno, _("failed to create pipe"));
+ die (EXIT_FAILURE, errno, _("failed to create pipe"));
child_pid = fork ();
if (child_pid == 0)
{
@@ -498,26 +499,26 @@ create (const char *name)
reading an EOF on the corresponding read-pipe. */
for (j = 0; j < n_open_pipes; ++j)
if (close (open_pipes[j]) != 0)
- error (EXIT_FAILURE, errno, _("closing prior pipe"));
+ die (EXIT_FAILURE, errno, _("closing prior pipe"));
if (close (fd_pair[1]))
- error (EXIT_FAILURE, errno, _("closing output pipe"));
+ die (EXIT_FAILURE, errno, _("closing output pipe"));
if (fd_pair[0] != STDIN_FILENO)
{
if (dup2 (fd_pair[0], STDIN_FILENO) != STDIN_FILENO)
- error (EXIT_FAILURE, errno, _("moving input pipe"));
+ die (EXIT_FAILURE, errno, _("moving input pipe"));
if (close (fd_pair[0]) != 0)
- error (EXIT_FAILURE, errno, _("closing input pipe"));
+ die (EXIT_FAILURE, errno, _("closing input pipe"));
}
sigprocmask (SIG_SETMASK, &oldblocked, NULL);
execl (shell_prog, last_component (shell_prog), "-c",
filter_command, (char *) NULL);
- error (EXIT_FAILURE, errno, _("failed to run command: \"%s -c %s\""),
- shell_prog, filter_command);
+ die (EXIT_FAILURE, errno, _("failed to run command: \"%s -c %s\""),
+ shell_prog, filter_command);
}
if (child_pid == -1)
- error (EXIT_FAILURE, errno, _("fork system call failed"));
+ die (EXIT_FAILURE, errno, _("fork system call failed"));
if (close (fd_pair[0]) != 0)
- error (EXIT_FAILURE, errno, _("failed to close input pipe"));
+ die (EXIT_FAILURE, errno, _("failed to close input pipe"));
filter_pid = child_pid;
if (n_open_pipes == open_pipes_alloc)
open_pipes = x2nrealloc (open_pipes, &open_pipes_alloc,
@@ -534,11 +535,11 @@ static void
closeout (FILE *fp, int fd, pid_t pid, char const *name)
{
if (fp != NULL && fclose (fp) != 0 && ! ignorable (errno))
- error (EXIT_FAILURE, errno, "%s", quotef (name));
+ die (EXIT_FAILURE, errno, "%s", quotef (name));
if (fd >= 0)
{
if (fp == NULL && close (fd) < 0)
- error (EXIT_FAILURE, errno, "%s", quotef (name));
+ die (EXIT_FAILURE, errno, "%s", quotef (name));
int j;
for (j = 0; j < n_open_pipes; ++j)
{
@@ -553,7 +554,7 @@ closeout (FILE *fp, int fd, pid_t pid, char const *name)
{
int wstatus = 0;
if (waitpid (pid, &wstatus, 0) == -1 && errno != ECHILD)
- error (EXIT_FAILURE, errno, _("waiting for child process"));
+ die (EXIT_FAILURE, errno, _("waiting for child process"));
if (WIFSIGNALED (wstatus))
{
int sig = WTERMSIG (wstatus);
@@ -577,8 +578,8 @@ closeout (FILE *fp, int fd, pid_t pid, char const *name)
else
{
/* shouldn't happen. */
- error (EXIT_FAILURE, 0,
- _("unknown status from command (0x%X)"), wstatus + 0u);
+ die (EXIT_FAILURE, 0,
+ _("unknown status from command (0x%X)"), wstatus + 0u);
}
}
}
@@ -599,7 +600,7 @@ cwrite (bool new_file_flag, const char *bp, size_t bytes)
next_file_name ();
output_desc = create (outfile);
if (output_desc < 0)
- error (EXIT_FAILURE, errno, "%s", quotef (outfile));
+ die (EXIT_FAILURE, errno, "%s", quotef (outfile));
}
if (full_write (output_desc, bp, bytes) == bytes)
@@ -607,7 +608,7 @@ cwrite (bool new_file_flag, const char *bp, size_t bytes)
else
{
if (! ignorable (errno))
- error (EXIT_FAILURE, errno, "%s", quotef (outfile));
+ die (EXIT_FAILURE, errno, "%s", quotef (outfile));
return false;
}
}
@@ -638,7 +639,7 @@ bytes_split (uintmax_t n_bytes, char *buf, size_t bufsize, size_t initial_read,
{
n_read = safe_read (STDIN_FILENO, buf, bufsize);
if (n_read == SAFE_READ_ERROR)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
eof = n_read == 0;
}
char *bp_out = buf;
@@ -697,7 +698,7 @@ lines_split (uintmax_t n_lines, char *buf, size_t bufsize)
{
n_read = safe_read (STDIN_FILENO, buf, bufsize);
if (n_read == SAFE_READ_ERROR)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
bp = bp_out = buf;
eob = bp + n_read;
*eob = eolchar;
@@ -746,7 +747,7 @@ line_bytes_split (uintmax_t n_bytes, char *buf, size_t bufsize)
{
n_read = safe_read (STDIN_FILENO, buf, bufsize);
if (n_read == SAFE_READ_ERROR)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
size_t n_left = n_read;
char *sob = buf;
while (n_left)
@@ -871,7 +872,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
else
{
if (lseek (STDIN_FILENO, start - initial_read, SEEK_CUR) < 0)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
initial_read = SIZE_MAX;
}
n_written = start;
@@ -892,7 +893,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
{
n_read = safe_read (STDIN_FILENO, buf, bufsize);
if (n_read == SAFE_READ_ERROR)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
}
if (n_read == 0)
break; /* eof. */
@@ -920,7 +921,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
large chunks from an existing file, so it's more efficient
to write out directly. */
if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
- error (EXIT_FAILURE, errno, "%s", _("write error"));
+ die (EXIT_FAILURE, errno, "%s", _("write error"));
}
else if (! k)
cwrite (new_file_flag, bp, to_write);
@@ -989,7 +990,7 @@ bytes_chunk_extract (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
else
{
if (lseek (STDIN_FILENO, start, SEEK_CUR) < 0)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
initial_read = SIZE_MAX;
}
@@ -1005,14 +1006,14 @@ bytes_chunk_extract (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
{
n_read = safe_read (STDIN_FILENO, buf, bufsize);
if (n_read == SAFE_READ_ERROR)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
}
if (n_read == 0)
break; /* eof. */
n_read = MIN (n_read, end - start);
if (full_write (STDOUT_FILENO, buf, n_read) != n_read
&& ! ignorable (errno))
- error (EXIT_FAILURE, errno, "%s", quotef ("-"));
+ die (EXIT_FAILURE, errno, "%s", quotef ("-"));
start += n_read;
}
}
@@ -1077,7 +1078,7 @@ ofile_open (of_t *files, size_t i_check, size_t nfiles)
break;
if (!(errno == EMFILE || errno == ENFILE))
- error (EXIT_FAILURE, errno, "%s", quotef (files[i_check].of_name));
+ die (EXIT_FAILURE, errno, "%s", quotef (files[i_check].of_name));
file_limit = true;
@@ -1087,19 +1088,19 @@ ofile_open (of_t *files, size_t i_check, size_t nfiles)
i_reopen = i_reopen ? i_reopen - 1 : nfiles - 1;
/* No more open files to close, exit with E[NM]FILE. */
if (i_reopen == i_check)
- error (EXIT_FAILURE, errno, "%s",
- quotef (files[i_check].of_name));
+ die (EXIT_FAILURE, errno, "%s",
+ quotef (files[i_check].of_name));
}
if (fclose (files[i_reopen].ofile) != 0)
- error (EXIT_FAILURE, errno, "%s", quotef (files[i_reopen].of_name));
+ die (EXIT_FAILURE, errno, "%s", quotef (files[i_reopen].of_name));
files[i_reopen].ofile = NULL;
files[i_reopen].ofd = OFD_APPEND;
}
files[i_check].ofd = fd;
if (!(files[i_check].ofile = fdopen (fd, "a")))
- error (EXIT_FAILURE, errno, "%s", quotef (files[i_check].of_name));
+ die (EXIT_FAILURE, errno, "%s", quotef (files[i_check].of_name));
files[i_check].opid = filter_pid;
filter_pid = 0;
}
@@ -1148,7 +1149,7 @@ lines_rr (uintmax_t k, uintmax_t n, char *buf, size_t bufsize)
char *bp = buf, *eob;
size_t n_read = safe_read (STDIN_FILENO, buf, bufsize);
if (n_read == SAFE_READ_ERROR)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
else if (n_read == 0)
break; /* eof. */
eob = buf + n_read;
@@ -1174,12 +1175,12 @@ lines_rr (uintmax_t k, uintmax_t n, char *buf, size_t bufsize)
if (line_no == k && unbuffered)
{
if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
- error (EXIT_FAILURE, errno, "%s", _("write error"));
+ die (EXIT_FAILURE, errno, "%s", _("write error"));
}
else if (line_no == k && fwrite (bp, to_write, 1, stdout) != 1)
{
clearerr (stdout); /* To silence close_stdout(). */
- error (EXIT_FAILURE, errno, "%s", _("write error"));
+ die (EXIT_FAILURE, errno, "%s", _("write error"));
}
if (next)
line_no = (line_no == n) ? 1 : line_no + 1;
@@ -1195,15 +1196,15 @@ lines_rr (uintmax_t k, uintmax_t n, char *buf, size_t bufsize)
if (full_write (files[i_file].ofd, bp, to_write) != to_write
&& ! ignorable (errno))
{
- error (EXIT_FAILURE, errno, "%s",
- quotef (files[i_file].of_name));
+ die (EXIT_FAILURE, errno, "%s",
+ quotef (files[i_file].of_name));
}
}
else if (fwrite (bp, to_write, 1, files[i_file].ofile) != 1
&& ! ignorable (errno))
{
- error (EXIT_FAILURE, errno, "%s",
- quotef (files[i_file].of_name));
+ die (EXIT_FAILURE, errno, "%s",
+ quotef (files[i_file].of_name));
}
if (! ignorable (errno))
@@ -1213,8 +1214,8 @@ lines_rr (uintmax_t k, uintmax_t n, char *buf, size_t bufsize)
{
if (fclose (files[i_file].ofile) != 0)
{
- error (EXIT_FAILURE, errno, "%s",
- quotef (files[i_file].of_name));
+ die (EXIT_FAILURE, errno, "%s",
+ quotef (files[i_file].of_name));
}
files[i_file].ofile = NULL;
files[i_file].ofd = OFD_APPEND;
@@ -1396,7 +1397,7 @@ main (int argc, char **argv)
{
char neweol = optarg[0];
if (! neweol)
- error (EXIT_FAILURE, 0, _("empty record separator"));
+ die (EXIT_FAILURE, 0, _("empty record separator"));
if (optarg[1])
{
if (STREQ (optarg, "\\0"))
@@ -1407,15 +1408,15 @@ main (int argc, char **argv)
"multi-character tab" instead of "multibyte tab", so
that the diagnostic's wording does not need to be
changed once multibyte characters are supported. */
- error (EXIT_FAILURE, 0, _("multi-character separator %s"),
- quote (optarg));
+ die (EXIT_FAILURE, 0, _("multi-character separator %s"),
+ quote (optarg));
}
}
/* Make it explicit we don't support multiple separators. */
if (0 <= eolchar && neweol != eolchar)
{
- error (EXIT_FAILURE, 0,
- _("multiple separator characters specified"));
+ die (EXIT_FAILURE, 0,
+ _("multiple separator characters specified"));
}
eolchar = neweol;
@@ -1445,9 +1446,9 @@ main (int argc, char **argv)
if (!DECIMAL_DIGIT_ACCUMULATE (n_units, c - '0', uintmax_t))
{
char buffer[INT_BUFSIZE_BOUND (uintmax_t)];
- error (EXIT_FAILURE, 0,
- _("line count option -%s%c... is too large"),
- umaxtostr (n_units, buffer), c);
+ die (EXIT_FAILURE, 0,
+ _("line count option -%s%c... is too large"),
+ umaxtostr (n_units, buffer), c);
}
break;
@@ -1548,8 +1549,8 @@ main (int argc, char **argv)
/* Open the input file. */
if (! STREQ (infile, "-")
&& fd_reopen (STDIN_FILENO, infile, O_RDONLY, 0) < 0)
- error (EXIT_FAILURE, errno, _("cannot open %s for reading"),
- quoteaf (infile));
+ die (EXIT_FAILURE, errno, _("cannot open %s for reading"),
+ quoteaf (infile));
/* Binary I/O is safer when byte counts are used. */
if (O_BINARY && ! isatty (STDIN_FILENO))
@@ -1558,7 +1559,7 @@ main (int argc, char **argv)
/* Get the optimal block size of input device and make a buffer. */
if (fstat (STDIN_FILENO, &in_stat_buf) != 0)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
bool specified_buf_size = !! in_blk_size;
if (! specified_buf_size)
@@ -1573,16 +1574,16 @@ main (int argc, char **argv)
file_size = input_file_size (STDIN_FILENO, &in_stat_buf,
buf, in_blk_size);
if (file_size < 0)
- error (EXIT_FAILURE, errno, _("%s: cannot determine file size"),
- quotef (infile));
+ die (EXIT_FAILURE, errno, _("%s: cannot determine file size"),
+ quotef (infile));
initial_read = MIN (file_size, in_blk_size);
/* Overflow, and sanity checking. */
if (OFF_T_MAX < n_units)
{
char buffer[INT_BUFSIZE_BOUND (uintmax_t)];
- error (EXIT_FAILURE, EOVERFLOW, "%s: %s",
- _("invalid number of chunks"),
- quote (umaxtostr (n_units, buffer)));
+ die (EXIT_FAILURE, EOVERFLOW, "%s: %s",
+ _("invalid number of chunks"),
+ quote (umaxtostr (n_units, buffer)));
}
/* increase file_size to n_units here, so that we still process
any input data, and create empty files for the rest. */
@@ -1643,7 +1644,7 @@ main (int argc, char **argv)
IF_LINT (free (b));
if (close (STDIN_FILENO) != 0)
- error (EXIT_FAILURE, errno, "%s", quotef (infile));
+ die (EXIT_FAILURE, errno, "%s", quotef (infile));
closeout (NULL, output_desc, filter_pid, outfile);
return EXIT_SUCCESS;