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