61ae265d83a63e905c0386f12faf63bc7b1ff5fd
[platform/upstream/coreutils.git] / src / split.c
1 /* split.c -- split a file into pieces.
2    Copyright (C) 1988, 1991, 1995-2010 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 <stdio.h>
26 #include <getopt.h>
27 #include <sys/types.h>
28
29 #include "system.h"
30 #include "error.h"
31 #include "fd-reopen.h"
32 #include "fcntl--.h"
33 #include "full-read.h"
34 #include "full-write.h"
35 #include "quote.h"
36 #include "xfreopen.h"
37 #include "xstrtol.h"
38
39 /* The official name of this program (e.g., no `g' prefix).  */
40 #define PROGRAM_NAME "split"
41
42 #define AUTHORS \
43   proper_name_utf8 ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \
44   proper_name ("Richard M. Stallman")
45
46 #define DEFAULT_SUFFIX_LENGTH 2
47
48 /* Base name of output files.  */
49 static char const *outbase;
50
51 /* Name of output files.  */
52 static char *outfile;
53
54 /* Pointer to the end of the prefix in OUTFILE.
55    Suffixes are inserted here.  */
56 static char *outfile_mid;
57
58 /* Length of OUTFILE's suffix.  */
59 static size_t suffix_length = DEFAULT_SUFFIX_LENGTH;
60
61 /* Alphabet of characters to use in suffix.  */
62 static char const *suffix_alphabet = "abcdefghijklmnopqrstuvwxyz";
63
64 /* Name of input file.  May be "-".  */
65 static char *infile;
66
67 /* Descriptor on which output file is open.  */
68 static int output_desc;
69
70 /* If true, print a diagnostic on standard error just before each
71    output file is opened. */
72 static bool verbose;
73
74 /* For long options that have no equivalent short option, use a
75    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
76 enum
77 {
78   VERBOSE_OPTION = CHAR_MAX + 1
79 };
80
81 static struct option const longopts[] =
82 {
83   {"bytes", required_argument, NULL, 'b'},
84   {"lines", required_argument, NULL, 'l'},
85   {"line-bytes", required_argument, NULL, 'C'},
86   {"suffix-length", required_argument, NULL, 'a'},
87   {"numeric-suffixes", no_argument, NULL, 'd'},
88   {"verbose", no_argument, NULL, VERBOSE_OPTION},
89   {GETOPT_HELP_OPTION_DECL},
90   {GETOPT_VERSION_OPTION_DECL},
91   {NULL, 0, NULL, 0}
92 };
93
94 void
95 usage (int status)
96 {
97   if (status != EXIT_SUCCESS)
98     fprintf (stderr, _("Try `%s --help' for more information.\n"),
99              program_name);
100   else
101     {
102       printf (_("\
103 Usage: %s [OPTION]... [INPUT [PREFIX]]\n\
104 "),
105               program_name);
106     fputs (_("\
107 Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default\n\
108 size is 1000 lines, and default PREFIX is `x'.  With no INPUT, or when INPUT\n\
109 is -, read standard input.\n\
110 \n\
111 "), stdout);
112       fputs (_("\
113 Mandatory arguments to long options are mandatory for short options too.\n\
114 "), stdout);
115       fprintf (stdout, _("\
116   -a, --suffix-length=N   use suffixes of length N (default %d)\n\
117   -b, --bytes=SIZE        put SIZE bytes per output file\n\
118   -C, --line-bytes=SIZE   put at most SIZE bytes of lines per output file\n\
119   -d, --numeric-suffixes  use numeric suffixes instead of alphabetic\n\
120   -l, --lines=NUMBER      put NUMBER lines per output file\n\
121 "), DEFAULT_SUFFIX_LENGTH);
122       fputs (_("\
123       --verbose           print a diagnostic just before each\n\
124                             output file is opened\n\
125 "), stdout);
126       fputs (HELP_OPTION_DESCRIPTION, stdout);
127       fputs (VERSION_OPTION_DESCRIPTION, stdout);
128       emit_size_note ();
129       emit_ancillary_info ();
130     }
131   exit (status);
132 }
133
134 /* Compute the next sequential output file name and store it into the
135    string `outfile'.  */
136
137 static void
138 next_file_name (void)
139 {
140   /* Index in suffix_alphabet of each character in the suffix.  */
141   static size_t *sufindex;
142
143   if (! outfile)
144     {
145       /* Allocate and initialize the first file name.  */
146
147       size_t outbase_length = strlen (outbase);
148       size_t outfile_length = outbase_length + suffix_length;
149       if (outfile_length + 1 < outbase_length)
150         xalloc_die ();
151       outfile = xmalloc (outfile_length + 1);
152       outfile_mid = outfile + outbase_length;
153       memcpy (outfile, outbase, outbase_length);
154       memset (outfile_mid, suffix_alphabet[0], suffix_length);
155       outfile[outfile_length] = 0;
156       sufindex = xcalloc (suffix_length, sizeof *sufindex);
157
158 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
159       /* POSIX requires that if the output file name is too long for
160          its directory, `split' must fail without creating any files.
161          This must be checked for explicitly on operating systems that
162          silently truncate file names.  */
163       {
164         char *dir = dir_name (outfile);
165         long name_max = pathconf (dir, _PC_NAME_MAX);
166         if (0 <= name_max && name_max < base_len (last_component (outfile)))
167           error (EXIT_FAILURE, ENAMETOOLONG, "%s", outfile);
168         free (dir);
169       }
170 #endif
171     }
172   else
173     {
174       /* Increment the suffix in place, if possible.  */
175
176       size_t i = suffix_length;
177       while (i-- != 0)
178         {
179           sufindex[i]++;
180           outfile_mid[i] = suffix_alphabet[sufindex[i]];
181           if (outfile_mid[i])
182             return;
183           sufindex[i] = 0;
184           outfile_mid[i] = suffix_alphabet[sufindex[i]];
185         }
186       error (EXIT_FAILURE, 0, _("output file suffixes exhausted"));
187     }
188 }
189
190 /* Write BYTES bytes at BP to an output file.
191    If NEW_FILE_FLAG is true, open the next output file.
192    Otherwise add to the same output file already in use.  */
193
194 static void
195 cwrite (bool new_file_flag, const char *bp, size_t bytes)
196 {
197   if (new_file_flag)
198     {
199       if (output_desc >= 0 && close (output_desc) < 0)
200         error (EXIT_FAILURE, errno, "%s", outfile);
201
202       next_file_name ();
203       if (verbose)
204         fprintf (stdout, _("creating file %s\n"), quote (outfile));
205       output_desc = open (outfile,
206                           O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
207                           (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
208                            | S_IROTH | S_IWOTH));
209       if (output_desc < 0)
210         error (EXIT_FAILURE, errno, "%s", outfile);
211     }
212   if (full_write (output_desc, bp, bytes) != bytes)
213     error (EXIT_FAILURE, errno, "%s", outfile);
214 }
215
216 /* Split into pieces of exactly N_BYTES bytes.
217    Use buffer BUF, whose size is BUFSIZE.  */
218
219 static void
220 bytes_split (uintmax_t n_bytes, char *buf, size_t bufsize)
221 {
222   size_t n_read;
223   bool new_file_flag = true;
224   size_t to_read;
225   uintmax_t to_write = n_bytes;
226   char *bp_out;
227
228   do
229     {
230       n_read = full_read (STDIN_FILENO, buf, bufsize);
231       if (n_read < bufsize && errno)
232         error (EXIT_FAILURE, errno, "%s", infile);
233       bp_out = buf;
234       to_read = n_read;
235       while (true)
236         {
237           if (to_read < to_write)
238             {
239               if (to_read)      /* do not write 0 bytes! */
240                 {
241                   cwrite (new_file_flag, bp_out, to_read);
242                   to_write -= to_read;
243                   new_file_flag = false;
244                 }
245               break;
246             }
247           else
248             {
249               size_t w = to_write;
250               cwrite (new_file_flag, bp_out, w);
251               bp_out += w;
252               to_read -= w;
253               new_file_flag = true;
254               to_write = n_bytes;
255             }
256         }
257     }
258   while (n_read == bufsize);
259 }
260
261 /* Split into pieces of exactly N_LINES lines.
262    Use buffer BUF, whose size is BUFSIZE.  */
263
264 static void
265 lines_split (uintmax_t n_lines, char *buf, size_t bufsize)
266 {
267   size_t n_read;
268   char *bp, *bp_out, *eob;
269   bool new_file_flag = true;
270   uintmax_t n = 0;
271
272   do
273     {
274       n_read = full_read (STDIN_FILENO, buf, bufsize);
275       if (n_read < bufsize && errno)
276         error (EXIT_FAILURE, errno, "%s", infile);
277       bp = bp_out = buf;
278       eob = bp + n_read;
279       *eob = '\n';
280       while (true)
281         {
282           bp = memchr (bp, '\n', eob - bp + 1);
283           if (bp == eob)
284             {
285               if (eob != bp_out) /* do not write 0 bytes! */
286                 {
287                   size_t len = eob - bp_out;
288                   cwrite (new_file_flag, bp_out, len);
289                   new_file_flag = false;
290                 }
291               break;
292             }
293
294           ++bp;
295           if (++n >= n_lines)
296             {
297               cwrite (new_file_flag, bp_out, bp - bp_out);
298               bp_out = bp;
299               new_file_flag = true;
300               n = 0;
301             }
302         }
303     }
304   while (n_read == bufsize);
305 }
306 \f
307 /* Split into pieces that are as large as possible while still not more
308    than N_BYTES bytes, and are split on line boundaries except
309    where lines longer than N_BYTES bytes occur.
310    FIXME: Allow N_BYTES to be any uintmax_t value, and don't require a
311    buffer of size N_BYTES, in case N_BYTES is very large.  */
312
313 static void
314 line_bytes_split (size_t n_bytes)
315 {
316   char *bp;
317   bool eof = false;
318   size_t n_buffered = 0;
319   char *buf = xmalloc (n_bytes);
320
321   do
322     {
323       /* Fill up the full buffer size from the input file.  */
324
325       size_t to_read = n_bytes - n_buffered;
326       size_t n_read = full_read (STDIN_FILENO, buf + n_buffered, to_read);
327       if (n_read < to_read && errno)
328         error (EXIT_FAILURE, errno, "%s", infile);
329
330       n_buffered += n_read;
331       if (n_buffered != n_bytes)
332         {
333           if (n_buffered == 0)
334             break;
335           eof = true;
336         }
337
338       /* Find where to end this chunk.  */
339       bp = buf + n_buffered;
340       if (n_buffered == n_bytes)
341         {
342           while (bp > buf && bp[-1] != '\n')
343             bp--;
344         }
345
346       /* If chunk has no newlines, use all the chunk.  */
347       if (bp == buf)
348         bp = buf + n_buffered;
349
350       /* Output the chars as one output file.  */
351       cwrite (true, buf, bp - buf);
352
353       /* Discard the chars we just output; move rest of chunk
354          down to be the start of the next chunk.  Source and
355          destination probably overlap.  */
356       n_buffered -= bp - buf;
357       if (n_buffered > 0)
358         memmove (buf, bp, n_buffered);
359     }
360   while (!eof);
361   free (buf);
362 }
363
364 #define FAIL_ONLY_ONE_WAY()                                     \
365   do                                                            \
366     {                                                           \
367       error (0, 0, _("cannot split in more than one way"));     \
368       usage (EXIT_FAILURE);                                     \
369     }                                                           \
370   while (0)
371
372 int
373 main (int argc, char **argv)
374 {
375   struct stat stat_buf;
376   enum
377     {
378       type_undef, type_bytes, type_byteslines, type_lines, type_digits
379     } split_type = type_undef;
380   size_t in_blk_size;           /* optimal block size of input file device */
381   char *buf;                    /* file i/o buffer */
382   size_t page_size = getpagesize ();
383   uintmax_t n_units;
384   static char const multipliers[] = "bEGKkMmPTYZ0";
385   int c;
386   int digits_optind = 0;
387
388   initialize_main (&argc, &argv);
389   set_program_name (argv[0]);
390   setlocale (LC_ALL, "");
391   bindtextdomain (PACKAGE, LOCALEDIR);
392   textdomain (PACKAGE);
393
394   atexit (close_stdout);
395
396   /* Parse command line options.  */
397
398   infile = bad_cast ( "-");
399   outbase = bad_cast ("x");
400
401   while (1)
402     {
403       /* This is the argv-index of the option we will read next.  */
404       int this_optind = optind ? optind : 1;
405
406       c = getopt_long (argc, argv, "0123456789C:a:b:dl:", longopts, NULL);
407       if (c == -1)
408         break;
409
410       switch (c)
411         {
412         case 'a':
413           {
414             unsigned long tmp;
415             if (xstrtoul (optarg, NULL, 10, &tmp, "") != LONGINT_OK
416                 || SIZE_MAX / sizeof (size_t) < tmp)
417               {
418                 error (0, 0, _("%s: invalid suffix length"), optarg);
419                 usage (EXIT_FAILURE);
420               }
421             suffix_length = tmp;
422           }
423           break;
424
425         case 'b':
426           if (split_type != type_undef)
427             FAIL_ONLY_ONE_WAY ();
428           split_type = type_bytes;
429           if (xstrtoumax (optarg, NULL, 10, &n_units, multipliers) != LONGINT_OK
430               || n_units == 0)
431             {
432               error (0, 0, _("%s: invalid number of bytes"), optarg);
433               usage (EXIT_FAILURE);
434             }
435           break;
436
437         case 'l':
438           if (split_type != type_undef)
439             FAIL_ONLY_ONE_WAY ();
440           split_type = type_lines;
441           if (xstrtoumax (optarg, NULL, 10, &n_units, "") != LONGINT_OK
442               || n_units == 0)
443             {
444               error (0, 0, _("%s: invalid number of lines"), optarg);
445               usage (EXIT_FAILURE);
446             }
447           break;
448
449         case 'C':
450           if (split_type != type_undef)
451             FAIL_ONLY_ONE_WAY ();
452           split_type = type_byteslines;
453           if (xstrtoumax (optarg, NULL, 10, &n_units, multipliers) != LONGINT_OK
454               || n_units == 0 || SIZE_MAX < n_units)
455             {
456               error (0, 0, _("%s: invalid number of bytes"), optarg);
457               usage (EXIT_FAILURE);
458             }
459           break;
460
461         case '0':
462         case '1':
463         case '2':
464         case '3':
465         case '4':
466         case '5':
467         case '6':
468         case '7':
469         case '8':
470         case '9':
471           if (split_type == type_undef)
472             {
473               split_type = type_digits;
474               n_units = 0;
475             }
476           if (split_type != type_undef && split_type != type_digits)
477             FAIL_ONLY_ONE_WAY ();
478           if (digits_optind != 0 && digits_optind != this_optind)
479             n_units = 0;        /* More than one number given; ignore other. */
480           digits_optind = this_optind;
481           if (!DECIMAL_DIGIT_ACCUMULATE (n_units, c - '0', uintmax_t))
482             {
483               char buffer[INT_BUFSIZE_BOUND (uintmax_t)];
484               error (EXIT_FAILURE, 0,
485                      _("line count option -%s%c... is too large"),
486                      umaxtostr (n_units, buffer), c);
487             }
488           break;
489
490         case 'd':
491           suffix_alphabet = "0123456789";
492           break;
493
494         case VERBOSE_OPTION:
495           verbose = true;
496           break;
497
498         case_GETOPT_HELP_CHAR;
499
500         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
501
502         default:
503           usage (EXIT_FAILURE);
504         }
505     }
506
507   /* Handle default case.  */
508   if (split_type == type_undef)
509     {
510       split_type = type_lines;
511       n_units = 1000;
512     }
513
514   if (n_units == 0)
515     {
516       error (0, 0, _("invalid number of lines: 0"));
517       usage (EXIT_FAILURE);
518     }
519
520   /* Get out the filename arguments.  */
521
522   if (optind < argc)
523     infile = argv[optind++];
524
525   if (optind < argc)
526     outbase = argv[optind++];
527
528   if (optind < argc)
529     {
530       error (0, 0, _("extra operand %s"), quote (argv[optind]));
531       usage (EXIT_FAILURE);
532     }
533
534   /* Open the input file.  */
535   if (! STREQ (infile, "-")
536       && fd_reopen (STDIN_FILENO, infile, O_RDONLY, 0) < 0)
537     error (EXIT_FAILURE, errno, _("cannot open %s for reading"),
538            quote (infile));
539
540   /* Binary I/O is safer when bytecounts are used.  */
541   if (O_BINARY && ! isatty (STDIN_FILENO))
542     xfreopen (NULL, "rb", stdin);
543
544   /* No output file is open now.  */
545   output_desc = -1;
546
547   /* Get the optimal block size of input device and make a buffer.  */
548
549   if (fstat (STDIN_FILENO, &stat_buf) != 0)
550     error (EXIT_FAILURE, errno, "%s", infile);
551   in_blk_size = io_blksize (stat_buf);
552
553   buf = ptr_align (xmalloc (in_blk_size + 1 + page_size - 1), page_size);
554
555   switch (split_type)
556     {
557     case type_digits:
558     case type_lines:
559       lines_split (n_units, buf, in_blk_size);
560       break;
561
562     case type_bytes:
563       bytes_split (n_units, buf, in_blk_size);
564       break;
565
566     case type_byteslines:
567       line_bytes_split (n_units);
568       break;
569
570     default:
571       abort ();
572     }
573
574   if (close (STDIN_FILENO) != 0)
575     error (EXIT_FAILURE, errno, "%s", infile);
576   if (output_desc >= 0 && close (output_desc) < 0)
577     error (EXIT_FAILURE, errno, "%s", outfile);
578
579   exit (EXIT_SUCCESS);
580 }