TODO: add an item for a chmod optimization
[platform/upstream/coreutils.git] / src / csplit.c
1 /* csplit - split a file into sections determined by context lines
2    Copyright (C) 91, 1995-2008 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
17 /* Written by Stuart Kemp, cpsrk@groper.jcu.edu.au.
18    Modified by David MacKenzie, djm@gnu.ai.mit.edu. */
19
20 #include <config.h>
21
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <signal.h>
25
26 #include "system.h"
27
28 #include <regex.h>
29
30 #include "error.h"
31 #include "fd-reopen.h"
32 #include "quote.h"
33 #include "safe-read.h"
34 #include "stdio--.h"
35 #include "xstrtol.h"
36
37 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
38    present.  */
39 #ifndef SA_NOCLDSTOP
40 # define SA_NOCLDSTOP 0
41 # define sigprocmask(How, Set, Oset) /* empty */
42 # define sigset_t int
43 # if ! HAVE_SIGINTERRUPT
44 #  define siginterrupt(sig, flag) /* empty */
45 # endif
46 #endif
47
48 /* The official name of this program (e.g., no `g' prefix).  */
49 #define PROGRAM_NAME "csplit"
50
51 #define AUTHORS \
52   proper_name ("Stuart Kemp"), \
53   proper_name ("David MacKenzie")
54
55 /* Increment size of area for control records. */
56 #define ALLOC_SIZE 20
57
58 /* The default prefix for output file names. */
59 #define DEFAULT_PREFIX  "xx"
60
61 /* A compiled pattern arg. */
62 struct control
63 {
64   intmax_t offset;              /* Offset from regexp to split at. */
65   uintmax_t lines_required;     /* Number of lines required. */
66   uintmax_t repeat;             /* Repeat count. */
67   int argnum;                   /* ARGV index. */
68   bool repeat_forever;          /* True if `*' used as a repeat count. */
69   bool ignore;                  /* If true, produce no output (for regexp). */
70   bool regexpr;                 /* True if regular expression was used. */
71   struct re_pattern_buffer re_compiled; /* Compiled regular expression. */
72 };
73
74 /* Initial size of data area in buffers. */
75 #define START_SIZE      8191
76
77 /* Increment size for data area. */
78 #define INCR_SIZE       2048
79
80 /* Number of lines kept in each node in line list. */
81 #define CTRL_SIZE       80
82
83 #ifdef DEBUG
84 /* Some small values to test the algorithms. */
85 # define START_SIZE     200
86 # define INCR_SIZE      10
87 # define CTRL_SIZE      1
88 #endif
89
90 /* A string with a length count. */
91 struct cstring
92 {
93   size_t len;
94   char *str;
95 };
96
97 /* Pointers to the beginnings of lines in the buffer area.
98    These structures are linked together if needed. */
99 struct line
100 {
101   size_t used;                  /* Number of offsets used in this struct. */
102   size_t insert_index;          /* Next offset to use when inserting line. */
103   size_t retrieve_index;        /* Next index to use when retrieving line. */
104   struct cstring starts[CTRL_SIZE]; /* Lines in the data area. */
105   struct line *next;            /* Next in linked list. */
106 };
107
108 /* The structure to hold the input lines.
109    Contains a pointer to the data area and a list containing
110    pointers to the individual lines. */
111 struct buffer_record
112 {
113   size_t bytes_alloc;           /* Size of the buffer area. */
114   size_t bytes_used;            /* Bytes used in the buffer area. */
115   uintmax_t start_line;         /* First line number in this buffer. */
116   uintmax_t first_available;    /* First line that can be retrieved. */
117   size_t num_lines;             /* Number of complete lines in this buffer. */
118   char *buffer;                 /* Data area. */
119   struct line *line_start;      /* Head of list of pointers to lines. */
120   struct line *curr_line;       /* The line start record currently in use. */
121   struct buffer_record *next;
122 };
123
124 static void close_output_file (void);
125 static void create_output_file (void);
126 static void delete_all_files (bool);
127 static void save_line_to_file (const struct cstring *line);
128 void usage (int status);
129
130 /* Start of buffer list. */
131 static struct buffer_record *head = NULL;
132
133 /* Partially read line. */
134 static char *hold_area = NULL;
135
136 /* Number of bytes in `hold_area'. */
137 static size_t hold_count = 0;
138
139 /* Number of the last line in the buffers. */
140 static uintmax_t last_line_number = 0;
141
142 /* Number of the line currently being examined. */
143 static uintmax_t current_line = 0;
144
145 /* If true, we have read EOF. */
146 static bool have_read_eof = false;
147
148 /* Name of output files. */
149 static char *volatile filename_space = NULL;
150
151 /* Prefix part of output file names. */
152 static char const *volatile prefix = NULL;
153
154 /* Suffix part of output file names. */
155 static char *volatile suffix = NULL;
156
157 /* Number of digits to use in output file names. */
158 static int volatile digits = 2;
159
160 /* Number of files created so far. */
161 static unsigned int volatile files_created = 0;
162
163 /* Number of bytes written to current file. */
164 static uintmax_t bytes_written;
165
166 /* Output file pointer. */
167 static FILE *output_stream = NULL;
168
169 /* Output file name. */
170 static char *output_filename = NULL;
171
172 /* Perhaps it would be cleaner to pass arg values instead of indexes. */
173 static char **global_argv;
174
175 /* If true, do not print the count of bytes in each output file. */
176 static bool suppress_count;
177
178 /* If true, remove output files on error. */
179 static bool volatile remove_files;
180
181 /* If true, remove all output files which have a zero length. */
182 static bool elide_empty_files;
183
184 /* The compiled pattern arguments, which determine how to split
185    the input file. */
186 static struct control *controls;
187
188 /* Number of elements in `controls'. */
189 static size_t control_used;
190
191 /* The set of signals that are caught.  */
192 static sigset_t caught_signals;
193
194 static struct option const longopts[] =
195 {
196   {"digits", required_argument, NULL, 'n'},
197   {"quiet", no_argument, NULL, 'q'},
198   {"silent", no_argument, NULL, 's'},
199   {"keep-files", no_argument, NULL, 'k'},
200   {"elide-empty-files", no_argument, NULL, 'z'},
201   {"prefix", required_argument, NULL, 'f'},
202   {"suffix-format", required_argument, NULL, 'b'},
203   {GETOPT_HELP_OPTION_DECL},
204   {GETOPT_VERSION_OPTION_DECL},
205   {NULL, 0, NULL, 0}
206 };
207
208 /* Optionally remove files created so far; then exit.
209    Called when an error detected. */
210
211 static void
212 cleanup (void)
213 {
214   sigset_t oldset;
215
216   close_output_file ();
217
218   sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
219   delete_all_files (false);
220   sigprocmask (SIG_SETMASK, &oldset, NULL);
221 }
222
223 static void cleanup_fatal (void) ATTRIBUTE_NORETURN;
224 static void
225 cleanup_fatal (void)
226 {
227   cleanup ();
228   exit (EXIT_FAILURE);
229 }
230
231 extern void
232 xalloc_die (void)
233 {
234   error (0, 0, "%s", _("memory exhausted"));
235   cleanup_fatal ();
236 }
237
238 static void
239 interrupt_handler (int sig)
240 {
241   if (! SA_NOCLDSTOP)
242     signal (sig, SIG_IGN);
243
244   delete_all_files (true);
245
246   signal (sig, SIG_DFL);
247   raise (sig);
248 }
249
250 /* Keep track of NUM bytes of a partial line in buffer START.
251    These bytes will be retrieved later when another large buffer is read.  */
252
253 static void
254 save_to_hold_area (char *start, size_t num)
255 {
256   free (hold_area);
257   hold_area = start;
258   hold_count = num;
259 }
260
261 /* Read up to MAX_N_BYTES bytes from the input stream into DEST.
262    Return the number of bytes read. */
263
264 static size_t
265 read_input (char *dest, size_t max_n_bytes)
266 {
267   size_t bytes_read;
268
269   if (max_n_bytes == 0)
270     return 0;
271
272   bytes_read = safe_read (STDIN_FILENO, dest, max_n_bytes);
273
274   if (bytes_read == 0)
275     have_read_eof = true;
276
277   if (bytes_read == SAFE_READ_ERROR)
278     {
279       error (0, errno, _("read error"));
280       cleanup_fatal ();
281     }
282
283   return bytes_read;
284 }
285
286 /* Initialize existing line record P. */
287
288 static void
289 clear_line_control (struct line *p)
290 {
291   p->used = 0;
292   p->insert_index = 0;
293   p->retrieve_index = 0;
294 }
295
296 /* Return a new, initialized line record. */
297
298 static struct line *
299 new_line_control (void)
300 {
301   struct line *p = xmalloc (sizeof *p);
302
303   p->next = NULL;
304   clear_line_control (p);
305
306   return p;
307 }
308
309 /* Record LINE_START, which is the address of the start of a line
310    of length LINE_LEN in the large buffer, in the lines buffer of B. */
311
312 static void
313 keep_new_line (struct buffer_record *b, char *line_start, size_t line_len)
314 {
315   struct line *l;
316
317   /* If there is no existing area to keep line info, get some. */
318   if (b->line_start == NULL)
319     b->line_start = b->curr_line = new_line_control ();
320
321   /* If existing area for lines is full, get more. */
322   if (b->curr_line->used == CTRL_SIZE)
323     {
324       b->curr_line->next = new_line_control ();
325       b->curr_line = b->curr_line->next;
326     }
327
328   l = b->curr_line;
329
330   /* Record the start of the line, and update counters. */
331   l->starts[l->insert_index].str = line_start;
332   l->starts[l->insert_index].len = line_len;
333   l->used++;
334   l->insert_index++;
335 }
336
337 /* Scan the buffer in B for newline characters
338    and record the line start locations and lengths in B.
339    Return the number of lines found in this buffer.
340
341    There may be an incomplete line at the end of the buffer;
342    a pointer is kept to this area, which will be used when
343    the next buffer is filled. */
344
345 static size_t
346 record_line_starts (struct buffer_record *b)
347 {
348   char *line_start;             /* Start of current line. */
349   char *line_end;               /* End of each line found. */
350   size_t bytes_left;            /* Length of incomplete last line. */
351   size_t lines;                 /* Number of lines found. */
352   size_t line_length;           /* Length of each line found. */
353
354   if (b->bytes_used == 0)
355     return 0;
356
357   lines = 0;
358   line_start = b->buffer;
359   bytes_left = b->bytes_used;
360
361   for (;;)
362     {
363       line_end = memchr (line_start, '\n', bytes_left);
364       if (line_end == NULL)
365         break;
366       line_length = line_end - line_start + 1;
367       keep_new_line (b, line_start, line_length);
368       bytes_left -= line_length;
369       line_start = line_end + 1;
370       lines++;
371     }
372
373   /* Check for an incomplete last line. */
374   if (bytes_left)
375     {
376       if (have_read_eof)
377         {
378           keep_new_line (b, line_start, bytes_left);
379           lines++;
380         }
381       else
382         save_to_hold_area (xmemdup (line_start, bytes_left), bytes_left);
383     }
384
385   b->num_lines = lines;
386   b->first_available = b->start_line = last_line_number + 1;
387   last_line_number += lines;
388
389   return lines;
390 }
391
392 /* Return a new buffer with room to store SIZE bytes, plus
393    an extra byte for safety. */
394
395 static struct buffer_record *
396 create_new_buffer (size_t size)
397 {
398   struct buffer_record *new_buffer = xmalloc (sizeof *new_buffer);
399
400   new_buffer->buffer = xmalloc (size + 1);
401
402   new_buffer->bytes_alloc = size;
403   new_buffer->line_start = new_buffer->curr_line = NULL;
404
405   return new_buffer;
406 }
407
408 /* Return a new buffer of at least MINSIZE bytes.  If a buffer of at
409    least that size is currently free, use it, otherwise create a new one. */
410
411 static struct buffer_record *
412 get_new_buffer (size_t min_size)
413 {
414   struct buffer_record *new_buffer; /* Buffer to return. */
415   size_t alloc_size;    /* Actual size that will be requested. */
416
417   alloc_size = START_SIZE;
418   if (alloc_size < min_size)
419     {
420       size_t s = min_size - alloc_size + INCR_SIZE - 1;
421       alloc_size += s - s % INCR_SIZE;
422     }
423
424   new_buffer = create_new_buffer (alloc_size);
425
426   new_buffer->num_lines = 0;
427   new_buffer->bytes_used = 0;
428   new_buffer->start_line = new_buffer->first_available = last_line_number + 1;
429   new_buffer->next = NULL;
430
431   return new_buffer;
432 }
433
434 static void
435 free_buffer (struct buffer_record *buf)
436 {
437   free (buf->buffer);
438   buf->buffer = NULL;
439 }
440
441 /* Append buffer BUF to the linked list of buffers that contain
442    some data yet to be processed. */
443
444 static void
445 save_buffer (struct buffer_record *buf)
446 {
447   struct buffer_record *p;
448
449   buf->next = NULL;
450   buf->curr_line = buf->line_start;
451
452   if (head == NULL)
453     head = buf;
454   else
455     {
456       for (p = head; p->next; p = p->next)
457         /* Do nothing. */ ;
458       p->next = buf;
459     }
460 }
461
462 /* Fill a buffer of input.
463
464    Set the initial size of the buffer to a default.
465    Fill the buffer (from the hold area and input stream)
466    and find the individual lines.
467    If no lines are found (the buffer is too small to hold the next line),
468    release the current buffer (whose contents would have been put in the
469    hold area) and repeat the process with another large buffer until at least
470    one entire line has been read.
471
472    Return true if a new buffer was obtained, otherwise false
473    (in which case end-of-file must have been encountered). */
474
475 static bool
476 load_buffer (void)
477 {
478   struct buffer_record *b;
479   size_t bytes_wanted = START_SIZE; /* Minimum buffer size. */
480   size_t bytes_avail;           /* Size of new buffer created. */
481   size_t lines_found;           /* Number of lines in this new buffer. */
482   char *p;                      /* Place to load into buffer. */
483
484   if (have_read_eof)
485     return false;
486
487   /* We must make the buffer at least as large as the amount of data
488      in the partial line left over from the last call. */
489   if (bytes_wanted < hold_count)
490     bytes_wanted = hold_count;
491
492   while (1)
493     {
494       b = get_new_buffer (bytes_wanted);
495       bytes_avail = b->bytes_alloc; /* Size of buffer returned. */
496       p = b->buffer;
497
498       /* First check the `holding' area for a partial line. */
499       if (hold_count)
500         {
501           memcpy (p, hold_area, hold_count);
502           p += hold_count;
503           b->bytes_used += hold_count;
504           bytes_avail -= hold_count;
505           hold_count = 0;
506         }
507
508       b->bytes_used += read_input (p, bytes_avail);
509
510       lines_found = record_line_starts (b);
511       if (!lines_found)
512         free_buffer (b);
513
514       if (lines_found || have_read_eof)
515         break;
516
517       if (xalloc_oversized (2, b->bytes_alloc))
518         xalloc_die ();
519       bytes_wanted = 2 * b->bytes_alloc;
520       free_buffer (b);
521       free (b);
522     }
523
524   if (lines_found)
525     save_buffer (b);
526   else
527     free (b);
528
529   return lines_found != 0;
530 }
531
532 /* Return the line number of the first line that has not yet been retrieved. */
533
534 static uintmax_t
535 get_first_line_in_buffer (void)
536 {
537   if (head == NULL && !load_buffer ())
538     error (EXIT_FAILURE, errno, _("input disappeared"));
539
540   return head->first_available;
541 }
542
543 /* Return a pointer to the logical first line in the buffer and make the
544    next line the logical first line.
545    Return NULL if there is no more input. */
546
547 static struct cstring *
548 remove_line (void)
549 {
550   /* If non-NULL, this is the buffer for which the previous call
551      returned the final line.  So now, presuming that line has been
552      processed, we can free the buffer and reset this pointer.  */
553   static struct buffer_record *prev_buf = NULL;
554
555   struct cstring *line;         /* Return value. */
556   struct line *l;               /* For convenience. */
557
558   if (prev_buf)
559     {
560       free_buffer (prev_buf);
561       prev_buf = NULL;
562     }
563
564   if (head == NULL && !load_buffer ())
565     return NULL;
566
567   if (current_line < head->first_available)
568     current_line = head->first_available;
569
570   ++(head->first_available);
571
572   l = head->curr_line;
573
574   line = &l->starts[l->retrieve_index];
575
576   /* Advance index to next line. */
577   if (++l->retrieve_index == l->used)
578     {
579       /* Go on to the next line record. */
580       head->curr_line = l->next;
581       if (head->curr_line == NULL || head->curr_line->used == 0)
582         {
583           /* Go on to the next data block.
584              but first record the current one so we can free it
585              once the line we're returning has been processed.  */
586           prev_buf = head;
587           head = head->next;
588         }
589     }
590
591   return line;
592 }
593
594 /* Search the buffers for line LINENUM, reading more input if necessary.
595    Return a pointer to the line, or NULL if it is not found in the file. */
596
597 static struct cstring *
598 find_line (uintmax_t linenum)
599 {
600   struct buffer_record *b;
601
602   if (head == NULL && !load_buffer ())
603     return NULL;
604
605   if (linenum < head->start_line)
606     return NULL;
607
608   for (b = head;;)
609     {
610       if (linenum < b->start_line + b->num_lines)
611         {
612           /* The line is in this buffer. */
613           struct line *l;
614           size_t offset;        /* How far into the buffer the line is. */
615
616           l = b->line_start;
617           offset = linenum - b->start_line;
618           /* Find the control record. */
619           while (offset >= CTRL_SIZE)
620             {
621               l = l->next;
622               offset -= CTRL_SIZE;
623             }
624           return &l->starts[offset];
625         }
626       if (b->next == NULL && !load_buffer ())
627         return NULL;
628       b = b->next;              /* Try the next data block. */
629     }
630 }
631
632 /* Return true if at least one more line is available for input. */
633
634 static bool
635 no_more_lines (void)
636 {
637   return find_line (current_line + 1) == NULL;
638 }
639
640 /* Open NAME as standard input.  */
641
642 static void
643 set_input_file (const char *name)
644 {
645   if (! STREQ (name, "-") && fd_reopen (STDIN_FILENO, name, O_RDONLY, 0) < 0)
646     error (EXIT_FAILURE, errno, _("cannot open %s for reading"), quote (name));
647 }
648
649 /* Write all lines from the beginning of the buffer up to, but
650    not including, line LAST_LINE, to the current output file.
651    If IGNORE is true, do not output lines selected here.
652    ARGNUM is the index in ARGV of the current pattern. */
653
654 static void
655 write_to_file (uintmax_t last_line, bool ignore, int argnum)
656 {
657   struct cstring *line;
658   uintmax_t first_line;         /* First available input line. */
659   uintmax_t lines;              /* Number of lines to output. */
660   uintmax_t i;
661
662   first_line = get_first_line_in_buffer ();
663
664   if (first_line > last_line)
665     {
666       error (0, 0, _("%s: line number out of range"), global_argv[argnum]);
667       cleanup_fatal ();
668     }
669
670   lines = last_line - first_line;
671
672   for (i = 0; i < lines; i++)
673     {
674       line = remove_line ();
675       if (line == NULL)
676         {
677           error (0, 0, _("%s: line number out of range"), global_argv[argnum]);
678           cleanup_fatal ();
679         }
680       if (!ignore)
681         save_line_to_file (line);
682     }
683 }
684
685 /* Output any lines left after all regexps have been processed. */
686
687 static void
688 dump_rest_of_file (void)
689 {
690   struct cstring *line;
691
692   while ((line = remove_line ()) != NULL)
693     save_line_to_file (line);
694 }
695
696 /* Handle an attempt to read beyond EOF under the control of record P,
697    on iteration REPETITION if nonzero. */
698
699 static void handle_line_error (const struct control *, uintmax_t)
700      ATTRIBUTE_NORETURN;
701 static void
702 handle_line_error (const struct control *p, uintmax_t repetition)
703 {
704   char buf[INT_BUFSIZE_BOUND (uintmax_t)];
705
706   fprintf (stderr, _("%s: %s: line number out of range"),
707            program_name, quote (umaxtostr (p->lines_required, buf)));
708   if (repetition)
709     fprintf (stderr, _(" on repetition %s\n"), umaxtostr (repetition, buf));
710   else
711     fprintf (stderr, "\n");
712
713   cleanup_fatal ();
714 }
715
716 /* Determine the line number that marks the end of this file,
717    then get those lines and save them to the output file.
718    P is the control record.
719    REPETITION is the repetition number. */
720
721 static void
722 process_line_count (const struct control *p, uintmax_t repetition)
723 {
724   uintmax_t linenum;
725   uintmax_t last_line_to_save = p->lines_required * (repetition + 1);
726   struct cstring *line;
727
728   create_output_file ();
729
730   linenum = get_first_line_in_buffer ();
731
732   while (linenum++ < last_line_to_save)
733     {
734       line = remove_line ();
735       if (line == NULL)
736         handle_line_error (p, repetition);
737       save_line_to_file (line);
738     }
739
740   close_output_file ();
741
742   /* Ensure that the line number specified is not 1 greater than
743      the number of lines in the file. */
744   if (no_more_lines ())
745     handle_line_error (p, repetition);
746 }
747
748 static void regexp_error (struct control *, uintmax_t, bool) ATTRIBUTE_NORETURN;
749 static void
750 regexp_error (struct control *p, uintmax_t repetition, bool ignore)
751 {
752   fprintf (stderr, _("%s: %s: match not found"),
753            program_name, quote (global_argv[p->argnum]));
754
755   if (repetition)
756     {
757       char buf[INT_BUFSIZE_BOUND (uintmax_t)];
758       fprintf (stderr, _(" on repetition %s\n"), umaxtostr (repetition, buf));
759     }
760   else
761     fprintf (stderr, "\n");
762
763   if (!ignore)
764     {
765       dump_rest_of_file ();
766       close_output_file ();
767     }
768   cleanup_fatal ();
769 }
770
771 /* Read the input until a line matches the regexp in P, outputting
772    it unless P->IGNORE is true.
773    REPETITION is this repeat-count; 0 means the first time. */
774
775 static void
776 process_regexp (struct control *p, uintmax_t repetition)
777 {
778   struct cstring *line;         /* From input file. */
779   size_t line_len;              /* To make "$" in regexps work. */
780   uintmax_t break_line;         /* First line number of next file. */
781   bool ignore = p->ignore;      /* If true, skip this section. */
782   regoff_t ret;
783
784   if (!ignore)
785     create_output_file ();
786
787   /* If there is no offset for the regular expression, or
788      it is positive, then it is not necessary to buffer the lines. */
789
790   if (p->offset >= 0)
791     {
792       for (;;)
793         {
794           line = find_line (++current_line);
795           if (line == NULL)
796             {
797               if (p->repeat_forever)
798                 {
799                   if (!ignore)
800                     {
801                       dump_rest_of_file ();
802                       close_output_file ();
803                     }
804                   exit (EXIT_SUCCESS);
805                 }
806               else
807                 regexp_error (p, repetition, ignore);
808             }
809           line_len = line->len;
810           if (line->str[line_len - 1] == '\n')
811             line_len--;
812           ret = re_search (&p->re_compiled, line->str, line_len,
813                            0, line_len, NULL);
814           if (ret == -2)
815             {
816               error (0, 0, _("error in regular expression search"));
817               cleanup_fatal ();
818             }
819           if (ret == -1)
820             {
821               line = remove_line ();
822               if (!ignore)
823                 save_line_to_file (line);
824             }
825           else
826             break;
827         }
828     }
829   else
830     {
831       /* Buffer the lines. */
832       for (;;)
833         {
834           line = find_line (++current_line);
835           if (line == NULL)
836             {
837               if (p->repeat_forever)
838                 {
839                   if (!ignore)
840                     {
841                       dump_rest_of_file ();
842                       close_output_file ();
843                     }
844                   exit (EXIT_SUCCESS);
845                 }
846               else
847                 regexp_error (p, repetition, ignore);
848             }
849           line_len = line->len;
850           if (line->str[line_len - 1] == '\n')
851             line_len--;
852           ret = re_search (&p->re_compiled, line->str, line_len,
853                            0, line_len, NULL);
854           if (ret == -2)
855             {
856               error (0, 0, _("error in regular expression search"));
857               cleanup_fatal ();
858             }
859           if (ret != -1)
860             break;
861         }
862     }
863
864   /* Account for any offset from this regexp. */
865   break_line = current_line + p->offset;
866
867   write_to_file (break_line, ignore, p->argnum);
868
869   if (!ignore)
870     close_output_file ();
871
872   if (p->offset > 0)
873     current_line = break_line;
874 }
875
876 /* Split the input file according to the control records we have built. */
877
878 static void
879 split_file (void)
880 {
881   size_t i;
882
883   for (i = 0; i < control_used; i++)
884     {
885       uintmax_t j;
886       if (controls[i].regexpr)
887         {
888           for (j = 0; (controls[i].repeat_forever
889                        || j <= controls[i].repeat); j++)
890             process_regexp (&controls[i], j);
891         }
892       else
893         {
894           for (j = 0; (controls[i].repeat_forever
895                        || j <= controls[i].repeat); j++)
896             process_line_count (&controls[i], j);
897         }
898     }
899
900   create_output_file ();
901   dump_rest_of_file ();
902   close_output_file ();
903 }
904
905 /* Return the name of output file number NUM.
906
907    This function is called from a signal handler, so it should invoke
908    only reentrant functions that are async-signal-safe.  POSIX does
909    not guarantee this for the functions called below, but we don't
910    know of any hosts where this implementation isn't safe.  */
911
912 static char *
913 make_filename (unsigned int num)
914 {
915   strcpy (filename_space, prefix);
916   if (suffix)
917     sprintf (filename_space + strlen (prefix), suffix, num);
918   else
919     sprintf (filename_space + strlen (prefix), "%0*u", digits, num);
920   return filename_space;
921 }
922
923 /* Create the next output file. */
924
925 static void
926 create_output_file (void)
927 {
928   sigset_t oldset;
929   bool fopen_ok;
930   int fopen_errno;
931
932   output_filename = make_filename (files_created);
933
934   /* Create the output file in a critical section, to avoid races.  */
935   sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
936   output_stream = fopen (output_filename, "w");
937   fopen_ok = (output_stream != NULL);
938   fopen_errno = errno;
939   files_created += fopen_ok;
940   sigprocmask (SIG_SETMASK, &oldset, NULL);
941
942   if (! fopen_ok)
943     {
944       error (0, fopen_errno, "%s", output_filename);
945       cleanup_fatal ();
946     }
947   bytes_written = 0;
948 }
949
950 /* If requested, delete all the files we have created.  This function
951    must be called only from critical sections.  */
952
953 static void
954 delete_all_files (bool in_signal_handler)
955 {
956   unsigned int i;
957
958   if (! remove_files)
959     return;
960
961   for (i = 0; i < files_created; i++)
962     {
963       const char *name = make_filename (i);
964       if (unlink (name) != 0 && !in_signal_handler)
965         error (0, errno, "%s", name);
966     }
967
968   files_created = 0;
969 }
970
971 /* Close the current output file and print the count
972    of characters in this file. */
973
974 static void
975 close_output_file (void)
976 {
977   if (output_stream)
978     {
979       if (ferror (output_stream))
980         {
981           error (0, 0, _("write error for %s"), quote (output_filename));
982           output_stream = NULL;
983           cleanup_fatal ();
984         }
985       if (fclose (output_stream) != 0)
986         {
987           error (0, errno, "%s", output_filename);
988           output_stream = NULL;
989           cleanup_fatal ();
990         }
991       if (bytes_written == 0 && elide_empty_files)
992         {
993           sigset_t oldset;
994           bool unlink_ok;
995           int unlink_errno;
996
997           /* Remove the output file in a critical section, to avoid races.  */
998           sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
999           unlink_ok = (unlink (output_filename) == 0);
1000           unlink_errno = errno;
1001           files_created -= unlink_ok;
1002           sigprocmask (SIG_SETMASK, &oldset, NULL);
1003
1004           if (! unlink_ok)
1005             error (0, unlink_errno, "%s", output_filename);
1006         }
1007       else
1008         {
1009           if (!suppress_count)
1010             {
1011               char buf[INT_BUFSIZE_BOUND (uintmax_t)];
1012               fprintf (stdout, "%s\n", umaxtostr (bytes_written, buf));
1013             }
1014         }
1015       output_stream = NULL;
1016     }
1017 }
1018
1019 /* Save line LINE to the output file and
1020    increment the character count for the current file. */
1021
1022 static void
1023 save_line_to_file (const struct cstring *line)
1024 {
1025   fwrite (line->str, sizeof (char), line->len, output_stream);
1026   bytes_written += line->len;
1027 }
1028
1029 /* Return a new, initialized control record. */
1030
1031 static struct control *
1032 new_control_record (void)
1033 {
1034   static size_t control_allocated = 0; /* Total space allocated. */
1035   struct control *p;
1036
1037   if (control_used == control_allocated)
1038     controls = X2NREALLOC (controls, &control_allocated);
1039   p = &controls[control_used++];
1040   p->regexpr = false;
1041   p->repeat = 0;
1042   p->repeat_forever = false;
1043   p->lines_required = 0;
1044   p->offset = 0;
1045   return p;
1046 }
1047
1048 /* Check if there is a numeric offset after a regular expression.
1049    STR is the entire command line argument.
1050    P is the control record for this regular expression.
1051    NUM is the numeric part of STR. */
1052
1053 static void
1054 check_for_offset (struct control *p, const char *str, const char *num)
1055 {
1056   if (xstrtoimax (num, NULL, 10, &p->offset, "") != LONGINT_OK)
1057     error (EXIT_FAILURE, 0, _("%s: integer expected after delimiter"), str);
1058 }
1059
1060 /* Given that the first character of command line arg STR is '{',
1061    make sure that the rest of the string is a valid repeat count
1062    and store its value in P.
1063    ARGNUM is the ARGV index of STR. */
1064
1065 static void
1066 parse_repeat_count (int argnum, struct control *p, char *str)
1067 {
1068   uintmax_t val;
1069   char *end;
1070
1071   end = str + strlen (str) - 1;
1072   if (*end != '}')
1073     error (EXIT_FAILURE, 0, _("%s: `}' is required in repeat count"), str);
1074   *end = '\0';
1075
1076   if (str+1 == end-1 && *(str+1) == '*')
1077     p->repeat_forever = true;
1078   else
1079     {
1080       if (xstrtoumax (str + 1, NULL, 10, &val, "") != LONGINT_OK)
1081         {
1082           error (EXIT_FAILURE, 0,
1083                  _("%s}: integer required between `{' and `}'"),
1084                  global_argv[argnum]);
1085         }
1086       p->repeat = val;
1087     }
1088
1089   *end = '}';
1090 }
1091
1092 /* Extract the regular expression from STR and check for a numeric offset.
1093    STR should start with the regexp delimiter character.
1094    Return a new control record for the regular expression.
1095    ARGNUM is the ARGV index of STR.
1096    Unless IGNORE is true, mark these lines for output. */
1097
1098 static struct control *
1099 extract_regexp (int argnum, bool ignore, char const *str)
1100 {
1101   size_t len;                   /* Number of bytes in this regexp. */
1102   char delim = *str;
1103   char const *closing_delim;
1104   struct control *p;
1105   const char *err;
1106
1107   closing_delim = strrchr (str + 1, delim);
1108   if (closing_delim == NULL)
1109     error (EXIT_FAILURE, 0,
1110            _("%s: closing delimiter `%c' missing"), str, delim);
1111
1112   len = closing_delim - str - 1;
1113   p = new_control_record ();
1114   p->argnum = argnum;
1115   p->ignore = ignore;
1116
1117   p->regexpr = true;
1118   p->re_compiled.buffer = NULL;
1119   p->re_compiled.allocated = 0;
1120   p->re_compiled.fastmap = xmalloc (UCHAR_MAX + 1);
1121   p->re_compiled.translate = NULL;
1122   re_syntax_options =
1123     RE_SYNTAX_POSIX_BASIC & ~RE_CONTEXT_INVALID_DUP & ~RE_NO_EMPTY_RANGES;
1124   err = re_compile_pattern (str + 1, len, &p->re_compiled);
1125   if (err)
1126     {
1127       error (0, 0, _("%s: invalid regular expression: %s"), str, err);
1128       cleanup_fatal ();
1129     }
1130
1131   if (closing_delim[1])
1132     check_for_offset (p, str, closing_delim + 1);
1133
1134   return p;
1135 }
1136
1137 /* Extract the break patterns from args START through ARGC - 1 of ARGV.
1138    After each pattern, check if the next argument is a repeat count. */
1139
1140 static void
1141 parse_patterns (int argc, int start, char **argv)
1142 {
1143   int i;                        /* Index into ARGV. */
1144   struct control *p;            /* New control record created. */
1145   uintmax_t val;
1146   static uintmax_t last_val = 0;
1147
1148   for (i = start; i < argc; i++)
1149     {
1150       if (*argv[i] == '/' || *argv[i] == '%')
1151         {
1152           p = extract_regexp (i, *argv[i] == '%', argv[i]);
1153         }
1154       else
1155         {
1156           p = new_control_record ();
1157           p->argnum = i;
1158
1159           if (xstrtoumax (argv[i], NULL, 10, &val, "") != LONGINT_OK)
1160             error (EXIT_FAILURE, 0, _("%s: invalid pattern"), argv[i]);
1161           if (val == 0)
1162             error (EXIT_FAILURE, 0,
1163                    _("%s: line number must be greater than zero"),
1164                    argv[i]);
1165           if (val < last_val)
1166             {
1167               char buf[INT_BUFSIZE_BOUND (uintmax_t)];
1168               error (EXIT_FAILURE, 0,
1169                _("line number %s is smaller than preceding line number, %s"),
1170                      quote (argv[i]), umaxtostr (last_val, buf));
1171             }
1172
1173           if (val == last_val)
1174             error (0, 0,
1175            _("warning: line number %s is the same as preceding line number"),
1176                    quote (argv[i]));
1177
1178           last_val = val;
1179
1180           p->lines_required = val;
1181         }
1182
1183       if (i + 1 < argc && *argv[i + 1] == '{')
1184         {
1185           /* We have a repeat count. */
1186           i++;
1187           parse_repeat_count (i, p, argv[i]);
1188         }
1189     }
1190 }
1191
1192 static unsigned int
1193 get_format_flags (char **format_ptr)
1194 {
1195   unsigned int count = 0;
1196
1197   for (; **format_ptr; (*format_ptr)++)
1198     {
1199       switch (**format_ptr)
1200         {
1201         case '-':
1202           break;
1203
1204         case '+':
1205         case ' ':
1206           count |= 1;
1207           break;
1208
1209         case '#':
1210           count |= 2;   /* Allow for 0x prefix preceding an `x' conversion.  */
1211           break;
1212
1213         default:
1214           return count;
1215         }
1216     }
1217   return count;
1218 }
1219
1220 static size_t
1221 get_format_width (char **format_ptr)
1222 {
1223   unsigned long int val = 0;
1224
1225   if (ISDIGIT (**format_ptr)
1226       && (xstrtoul (*format_ptr, format_ptr, 10, &val, NULL) != LONGINT_OK
1227           || SIZE_MAX < val))
1228     error (EXIT_FAILURE, 0, _("invalid format width"));
1229
1230   /* Allow for enough octal digits to represent the value of UINT_MAX,
1231      even if the field width is less than that.  */
1232   return MAX (val, (sizeof (unsigned int) * CHAR_BIT + 2) / 3);
1233 }
1234
1235 static size_t
1236 get_format_prec (char **format_ptr)
1237 {
1238   if (**format_ptr != '.')
1239     return 0;
1240   (*format_ptr)++;
1241
1242   if (! ISDIGIT (**format_ptr))
1243     return 0;
1244   else
1245     {
1246       unsigned long int val;
1247       if (xstrtoul (*format_ptr, format_ptr, 10, &val, NULL) != LONGINT_OK
1248           || SIZE_MAX < val)
1249         error (EXIT_FAILURE, 0, _("invalid format precision"));
1250       return val;
1251     }
1252 }
1253
1254 static void
1255 get_format_conv_type (char **format_ptr)
1256 {
1257   unsigned char ch = *(*format_ptr)++;
1258
1259   switch (ch)
1260     {
1261     case 'd':
1262     case 'i':
1263     case 'o':
1264     case 'u':
1265     case 'x':
1266     case 'X':
1267       break;
1268
1269     case 0:
1270       error (EXIT_FAILURE, 0, _("missing conversion specifier in suffix"));
1271       break;
1272
1273     default:
1274       if (isprint (ch))
1275         error (EXIT_FAILURE, 0,
1276                _("invalid conversion specifier in suffix: %c"), ch);
1277       else
1278         error (EXIT_FAILURE, 0,
1279                _("invalid conversion specifier in suffix: \\%.3o"), ch);
1280     }
1281 }
1282
1283 static size_t
1284 max_out (char *format)
1285 {
1286   size_t out_count = 0;
1287   bool percent = false;
1288
1289   while (*format)
1290     {
1291       if (*format++ != '%')
1292         out_count++;
1293       else if (*format == '%')
1294         {
1295           format++;
1296           out_count++;
1297         }
1298       else
1299         {
1300           if (percent)
1301             error (EXIT_FAILURE, 0,
1302                    _("too many %% conversion specifications in suffix"));
1303           percent = true;
1304           out_count += get_format_flags (&format);
1305           {
1306             size_t width = get_format_width (&format);
1307             size_t prec = get_format_prec (&format);
1308
1309             out_count += MAX (width, prec);
1310           }
1311           get_format_conv_type (&format);
1312         }
1313     }
1314
1315   if (! percent)
1316     error (EXIT_FAILURE, 0,
1317            _("missing %% conversion specification in suffix"));
1318
1319   return out_count;
1320 }
1321
1322 int
1323 main (int argc, char **argv)
1324 {
1325   int optc;
1326   unsigned long int val;
1327
1328   initialize_main (&argc, &argv);
1329   set_program_name (argv[0]);
1330   setlocale (LC_ALL, "");
1331   bindtextdomain (PACKAGE, LOCALEDIR);
1332   textdomain (PACKAGE);
1333
1334   atexit (close_stdout);
1335
1336   global_argv = argv;
1337   controls = NULL;
1338   control_used = 0;
1339   suppress_count = false;
1340   remove_files = true;
1341   prefix = DEFAULT_PREFIX;
1342
1343   while ((optc = getopt_long (argc, argv, "f:b:kn:sqz", longopts, NULL)) != -1)
1344     switch (optc)
1345       {
1346       case 'f':
1347         prefix = optarg;
1348         break;
1349
1350       case 'b':
1351         suffix = optarg;
1352         break;
1353
1354       case 'k':
1355         remove_files = false;
1356         break;
1357
1358       case 'n':
1359         if (xstrtoul (optarg, NULL, 10, &val, "") != LONGINT_OK
1360             || val > INT_MAX)
1361           error (EXIT_FAILURE, 0, _("%s: invalid number"), optarg);
1362         digits = val;
1363         break;
1364
1365       case 's':
1366       case 'q':
1367         suppress_count = true;
1368         break;
1369
1370       case 'z':
1371         elide_empty_files = true;
1372         break;
1373
1374       case_GETOPT_HELP_CHAR;
1375
1376       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1377
1378       default:
1379         usage (EXIT_FAILURE);
1380       }
1381
1382   if (argc - optind < 2)
1383     {
1384       if (argc <= optind)
1385         error (0, 0, _("missing operand"));
1386       else
1387         error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
1388       usage (EXIT_FAILURE);
1389     }
1390
1391   if (suffix)
1392     filename_space = xmalloc (strlen (prefix) + max_out (suffix) + 2);
1393   else
1394     filename_space = xmalloc (strlen (prefix) + digits + 2);
1395
1396   set_input_file (argv[optind++]);
1397
1398   parse_patterns (argc, optind, argv);
1399
1400   {
1401     int i;
1402     static int const sig[] =
1403       {
1404         /* The usual suspects.  */
1405         SIGALRM, SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM,
1406 #ifdef SIGPOLL
1407         SIGPOLL,
1408 #endif
1409 #ifdef SIGPROF
1410         SIGPROF,
1411 #endif
1412 #ifdef SIGVTALRM
1413         SIGVTALRM,
1414 #endif
1415 #ifdef SIGXCPU
1416         SIGXCPU,
1417 #endif
1418 #ifdef SIGXFSZ
1419         SIGXFSZ,
1420 #endif
1421       };
1422     enum { nsigs = sizeof sig / sizeof sig[0] };
1423
1424 #if SA_NOCLDSTOP
1425     struct sigaction act;
1426
1427     sigemptyset (&caught_signals);
1428     for (i = 0; i < nsigs; i++)
1429       {
1430         sigaction (sig[i], NULL, &act);
1431         if (act.sa_handler != SIG_IGN)
1432           sigaddset (&caught_signals, sig[i]);
1433       }
1434
1435     act.sa_handler = interrupt_handler;
1436     act.sa_mask = caught_signals;
1437     act.sa_flags = 0;
1438
1439     for (i = 0; i < nsigs; i++)
1440       if (sigismember (&caught_signals, sig[i]))
1441         sigaction (sig[i], &act, NULL);
1442 #else
1443     for (i = 0; i < nsigs; i++)
1444       if (signal (sig[i], SIG_IGN) != SIG_IGN)
1445         {
1446           signal (sig[i], interrupt_handler);
1447           siginterrupt (sig[i], 1);
1448         }
1449 #endif
1450   }
1451
1452   split_file ();
1453
1454   if (close (STDIN_FILENO) != 0)
1455     {
1456       error (0, errno, _("read error"));
1457       cleanup_fatal ();
1458     }
1459
1460   exit (EXIT_SUCCESS);
1461 }
1462
1463 void
1464 usage (int status)
1465 {
1466   if (status != EXIT_SUCCESS)
1467     fprintf (stderr, _("Try `%s --help' for more information.\n"),
1468              program_name);
1469   else
1470     {
1471       printf (_("\
1472 Usage: %s [OPTION]... FILE PATTERN...\n\
1473 "),
1474               program_name);
1475       fputs (_("\
1476 Output pieces of FILE separated by PATTERN(s) to files `xx00', `xx01', ...,\n\
1477 and output byte counts of each piece to standard output.\n\
1478 \n\
1479 "), stdout);
1480       fputs (_("\
1481 Mandatory arguments to long options are mandatory for short options too.\n\
1482 "), stdout);
1483       fputs (_("\
1484   -b, --suffix-format=FORMAT  use sprintf FORMAT instead of %02d\n\
1485   -f, --prefix=PREFIX        use PREFIX instead of `xx'\n\
1486   -k, --keep-files           do not remove output files on errors\n\
1487 "), stdout);
1488       fputs (_("\
1489   -n, --digits=DIGITS        use specified number of digits instead of 2\n\
1490   -s, --quiet, --silent      do not print counts of output file sizes\n\
1491   -z, --elide-empty-files    remove empty output files\n\
1492 "), stdout);
1493       fputs (HELP_OPTION_DESCRIPTION, stdout);
1494       fputs (VERSION_OPTION_DESCRIPTION, stdout);
1495       fputs (_("\
1496 \n\
1497 Read standard input if FILE is -.  Each PATTERN may be:\n\
1498 "), stdout);
1499       fputs (_("\
1500 \n\
1501   INTEGER            copy up to but not including specified line number\n\
1502   /REGEXP/[OFFSET]   copy up to but not including a matching line\n\
1503   %REGEXP%[OFFSET]   skip to, but not including a matching line\n\
1504   {INTEGER}          repeat the previous pattern specified number of times\n\
1505   {*}                repeat the previous pattern as many times as possible\n\
1506 \n\
1507 A line OFFSET is a required `+' or `-' followed by a positive integer.\n\
1508 "), stdout);
1509       emit_bug_reporting_address ();
1510     }
1511   exit (status);
1512 }