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