maint: update all copyright year number ranges
[platform/upstream/coreutils.git] / src / split.c
1 /* split.c -- split a file into pieces.
2    Copyright (C) 1988-2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16 \f
17 /* By tege@sics.se, with rms.
18
19    To do:
20    * Implement -t CHAR or -t REGEX to specify break characters other
21      than newline. */
22
23 #include <config.h>
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <signal.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31
32 #include "system.h"
33 #include "error.h"
34 #include "fd-reopen.h"
35 #include "fcntl--.h"
36 #include "full-read.h"
37 #include "full-write.h"
38 #include "ioblksize.h"
39 #include "quote.h"
40 #include "safe-read.h"
41 #include "sig2str.h"
42 #include "xfreopen.h"
43 #include "xstrtol.h"
44
45 /* The official name of this program (e.g., no 'g' prefix).  */
46 #define PROGRAM_NAME "split"
47
48 #define AUTHORS \
49   proper_name_utf8 ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \
50   proper_name ("Richard M. Stallman")
51
52 /* Shell command to filter through, instead of creating files.  */
53 static char const *filter_command;
54
55 /* Process ID of the filter.  */
56 static int filter_pid;
57
58 /* Array of open pipes.  */
59 static int *open_pipes;
60 static size_t open_pipes_alloc;
61 static size_t n_open_pipes;
62
63 /* Blocked signals.  */
64 static sigset_t oldblocked;
65 static sigset_t newblocked;
66
67 /* Base name of output files.  */
68 static char const *outbase;
69
70 /* Name of output files.  */
71 static char *outfile;
72
73 /* Pointer to the end of the prefix in OUTFILE.
74    Suffixes are inserted here.  */
75 static char *outfile_mid;
76
77 /* Generate new suffix when suffixes are exhausted.  */
78 static bool suffix_auto = true;
79
80 /* Length of OUTFILE's suffix.  */
81 static size_t suffix_length;
82
83 /* Alphabet of characters to use in suffix.  */
84 static char const *suffix_alphabet = "abcdefghijklmnopqrstuvwxyz";
85
86 /* Numerical suffix start value.  */
87 static const char *numeric_suffix_start;
88
89 /* Additional suffix to append to output file names.  */
90 static char const *additional_suffix;
91
92 /* Name of input file.  May be "-".  */
93 static char *infile;
94
95 /* stat buf for input file.  */
96 static struct stat in_stat_buf;
97
98 /* Descriptor on which output file is open.  */
99 static int output_desc = -1;
100
101 /* If true, print a diagnostic on standard error just before each
102    output file is opened. */
103 static bool verbose;
104
105 /* If true, don't generate zero length output files. */
106 static bool elide_empty_files;
107
108 /* If true, in round robin mode, immediately copy
109    input to output, which is much slower, so disabled by default.  */
110 static bool unbuffered;
111
112 /* The split mode to use.  */
113 enum Split_type
114 {
115   type_undef, type_bytes, type_byteslines, type_lines, type_digits,
116   type_chunk_bytes, type_chunk_lines, type_rr
117 };
118
119 /* For long options that have no equivalent short option, use a
120    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
121 enum
122 {
123   VERBOSE_OPTION = CHAR_MAX + 1,
124   FILTER_OPTION,
125   IO_BLKSIZE_OPTION,
126   ADDITIONAL_SUFFIX_OPTION
127 };
128
129 static struct option const longopts[] =
130 {
131   {"bytes", required_argument, NULL, 'b'},
132   {"lines", required_argument, NULL, 'l'},
133   {"line-bytes", required_argument, NULL, 'C'},
134   {"number", required_argument, NULL, 'n'},
135   {"elide-empty-files", no_argument, NULL, 'e'},
136   {"unbuffered", no_argument, NULL, 'u'},
137   {"suffix-length", required_argument, NULL, 'a'},
138   {"additional-suffix", required_argument, NULL,
139    ADDITIONAL_SUFFIX_OPTION},
140   {"numeric-suffixes", optional_argument, NULL, 'd'},
141   {"filter", required_argument, NULL, FILTER_OPTION},
142   {"verbose", no_argument, NULL, VERBOSE_OPTION},
143   {"-io-blksize", required_argument, NULL,
144    IO_BLKSIZE_OPTION}, /* do not document */
145   {GETOPT_HELP_OPTION_DECL},
146   {GETOPT_VERSION_OPTION_DECL},
147   {NULL, 0, NULL, 0}
148 };
149
150 /* Return true if the errno value, ERR, is ignorable.  */
151 static inline bool
152 ignorable (int err)
153 {
154   return filter_command && err == EPIPE;
155 }
156
157 static void
158 set_suffix_length (uintmax_t n_units, enum Split_type split_type)
159 {
160 #define DEFAULT_SUFFIX_LENGTH 2
161
162   size_t suffix_needed = 0;
163
164   /* The suffix auto length feature is incompatible with
165      a user specified start value as the generated suffixes
166      are not all consecutive.  */
167   if (numeric_suffix_start)
168     suffix_auto = false;
169
170   /* Auto-calculate the suffix length if the number of files is given.  */
171   if (split_type == type_chunk_bytes || split_type == type_chunk_lines
172       || split_type == type_rr)
173     {
174       size_t alphabet_len = strlen (suffix_alphabet);
175       bool alphabet_slop = (n_units % alphabet_len) != 0;
176       while (n_units /= alphabet_len)
177         suffix_needed++;
178       suffix_needed += alphabet_slop;
179       suffix_auto = false;
180     }
181
182   if (suffix_length)            /* set by user */
183     {
184       if (suffix_length < suffix_needed)
185         {
186           error (EXIT_FAILURE, 0,
187                  _("the suffix length needs to be at least %zu"),
188                  suffix_needed);
189         }
190       suffix_auto = false;
191       return;
192     }
193   else
194     suffix_length = MAX (DEFAULT_SUFFIX_LENGTH, suffix_needed);
195 }
196
197 void
198 usage (int status)
199 {
200   if (status != EXIT_SUCCESS)
201     emit_try_help ();
202   else
203     {
204       printf (_("\
205 Usage: %s [OPTION]... [INPUT [PREFIX]]\n\
206 "),
207               program_name);
208       fputs (_("\
209 Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default\n\
210 size is 1000 lines, and default PREFIX is 'x'.  With no INPUT, or when INPUT\n\
211 is -, read standard input.\n\
212 \n\
213 "), stdout);
214       fputs (_("\
215 Mandatory arguments to long options are mandatory for short options too.\n\
216 "), stdout);
217       fprintf (stdout, _("\
218   -a, --suffix-length=N   generate suffixes of length N (default %d)\n\
219       --additional-suffix=SUFFIX  append an additional SUFFIX to file names.\n\
220   -b, --bytes=SIZE        put SIZE bytes per output file\n\
221   -C, --line-bytes=SIZE   put at most SIZE bytes of lines per output file\n\
222   -d, --numeric-suffixes[=FROM]  use numeric suffixes instead of alphabetic.\n\
223                                    FROM changes the start value (default 0).\n\
224   -e, --elide-empty-files  do not generate empty output files with '-n'\n\
225       --filter=COMMAND    write to shell COMMAND; file name is $FILE\n\
226   -l, --lines=NUMBER      put NUMBER lines per output file\n\
227   -n, --number=CHUNKS     generate CHUNKS output files.  See below\n\
228   -u, --unbuffered        immediately copy input to output with '-n r/...'\n\
229 "), DEFAULT_SUFFIX_LENGTH);
230       fputs (_("\
231       --verbose           print a diagnostic just before each\n\
232                             output file is opened\n\
233 "), stdout);
234       fputs (HELP_OPTION_DESCRIPTION, stdout);
235       fputs (VERSION_OPTION_DESCRIPTION, stdout);
236       emit_size_note ();
237       fputs (_("\n\
238 CHUNKS may be:\n\
239 N       split into N files based on size of input\n\
240 K/N     output Kth of N to stdout\n\
241 l/N     split into N files without splitting lines\n\
242 l/K/N   output Kth of N to stdout without splitting lines\n\
243 r/N     like 'l' but use round robin distribution\n\
244 r/K/N   likewise but only output Kth of N to stdout\n\
245 "), stdout);
246       emit_ancillary_info ();
247     }
248   exit (status);
249 }
250
251 /* Compute the next sequential output file name and store it into the
252    string 'outfile'.  */
253
254 static void
255 next_file_name (void)
256 {
257   /* Index in suffix_alphabet of each character in the suffix.  */
258   static size_t *sufindex;
259   static size_t outbase_length;
260   static size_t outfile_length;
261   static size_t addsuf_length;
262
263   if (! outfile)
264     {
265       bool widen;
266
267 new_name:
268       widen = !! outfile_length;
269
270       if (! widen)
271         {
272           /* Allocate and initialize the first file name.  */
273
274           outbase_length = strlen (outbase);
275           addsuf_length = additional_suffix ? strlen (additional_suffix) : 0;
276           outfile_length = outbase_length + suffix_length + addsuf_length;
277         }
278       else
279         {
280           /* Reallocate and initialize a new wider file name.
281              We do this by subsuming the unchanging part of
282              the generated suffix into the prefix (base), and
283              reinitializing the now one longer suffix.  */
284
285           outfile_length += 2;
286           suffix_length++;
287         }
288
289       if (outfile_length + 1 < outbase_length)
290         xalloc_die ();
291       outfile = xrealloc (outfile, outfile_length + 1);
292
293       if (! widen)
294         memcpy (outfile, outbase, outbase_length);
295       else
296         {
297           /* Append the last alphabet character to the file name prefix.  */
298           outfile[outbase_length] = suffix_alphabet[sufindex[0]];
299           outbase_length++;
300         }
301
302       outfile_mid = outfile + outbase_length;
303       memset (outfile_mid, suffix_alphabet[0], suffix_length);
304       if (additional_suffix)
305         memcpy (outfile_mid + suffix_length, additional_suffix, addsuf_length);
306       outfile[outfile_length] = 0;
307
308       free (sufindex);
309       sufindex = xcalloc (suffix_length, sizeof *sufindex);
310
311       if (numeric_suffix_start)
312         {
313           assert (! widen);
314
315           /* Update the output file name.  */
316           size_t i = strlen (numeric_suffix_start);
317           memcpy (outfile_mid + suffix_length - i, numeric_suffix_start, i);
318
319           /* Update the suffix index.  */
320           size_t *sufindex_end = sufindex + suffix_length;
321           while (i-- != 0)
322             *--sufindex_end = numeric_suffix_start[i] - '0';
323         }
324
325 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
326       /* POSIX requires that if the output file name is too long for
327          its directory, 'split' must fail without creating any files.
328          This must be checked for explicitly on operating systems that
329          silently truncate file names.  */
330       {
331         char *dir = dir_name (outfile);
332         long name_max = pathconf (dir, _PC_NAME_MAX);
333         if (0 <= name_max && name_max < base_len (last_component (outfile)))
334           error (EXIT_FAILURE, ENAMETOOLONG, "%s", outfile);
335         free (dir);
336       }
337 #endif
338     }
339   else
340     {
341       /* Increment the suffix in place, if possible.  */
342
343       size_t i = suffix_length;
344       while (i-- != 0)
345         {
346           sufindex[i]++;
347           if (suffix_auto && i == 0 && ! suffix_alphabet[sufindex[0] + 1])
348             goto new_name;
349           outfile_mid[i] = suffix_alphabet[sufindex[i]];
350           if (outfile_mid[i])
351             return;
352           sufindex[i] = 0;
353           outfile_mid[i] = suffix_alphabet[sufindex[i]];
354         }
355       error (EXIT_FAILURE, 0, _("output file suffixes exhausted"));
356     }
357 }
358
359 /* Create or truncate a file.  */
360
361 static int
362 create (const char *name)
363 {
364   if (!filter_command)
365     {
366       if (verbose)
367         fprintf (stdout, _("creating file %s\n"), quote (name));
368
369       int fd = open (name, O_WRONLY | O_CREAT | O_BINARY, MODE_RW_UGO);
370       if (fd < 0)
371         return fd;
372       struct stat out_stat_buf;
373       if (fstat (fd, &out_stat_buf) != 0)
374         error (EXIT_FAILURE, errno, _("failed to stat %s"), quote (name));
375       if (SAME_INODE (in_stat_buf, out_stat_buf))
376         error (EXIT_FAILURE, 0, _("%s would overwrite input; aborting"),
377                quote (name));
378       if (ftruncate (fd, 0) != 0)
379         error (EXIT_FAILURE, errno, _("%s: error truncating"), quote (name));
380
381       return fd;
382     }
383   else
384     {
385       int fd_pair[2];
386       pid_t child_pid;
387       char const *shell_prog = getenv ("SHELL");
388       if (shell_prog == NULL)
389         shell_prog = "/bin/sh";
390       if (setenv ("FILE", name, 1) != 0)
391         error (EXIT_FAILURE, errno,
392                _("failed to set FILE environment variable"));
393       if (verbose)
394         fprintf (stdout, _("executing with FILE=%s\n"), quote (name));
395       if (pipe (fd_pair) != 0)
396         error (EXIT_FAILURE, errno, _("failed to create pipe"));
397       child_pid = fork ();
398       if (child_pid == 0)
399         {
400           /* This is the child process.  If an error occurs here, the
401              parent will eventually learn about it after doing a wait,
402              at which time it will emit its own error message.  */
403           int j;
404           /* We have to close any pipes that were opened during an
405              earlier call, otherwise this process will be holding a
406              write-pipe that will prevent the earlier process from
407              reading an EOF on the corresponding read-pipe.  */
408           for (j = 0; j < n_open_pipes; ++j)
409             if (close (open_pipes[j]) != 0)
410               error (EXIT_FAILURE, errno, _("closing prior pipe"));
411           if (close (fd_pair[1]))
412             error (EXIT_FAILURE, errno, _("closing output pipe"));
413           if (fd_pair[0] != STDIN_FILENO)
414             {
415               if (dup2 (fd_pair[0], STDIN_FILENO) != STDIN_FILENO)
416                 error (EXIT_FAILURE, errno, _("moving input pipe"));
417               if (close (fd_pair[0]) != 0)
418                 error (EXIT_FAILURE, errno, _("closing input pipe"));
419             }
420           sigprocmask (SIG_SETMASK, &oldblocked, NULL);
421           execl (shell_prog, last_component (shell_prog), "-c",
422                  filter_command, (char *) NULL);
423           error (EXIT_FAILURE, errno, _("failed to run command: \"%s -c %s\""),
424                  shell_prog, filter_command);
425         }
426       if (child_pid == -1)
427         error (EXIT_FAILURE, errno, _("fork system call failed"));
428       if (close (fd_pair[0]) != 0)
429         error (EXIT_FAILURE, errno, _("failed to close input pipe"));
430       filter_pid = child_pid;
431       if (n_open_pipes == open_pipes_alloc)
432         open_pipes = x2nrealloc (open_pipes, &open_pipes_alloc,
433                                  sizeof *open_pipes);
434       open_pipes[n_open_pipes++] = fd_pair[1];
435       return fd_pair[1];
436     }
437 }
438
439 /* Close the output file, and do any associated cleanup.
440    If FP and FD are both specified, they refer to the same open file;
441    in this case FP is closed, but FD is still used in cleanup.  */
442 static void
443 closeout (FILE *fp, int fd, pid_t pid, char const *name)
444 {
445   if (fp != NULL && fclose (fp) != 0 && ! ignorable (errno))
446     error (EXIT_FAILURE, errno, "%s", name);
447   if (fd >= 0)
448     {
449       if (fp == NULL && close (fd) < 0)
450         error (EXIT_FAILURE, errno, "%s", name);
451       int j;
452       for (j = 0; j < n_open_pipes; ++j)
453         {
454           if (open_pipes[j] == fd)
455             {
456               open_pipes[j] = open_pipes[--n_open_pipes];
457               break;
458             }
459         }
460     }
461   if (pid > 0)
462     {
463       int wstatus = 0;
464       if (waitpid (pid, &wstatus, 0) == -1 && errno != ECHILD)
465         error (EXIT_FAILURE, errno, _("waiting for child process"));
466       if (WIFSIGNALED (wstatus))
467         {
468           int sig = WTERMSIG (wstatus);
469           if (sig != SIGPIPE)
470             {
471               char signame[MAX (SIG2STR_MAX, INT_BUFSIZE_BOUND (int))];
472               if (sig2str (sig, signame) != 0)
473                 sprintf (signame, "%d", sig);
474               error (sig + 128, 0,
475                      _("with FILE=%s, signal %s from command: %s"),
476                      name, signame, filter_command);
477             }
478         }
479       else if (WIFEXITED (wstatus))
480         {
481           int ex = WEXITSTATUS (wstatus);
482           if (ex != 0)
483             error (ex, 0, _("with FILE=%s, exit %d from command: %s"),
484                    name, ex, filter_command);
485         }
486       else
487         {
488           /* shouldn't happen.  */
489           error (EXIT_FAILURE, 0,
490                  _("unknown status from command (0x%X)"), wstatus);
491         }
492     }
493 }
494
495 /* Write BYTES bytes at BP to an output file.
496    If NEW_FILE_FLAG is true, open the next output file.
497    Otherwise add to the same output file already in use.  */
498
499 static void
500 cwrite (bool new_file_flag, const char *bp, size_t bytes)
501 {
502   if (new_file_flag)
503     {
504       if (!bp && bytes == 0 && elide_empty_files)
505         return;
506       closeout (NULL, output_desc, filter_pid, outfile);
507       next_file_name ();
508       if ((output_desc = create (outfile)) < 0)
509         error (EXIT_FAILURE, errno, "%s", outfile);
510     }
511   if (full_write (output_desc, bp, bytes) != bytes && ! ignorable (errno))
512     error (EXIT_FAILURE, errno, "%s", outfile);
513 }
514
515 /* Split into pieces of exactly N_BYTES bytes.
516    Use buffer BUF, whose size is BUFSIZE.  */
517
518 static void
519 bytes_split (uintmax_t n_bytes, char *buf, size_t bufsize, uintmax_t max_files)
520 {
521   size_t n_read;
522   bool new_file_flag = true;
523   size_t to_read;
524   uintmax_t to_write = n_bytes;
525   char *bp_out;
526   uintmax_t opened = 0;
527
528   do
529     {
530       n_read = full_read (STDIN_FILENO, buf, bufsize);
531       if (n_read < bufsize && errno)
532         error (EXIT_FAILURE, errno, "%s", infile);
533       bp_out = buf;
534       to_read = n_read;
535       while (true)
536         {
537           if (to_read < to_write)
538             {
539               if (to_read)      /* do not write 0 bytes! */
540                 {
541                   cwrite (new_file_flag, bp_out, to_read);
542                   opened += new_file_flag;
543                   to_write -= to_read;
544                   new_file_flag = false;
545                 }
546               break;
547             }
548           else
549             {
550               size_t w = to_write;
551               cwrite (new_file_flag, bp_out, w);
552               opened += new_file_flag;
553               new_file_flag = !max_files || (opened < max_files);
554               if (!new_file_flag && ignorable (errno))
555                 {
556                   /* If filter no longer accepting input, stop reading.  */
557                   n_read = 0;
558                   break;
559                 }
560               bp_out += w;
561               to_read -= w;
562               to_write = n_bytes;
563             }
564         }
565     }
566   while (n_read == bufsize);
567
568   /* Ensure NUMBER files are created, which truncates
569      any existing files or notifies any consumers on fifos.
570      FIXME: Should we do this before EXIT_FAILURE?  */
571   while (opened++ < max_files)
572     cwrite (true, NULL, 0);
573 }
574
575 /* Split into pieces of exactly N_LINES lines.
576    Use buffer BUF, whose size is BUFSIZE.  */
577
578 static void
579 lines_split (uintmax_t n_lines, char *buf, size_t bufsize)
580 {
581   size_t n_read;
582   char *bp, *bp_out, *eob;
583   bool new_file_flag = true;
584   uintmax_t n = 0;
585
586   do
587     {
588       n_read = full_read (STDIN_FILENO, buf, bufsize);
589       if (n_read < bufsize && errno)
590         error (EXIT_FAILURE, errno, "%s", infile);
591       bp = bp_out = buf;
592       eob = bp + n_read;
593       *eob = '\n';
594       while (true)
595         {
596           bp = memchr (bp, '\n', eob - bp + 1);
597           if (bp == eob)
598             {
599               if (eob != bp_out) /* do not write 0 bytes! */
600                 {
601                   size_t len = eob - bp_out;
602                   cwrite (new_file_flag, bp_out, len);
603                   new_file_flag = false;
604                 }
605               break;
606             }
607
608           ++bp;
609           if (++n >= n_lines)
610             {
611               cwrite (new_file_flag, bp_out, bp - bp_out);
612               bp_out = bp;
613               new_file_flag = true;
614               n = 0;
615             }
616         }
617     }
618   while (n_read == bufsize);
619 }
620 \f
621 /* Split into pieces that are as large as possible while still not more
622    than N_BYTES bytes, and are split on line boundaries except
623    where lines longer than N_BYTES bytes occur.
624    FIXME: Allow N_BYTES to be any uintmax_t value, and don't require a
625    buffer of size N_BYTES, in case N_BYTES is very large.  */
626
627 static void
628 line_bytes_split (size_t n_bytes)
629 {
630   char *bp;
631   bool eof = false;
632   size_t n_buffered = 0;
633   char *buf = xmalloc (n_bytes);
634
635   do
636     {
637       /* Fill up the full buffer size from the input file.  */
638
639       size_t to_read = n_bytes - n_buffered;
640       size_t n_read = full_read (STDIN_FILENO, buf + n_buffered, to_read);
641       if (n_read < to_read && errno)
642         error (EXIT_FAILURE, errno, "%s", infile);
643
644       n_buffered += n_read;
645       if (n_buffered != n_bytes)
646         {
647           if (n_buffered == 0)
648             break;
649           eof = true;
650         }
651
652       /* Find where to end this chunk.  */
653       bp = buf + n_buffered;
654       if (n_buffered == n_bytes)
655         {
656           while (bp > buf && bp[-1] != '\n')
657             bp--;
658         }
659
660       /* If chunk has no newlines, use all the chunk.  */
661       if (bp == buf)
662         bp = buf + n_buffered;
663
664       /* Output the chars as one output file.  */
665       cwrite (true, buf, bp - buf);
666
667       /* Discard the chars we just output; move rest of chunk
668          down to be the start of the next chunk.  Source and
669          destination probably overlap.  */
670       n_buffered -= bp - buf;
671       if (n_buffered > 0)
672         memmove (buf, bp, n_buffered);
673     }
674   while (!eof);
675   free (buf);
676 }
677
678 /* -n l/[K/]N: Write lines to files of approximately file size / N.
679    The file is partitioned into file size / N sized portions, with the
680    last assigned any excess.  If a line _starts_ within a partition
681    it is written completely to the corresponding file.  Since lines
682    are not split even if they overlap a partition, the files written
683    can be larger or smaller than the partition size, and even empty
684    if a line is so long as to completely overlap the partition.  */
685
686 static void
687 lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
688                    off_t file_size)
689 {
690   assert (n && k <= n && n <= file_size);
691
692   const off_t chunk_size = file_size / n;
693   uintmax_t chunk_no = 1;
694   off_t chunk_end = chunk_size - 1;
695   off_t n_written = 0;
696   bool new_file_flag = true;
697   bool chunk_truncated = false;
698
699   if (k > 1)
700     {
701       /* Start reading 1 byte before kth chunk of file.  */
702       off_t start = (k - 1) * chunk_size - 1;
703       if (lseek (STDIN_FILENO, start, SEEK_CUR) < 0)
704         error (EXIT_FAILURE, errno, "%s", infile);
705       n_written = start;
706       chunk_no = k - 1;
707       chunk_end = chunk_no * chunk_size - 1;
708     }
709
710   while (n_written < file_size)
711     {
712       char *bp = buf, *eob;
713       size_t n_read = full_read (STDIN_FILENO, buf, bufsize);
714       if (n_read < bufsize && errno)
715         error (EXIT_FAILURE, errno, "%s", infile);
716       else if (n_read == 0)
717         break; /* eof.  */
718       n_read = MIN (n_read, file_size - n_written);
719       chunk_truncated = false;
720       eob = buf + n_read;
721
722       while (bp != eob)
723         {
724           size_t to_write;
725           bool next = false;
726
727           /* Begin looking for '\n' at last byte of chunk.  */
728           off_t skip = MIN (n_read, MAX (0, chunk_end - n_written));
729           char *bp_out = memchr (bp + skip, '\n', n_read - skip);
730           if (bp_out++)
731             next = true;
732           else
733             bp_out = eob;
734           to_write = bp_out - bp;
735
736           if (k == chunk_no)
737             {
738               /* We don't use the stdout buffer here since we're writing
739                  large chunks from an existing file, so it's more efficient
740                  to write out directly.  */
741               if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
742                 error (EXIT_FAILURE, errno, "%s", _("write error"));
743             }
744           else if (! k)
745             cwrite (new_file_flag, bp, to_write);
746           n_written += to_write;
747           bp += to_write;
748           n_read -= to_write;
749           new_file_flag = next;
750
751           /* A line could have been so long that it skipped
752              entire chunks. So create empty files in that case.  */
753           while (next || chunk_end <= n_written - 1)
754             {
755               if (!next && bp == eob)
756                 {
757                   /* replenish buf, before going to next chunk.  */
758                   chunk_truncated = true;
759                   break;
760                 }
761               chunk_no++;
762               if (k && chunk_no > k)
763                 return;
764               if (chunk_no == n)
765                 chunk_end = file_size - 1; /* >= chunk_size.  */
766               else
767                 chunk_end += chunk_size;
768               if (chunk_end <= n_written - 1)
769                 {
770                   if (! k)
771                     cwrite (true, NULL, 0);
772                 }
773               else
774                 next = false;
775             }
776         }
777     }
778
779   if (chunk_truncated)
780     chunk_no++;
781
782   /* Ensure NUMBER files are created, which truncates
783      any existing files or notifies any consumers on fifos.
784      FIXME: Should we do this before EXIT_FAILURE?  */
785   while (!k && chunk_no++ <= n)
786     cwrite (true, NULL, 0);
787 }
788
789 /* -n K/N: Extract Kth of N chunks.  */
790
791 static void
792 bytes_chunk_extract (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
793                      off_t file_size)
794 {
795   off_t start;
796   off_t end;
797
798   assert (k && n && k <= n && n <= file_size);
799
800   start = (k - 1) * (file_size / n);
801   end = (k == n) ? file_size : k * (file_size / n);
802
803   if (lseek (STDIN_FILENO, start, SEEK_CUR) < 0)
804     error (EXIT_FAILURE, errno, "%s", infile);
805
806   while (start < end)
807     {
808       size_t n_read = full_read (STDIN_FILENO, buf, bufsize);
809       if (n_read < bufsize && errno)
810         error (EXIT_FAILURE, errno, "%s", infile);
811       else if (n_read == 0)
812         break; /* eof.  */
813       n_read = MIN (n_read, end - start);
814       if (full_write (STDOUT_FILENO, buf, n_read) != n_read
815           && ! ignorable (errno))
816         error (EXIT_FAILURE, errno, "%s", quote ("-"));
817       start += n_read;
818     }
819 }
820
821 typedef struct of_info
822 {
823   char *of_name;
824   int ofd;
825   FILE *ofile;
826   int opid;
827 } of_t;
828
829 enum
830 {
831   OFD_NEW = -1,
832   OFD_APPEND = -2
833 };
834
835 /* Rotate file descriptors when we're writing to more output files than we
836    have available file descriptors.
837    Return whether we came under file resource pressure.
838    If so, it's probably best to close each file when finished with it.  */
839
840 static bool
841 ofile_open (of_t *files, size_t i_check, size_t nfiles)
842 {
843   bool file_limit = false;
844
845   if (files[i_check].ofd <= OFD_NEW)
846     {
847       int fd;
848       size_t i_reopen = i_check ? i_check - 1 : nfiles - 1;
849
850       /* Another process could have opened a file in between the calls to
851          close and open, so we should keep trying until open succeeds or
852          we've closed all of our files.  */
853       while (true)
854         {
855           if (files[i_check].ofd == OFD_NEW)
856             fd = create (files[i_check].of_name);
857           else /* OFD_APPEND  */
858             {
859               /* Attempt to append to previously opened file.
860                  We use O_NONBLOCK to support writing to fifos,
861                  where the other end has closed because of our
862                  previous close.  In that case we'll immediately
863                  get an error, rather than waiting indefinitely.
864                  In specialised cases the consumer can keep reading
865                  from the fifo, terminating on conditions in the data
866                  itself, or perhaps never in the case of 'tail -f'.
867                  I.E. for fifos it is valid to attempt this reopen.
868
869                  We don't handle the filter_command case here, as create()
870                  will exit if there are not enough files in that case.
871                  I.E. we don't support restarting filters, as that would
872                  put too much burden on users specifying --filter commands.  */
873               fd = open (files[i_check].of_name,
874                          O_WRONLY | O_BINARY | O_APPEND | O_NONBLOCK);
875             }
876
877           if (-1 < fd)
878             break;
879
880           if (!(errno == EMFILE || errno == ENFILE))
881             error (EXIT_FAILURE, errno, "%s", files[i_check].of_name);
882
883           file_limit = true;
884
885           /* Search backwards for an open file to close.  */
886           while (files[i_reopen].ofd < 0)
887             {
888               i_reopen = i_reopen ? i_reopen - 1 : nfiles - 1;
889               /* No more open files to close, exit with E[NM]FILE.  */
890               if (i_reopen == i_check)
891                 error (EXIT_FAILURE, errno, "%s", files[i_check].of_name);
892             }
893
894           if (fclose (files[i_reopen].ofile) != 0)
895             error (EXIT_FAILURE, errno, "%s", files[i_reopen].of_name);
896           files[i_reopen].ofile = NULL;
897           files[i_reopen].ofd = OFD_APPEND;
898         }
899
900       files[i_check].ofd = fd;
901       if (!(files[i_check].ofile = fdopen (fd, "a")))
902         error (EXIT_FAILURE, errno, "%s", files[i_check].of_name);
903       files[i_check].opid = filter_pid;
904       filter_pid = 0;
905     }
906
907   return file_limit;
908 }
909
910 /* -n r/[K/]N: Divide file into N chunks in round robin fashion.
911    When K == 0, we try to keep the files open in parallel.
912    If we run out of file resources, then we revert
913    to opening and closing each file for each line.  */
914
915 static void
916 lines_rr (uintmax_t k, uintmax_t n, char *buf, size_t bufsize)
917 {
918   bool wrapped = false;
919   bool wrote = false;
920   bool file_limit;
921   size_t i_file;
922   of_t *files IF_LINT (= NULL);
923   uintmax_t line_no;
924
925   if (k)
926     line_no = 1;
927   else
928     {
929       if (SIZE_MAX < n)
930         error (exit_failure, 0, "%s", _("memory exhausted"));
931       files = xnmalloc (n, sizeof *files);
932
933       /* Generate output file names. */
934       for (i_file = 0; i_file < n; i_file++)
935         {
936           next_file_name ();
937           files[i_file].of_name = xstrdup (outfile);
938           files[i_file].ofd = OFD_NEW;
939           files[i_file].ofile = NULL;
940           files[i_file].opid = 0;
941         }
942       i_file = 0;
943       file_limit = false;
944     }
945
946   while (true)
947     {
948       char *bp = buf, *eob;
949       /* Use safe_read() rather than full_read() here
950          so that we process available data immediately.  */
951       size_t n_read = safe_read (STDIN_FILENO, buf, bufsize);
952       if (n_read == SAFE_READ_ERROR)
953         error (EXIT_FAILURE, errno, "%s", infile);
954       else if (n_read == 0)
955         break; /* eof.  */
956       eob = buf + n_read;
957
958       while (bp != eob)
959         {
960           size_t to_write;
961           bool next = false;
962
963           /* Find end of line. */
964           char *bp_out = memchr (bp, '\n', eob - bp);
965           if (bp_out)
966             {
967               bp_out++;
968               next = true;
969             }
970           else
971             bp_out = eob;
972           to_write = bp_out - bp;
973
974           if (k)
975             {
976               if (line_no == k && unbuffered)
977                 {
978                   if (full_write (STDOUT_FILENO, bp, to_write) != to_write)
979                     error (EXIT_FAILURE, errno, "%s", _("write error"));
980                 }
981               else if (line_no == k && fwrite (bp, to_write, 1, stdout) != 1)
982                 {
983                   clearerr (stdout); /* To silence close_stdout().  */
984                   error (EXIT_FAILURE, errno, "%s", _("write error"));
985                 }
986               if (next)
987                 line_no = (line_no == n) ? 1 : line_no + 1;
988             }
989           else
990             {
991               /* Secure file descriptor. */
992               file_limit |= ofile_open (files, i_file, n);
993               if (unbuffered)
994                 {
995                   /* Note writing to fd, rather than flushing the FILE gives
996                      an 8% performance benefit, due to reduced data copying.  */
997                   if (full_write (files[i_file].ofd, bp, to_write) != to_write
998                       && ! ignorable (errno))
999                     error (EXIT_FAILURE, errno, "%s", files[i_file].of_name);
1000                 }
1001               else if (fwrite (bp, to_write, 1, files[i_file].ofile) != 1
1002                        && ! ignorable (errno))
1003                 error (EXIT_FAILURE, errno, "%s", files[i_file].of_name);
1004               if (! ignorable (errno))
1005                 wrote = true;
1006
1007               if (file_limit)
1008                 {
1009                   if (fclose (files[i_file].ofile) != 0)
1010                     error (EXIT_FAILURE, errno, "%s", files[i_file].of_name);
1011                   files[i_file].ofile = NULL;
1012                   files[i_file].ofd = OFD_APPEND;
1013                 }
1014               if (next && ++i_file == n)
1015                 {
1016                   wrapped = true;
1017                   /* If no filters are accepting input, stop reading.  */
1018                   if (! wrote)
1019                     goto no_filters;
1020                   wrote = false;
1021                   i_file = 0;
1022                 }
1023             }
1024
1025           bp = bp_out;
1026         }
1027     }
1028
1029 no_filters:
1030   /* Ensure all files created, so that any existing files are truncated,
1031      and to signal any waiting fifo consumers.
1032      Also, close any open file descriptors.
1033      FIXME: Should we do this before EXIT_FAILURE?  */
1034   if (!k)
1035     {
1036       int ceiling = (wrapped ? n : i_file);
1037       for (i_file = 0; i_file < n; i_file++)
1038         {
1039           if (i_file >= ceiling && !elide_empty_files)
1040             file_limit |= ofile_open (files, i_file, n);
1041           if (files[i_file].ofd >= 0)
1042             closeout (files[i_file].ofile, files[i_file].ofd,
1043                       files[i_file].opid, files[i_file].of_name);
1044           files[i_file].ofd = OFD_APPEND;
1045         }
1046     }
1047   IF_LINT (free (files));
1048 }
1049
1050 #define FAIL_ONLY_ONE_WAY()                                     \
1051   do                                                            \
1052     {                                                           \
1053       error (0, 0, _("cannot split in more than one way"));     \
1054       usage (EXIT_FAILURE);                                     \
1055     }                                                           \
1056   while (0)
1057
1058 /* Parse K/N syntax of chunk options.  */
1059
1060 static void
1061 parse_chunk (uintmax_t *k_units, uintmax_t *n_units, char *slash)
1062 {
1063   *slash = '\0';
1064   if (xstrtoumax (slash + 1, NULL, 10, n_units, "") != LONGINT_OK
1065       || *n_units == 0)
1066     error (EXIT_FAILURE, 0, _("%s: invalid number of chunks"), slash + 1);
1067   if (slash != optarg           /* a leading number is specified.  */
1068       && (xstrtoumax (optarg, NULL, 10, k_units, "") != LONGINT_OK
1069           || *k_units == 0 || *n_units < *k_units))
1070     error (EXIT_FAILURE, 0, _("%s: invalid chunk number"), optarg);
1071 }
1072
1073
1074 int
1075 main (int argc, char **argv)
1076 {
1077   enum Split_type split_type = type_undef;
1078   size_t in_blk_size = 0;       /* optimal block size of input file device */
1079   size_t page_size = getpagesize ();
1080   uintmax_t k_units = 0;
1081   uintmax_t n_units;
1082
1083   static char const multipliers[] = "bEGKkMmPTYZ0";
1084   int c;
1085   int digits_optind = 0;
1086   off_t file_size IF_LINT (= 0);
1087
1088   initialize_main (&argc, &argv);
1089   set_program_name (argv[0]);
1090   setlocale (LC_ALL, "");
1091   bindtextdomain (PACKAGE, LOCALEDIR);
1092   textdomain (PACKAGE);
1093
1094   atexit (close_stdout);
1095
1096   /* Parse command line options.  */
1097
1098   infile = bad_cast ("-");
1099   outbase = bad_cast ("x");
1100
1101   while (true)
1102     {
1103       /* This is the argv-index of the option we will read next.  */
1104       int this_optind = optind ? optind : 1;
1105       char *slash;
1106
1107       c = getopt_long (argc, argv, "0123456789C:a:b:del:n:u",
1108                        longopts, NULL);
1109       if (c == -1)
1110         break;
1111
1112       switch (c)
1113         {
1114         case 'a':
1115           {
1116             unsigned long tmp;
1117             if (xstrtoul (optarg, NULL, 10, &tmp, "") != LONGINT_OK
1118                 || SIZE_MAX / sizeof (size_t) < tmp)
1119               {
1120                 error (0, 0, _("%s: invalid suffix length"), optarg);
1121                 usage (EXIT_FAILURE);
1122               }
1123             suffix_length = tmp;
1124           }
1125           break;
1126
1127         case ADDITIONAL_SUFFIX_OPTION:
1128           if (last_component (optarg) != optarg)
1129             {
1130               error (0, 0,
1131                      _("invalid suffix %s, contains directory separator"),
1132                      quote (optarg));
1133               usage (EXIT_FAILURE);
1134             }
1135           additional_suffix = optarg;
1136           break;
1137
1138         case 'b':
1139           if (split_type != type_undef)
1140             FAIL_ONLY_ONE_WAY ();
1141           split_type = type_bytes;
1142           if (xstrtoumax (optarg, NULL, 10, &n_units, multipliers) != LONGINT_OK
1143               || n_units == 0)
1144             {
1145               error (0, 0, _("%s: invalid number of bytes"), optarg);
1146               usage (EXIT_FAILURE);
1147             }
1148           /* If input is a pipe, we could get more data than is possible
1149              to write to a single file, so indicate that immediately
1150              rather than having possibly future invocations fail.  */
1151           if (OFF_T_MAX < n_units)
1152             error (EXIT_FAILURE, EFBIG,
1153                    _("%s: invalid number of bytes"), optarg);
1154
1155           break;
1156
1157         case 'l':
1158           if (split_type != type_undef)
1159             FAIL_ONLY_ONE_WAY ();
1160           split_type = type_lines;
1161           if (xstrtoumax (optarg, NULL, 10, &n_units, "") != LONGINT_OK
1162               || n_units == 0)
1163             {
1164               error (0, 0, _("%s: invalid number of lines"), optarg);
1165               usage (EXIT_FAILURE);
1166             }
1167           break;
1168
1169         case 'C':
1170           if (split_type != type_undef)
1171             FAIL_ONLY_ONE_WAY ();
1172           split_type = type_byteslines;
1173           if (xstrtoumax (optarg, NULL, 10, &n_units, multipliers) != LONGINT_OK
1174               || n_units == 0 || SIZE_MAX < n_units)
1175             {
1176               error (0, 0, _("%s: invalid number of bytes"), optarg);
1177               usage (EXIT_FAILURE);
1178             }
1179           if (OFF_T_MAX < n_units)
1180             error (EXIT_FAILURE, EFBIG,
1181                    _("%s: invalid number of bytes"), optarg);
1182           break;
1183
1184         case 'n':
1185           if (split_type != type_undef)
1186             FAIL_ONLY_ONE_WAY ();
1187           /* skip any whitespace */
1188           while (isspace (to_uchar (*optarg)))
1189             optarg++;
1190           if (STRNCMP_LIT (optarg, "r/") == 0)
1191             {
1192               split_type = type_rr;
1193               optarg += 2;
1194             }
1195           else if (STRNCMP_LIT (optarg, "l/") == 0)
1196             {
1197               split_type = type_chunk_lines;
1198               optarg += 2;
1199             }
1200           else
1201             split_type = type_chunk_bytes;
1202           if ((slash = strchr (optarg, '/')))
1203             parse_chunk (&k_units, &n_units, slash);
1204           else if (xstrtoumax (optarg, NULL, 10, &n_units, "") != LONGINT_OK
1205                    || n_units == 0)
1206             error (EXIT_FAILURE, 0, _("%s: invalid number of chunks"), optarg);
1207           break;
1208
1209         case 'u':
1210           unbuffered = true;
1211           break;
1212
1213         case '0':
1214         case '1':
1215         case '2':
1216         case '3':
1217         case '4':
1218         case '5':
1219         case '6':
1220         case '7':
1221         case '8':
1222         case '9':
1223           if (split_type == type_undef)
1224             {
1225               split_type = type_digits;
1226               n_units = 0;
1227             }
1228           if (split_type != type_undef && split_type != type_digits)
1229             FAIL_ONLY_ONE_WAY ();
1230           if (digits_optind != 0 && digits_optind != this_optind)
1231             n_units = 0;        /* More than one number given; ignore other. */
1232           digits_optind = this_optind;
1233           if (!DECIMAL_DIGIT_ACCUMULATE (n_units, c - '0', uintmax_t))
1234             {
1235               char buffer[INT_BUFSIZE_BOUND (uintmax_t)];
1236               error (EXIT_FAILURE, 0,
1237                      _("line count option -%s%c... is too large"),
1238                      umaxtostr (n_units, buffer), c);
1239             }
1240           break;
1241
1242         case 'd':
1243           suffix_alphabet = "0123456789";
1244           if (optarg)
1245             {
1246               if (strlen (optarg) != strspn (optarg, suffix_alphabet))
1247                 {
1248                   error (0, 0,
1249                          _("%s: invalid start value for numerical suffix"),
1250                          optarg);
1251                   usage (EXIT_FAILURE);
1252                 }
1253               else
1254                 {
1255                   /* Skip any leading zero.  */
1256                   while (*optarg == '0' && *(optarg + 1) != '\0')
1257                     optarg++;
1258                   numeric_suffix_start = optarg;
1259                 }
1260             }
1261           break;
1262
1263         case 'e':
1264           elide_empty_files = true;
1265           break;
1266
1267         case FILTER_OPTION:
1268           filter_command = optarg;
1269           break;
1270
1271         case IO_BLKSIZE_OPTION:
1272           {
1273             uintmax_t tmp_blk_size;
1274             if (xstrtoumax (optarg, NULL, 10, &tmp_blk_size,
1275                             multipliers) != LONGINT_OK
1276                 || tmp_blk_size == 0 || SIZE_MAX - page_size < tmp_blk_size)
1277               error (0, 0, _("%s: invalid IO block size"), optarg);
1278             else
1279               in_blk_size = tmp_blk_size;
1280           }
1281           break;
1282
1283         case VERBOSE_OPTION:
1284           verbose = true;
1285           break;
1286
1287         case_GETOPT_HELP_CHAR;
1288
1289         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1290
1291         default:
1292           usage (EXIT_FAILURE);
1293         }
1294     }
1295
1296   if (k_units != 0 && filter_command)
1297     {
1298       error (0, 0, _("--filter does not process a chunk extracted to stdout"));
1299       usage (EXIT_FAILURE);
1300     }
1301
1302   /* Handle default case.  */
1303   if (split_type == type_undef)
1304     {
1305       split_type = type_lines;
1306       n_units = 1000;
1307     }
1308
1309   if (n_units == 0)
1310     {
1311       error (0, 0, _("%s: invalid number of lines"), "0");
1312       usage (EXIT_FAILURE);
1313     }
1314
1315   set_suffix_length (n_units, split_type);
1316
1317   /* Get out the filename arguments.  */
1318
1319   if (optind < argc)
1320     infile = argv[optind++];
1321
1322   if (optind < argc)
1323     outbase = argv[optind++];
1324
1325   if (optind < argc)
1326     {
1327       error (0, 0, _("extra operand %s"), quote (argv[optind]));
1328       usage (EXIT_FAILURE);
1329     }
1330
1331   /* Check that the suffix length is large enough for the numerical
1332      suffix start value.  */
1333   if (numeric_suffix_start && strlen (numeric_suffix_start) > suffix_length)
1334     {
1335       error (0, 0, _("numerical suffix start value is too large "
1336                      "for the suffix length"));
1337       usage (EXIT_FAILURE);
1338     }
1339
1340   /* Open the input file.  */
1341   if (! STREQ (infile, "-")
1342       && fd_reopen (STDIN_FILENO, infile, O_RDONLY, 0) < 0)
1343     error (EXIT_FAILURE, errno, _("cannot open %s for reading"),
1344            quote (infile));
1345
1346   /* Binary I/O is safer when byte counts are used.  */
1347   if (O_BINARY && ! isatty (STDIN_FILENO))
1348     xfreopen (NULL, "rb", stdin);
1349
1350   /* Get the optimal block size of input device and make a buffer.  */
1351
1352   if (fstat (STDIN_FILENO, &in_stat_buf) != 0)
1353     error (EXIT_FAILURE, errno, "%s", infile);
1354   if (in_blk_size == 0)
1355     in_blk_size = io_blksize (in_stat_buf);
1356
1357   if (split_type == type_chunk_bytes || split_type == type_chunk_lines)
1358     {
1359       off_t input_offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1360       if (usable_st_size (&in_stat_buf))
1361         file_size = in_stat_buf.st_size;
1362       else if (0 <= input_offset)
1363         {
1364           file_size = lseek (STDIN_FILENO, 0, SEEK_END);
1365           input_offset = (file_size < 0
1366                           ? file_size
1367                           : lseek (STDIN_FILENO, input_offset, SEEK_SET));
1368         }
1369       if (input_offset < 0)
1370         error (EXIT_FAILURE, 0, _("%s: cannot determine file size"),
1371                quote (infile));
1372       file_size -= input_offset;
1373       /* Overflow, and sanity checking.  */
1374       if (OFF_T_MAX < n_units)
1375         {
1376           char buffer[INT_BUFSIZE_BOUND (uintmax_t)];
1377           error (EXIT_FAILURE, EFBIG, _("%s: invalid number of chunks"),
1378                  umaxtostr (n_units, buffer));
1379         }
1380       /* increase file_size to n_units here, so that we still process
1381          any input data, and create empty files for the rest.  */
1382       file_size = MAX (file_size, n_units);
1383     }
1384
1385   void *b = xmalloc (in_blk_size + 1 + page_size - 1);
1386   char *buf = ptr_align (b, page_size);
1387
1388   /* When filtering, closure of one pipe must not terminate the process,
1389      as there may still be other streams expecting input from us.  */
1390   if (filter_command)
1391     {
1392       struct sigaction act;
1393       sigemptyset (&newblocked);
1394       sigaction (SIGPIPE, NULL, &act);
1395       if (act.sa_handler != SIG_IGN)
1396         sigaddset (&newblocked, SIGPIPE);
1397       sigprocmask (SIG_BLOCK, &newblocked, &oldblocked);
1398     }
1399
1400   switch (split_type)
1401     {
1402     case type_digits:
1403     case type_lines:
1404       lines_split (n_units, buf, in_blk_size);
1405       break;
1406
1407     case type_bytes:
1408       bytes_split (n_units, buf, in_blk_size, 0);
1409       break;
1410
1411     case type_byteslines:
1412       line_bytes_split (n_units);
1413       break;
1414
1415     case type_chunk_bytes:
1416       if (k_units == 0)
1417         bytes_split (file_size / n_units, buf, in_blk_size, n_units);
1418       else
1419         bytes_chunk_extract (k_units, n_units, buf, in_blk_size, file_size);
1420       break;
1421
1422     case type_chunk_lines:
1423       lines_chunk_split (k_units, n_units, buf, in_blk_size, file_size);
1424       break;
1425
1426     case type_rr:
1427       /* Note, this is like 'sed -n ${k}~${n}p' when k > 0,
1428          but the functionality is provided for symmetry.  */
1429       lines_rr (k_units, n_units, buf, in_blk_size);
1430       break;
1431
1432     default:
1433       abort ();
1434     }
1435
1436   IF_LINT (free (b));
1437
1438   if (close (STDIN_FILENO) != 0)
1439     error (EXIT_FAILURE, errno, "%s", infile);
1440   closeout (NULL, output_desc, filter_pid, outfile);
1441
1442   exit (EXIT_SUCCESS);
1443 }