1 /* split.c -- split a file into pieces.
2 Copyright (C) 1988, 1991, 1995-2010 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* By tege@sics.se, with rms.
20 * Implement -t CHAR or -t REGEX to specify break characters other
27 #include <sys/types.h>
31 #include "fd-reopen.h"
33 #include "full-read.h"
34 #include "full-write.h"
39 /* The official name of this program (e.g., no `g' prefix). */
40 #define PROGRAM_NAME "split"
43 proper_name_utf8 ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \
44 proper_name ("Richard M. Stallman")
46 #define DEFAULT_SUFFIX_LENGTH 2
48 /* Base name of output files. */
49 static char const *outbase
;
51 /* Name of output files. */
54 /* Pointer to the end of the prefix in OUTFILE.
55 Suffixes are inserted here. */
56 static char *outfile_mid
;
58 /* Length of OUTFILE's suffix. */
59 static size_t suffix_length
= DEFAULT_SUFFIX_LENGTH
;
61 /* Alphabet of characters to use in suffix. */
62 static char const *suffix_alphabet
= "abcdefghijklmnopqrstuvwxyz";
64 /* Name of input file. May be "-". */
67 /* Descriptor on which output file is open. */
68 static int output_desc
;
70 /* If true, print a diagnostic on standard error just before each
71 output file is opened. */
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. */
78 VERBOSE_OPTION
= CHAR_MAX
+ 1
81 static struct option
const longopts
[] =
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
},
97 if (status
!= EXIT_SUCCESS
)
98 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
103 Usage: %s [OPTION]... [INPUT [PREFIX]]\n\
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\
113 Mandatory arguments to long options are mandatory for short options too.\n\
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
);
123 --verbose print a diagnostic just before each\n\
124 output file is opened\n\
126 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
127 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
129 emit_ancillary_info ();
134 /* Compute the next sequential output file name and store it into the
138 next_file_name (void)
140 /* Index in suffix_alphabet of each character in the suffix. */
141 static size_t *sufindex
;
145 /* Allocate and initialize the first file name. */
147 size_t outbase_length
= strlen (outbase
);
148 size_t outfile_length
= outbase_length
+ suffix_length
;
149 if (outfile_length
+ 1 < outbase_length
)
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
);
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. */
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
);
174 /* Increment the suffix in place, if possible. */
176 size_t i
= suffix_length
;
180 outfile_mid
[i
] = suffix_alphabet
[sufindex
[i
]];
184 outfile_mid
[i
] = suffix_alphabet
[sufindex
[i
]];
186 error (EXIT_FAILURE
, 0, _("output file suffixes exhausted"));
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. */
195 cwrite (bool new_file_flag
, const char *bp
, size_t bytes
)
199 if (output_desc
>= 0 && close (output_desc
) < 0)
200 error (EXIT_FAILURE
, errno
, "%s", outfile
);
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
));
210 error (EXIT_FAILURE
, errno
, "%s", outfile
);
212 if (full_write (output_desc
, bp
, bytes
) != bytes
)
213 error (EXIT_FAILURE
, errno
, "%s", outfile
);
216 /* Split into pieces of exactly N_BYTES bytes.
217 Use buffer BUF, whose size is BUFSIZE. */
220 bytes_split (uintmax_t n_bytes
, char *buf
, size_t bufsize
)
223 bool new_file_flag
= true;
225 uintmax_t to_write
= n_bytes
;
230 n_read
= full_read (STDIN_FILENO
, buf
, bufsize
);
231 if (n_read
< bufsize
&& errno
)
232 error (EXIT_FAILURE
, errno
, "%s", infile
);
237 if (to_read
< to_write
)
239 if (to_read
) /* do not write 0 bytes! */
241 cwrite (new_file_flag
, bp_out
, to_read
);
243 new_file_flag
= false;
250 cwrite (new_file_flag
, bp_out
, w
);
253 new_file_flag
= true;
258 while (n_read
== bufsize
);
261 /* Split into pieces of exactly N_LINES lines.
262 Use buffer BUF, whose size is BUFSIZE. */
265 lines_split (uintmax_t n_lines
, char *buf
, size_t bufsize
)
268 char *bp
, *bp_out
, *eob
;
269 bool new_file_flag
= true;
274 n_read
= full_read (STDIN_FILENO
, buf
, bufsize
);
275 if (n_read
< bufsize
&& errno
)
276 error (EXIT_FAILURE
, errno
, "%s", infile
);
282 bp
= memchr (bp
, '\n', eob
- bp
+ 1);
285 if (eob
!= bp_out
) /* do not write 0 bytes! */
287 size_t len
= eob
- bp_out
;
288 cwrite (new_file_flag
, bp_out
, len
);
289 new_file_flag
= false;
297 cwrite (new_file_flag
, bp_out
, bp
- bp_out
);
299 new_file_flag
= true;
304 while (n_read
== bufsize
);
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. */
314 line_bytes_split (size_t n_bytes
)
318 size_t n_buffered
= 0;
319 char *buf
= xmalloc (n_bytes
);
323 /* Fill up the full buffer size from the input file. */
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
);
330 n_buffered
+= n_read
;
331 if (n_buffered
!= n_bytes
)
338 /* Find where to end this chunk. */
339 bp
= buf
+ n_buffered
;
340 if (n_buffered
== n_bytes
)
342 while (bp
> buf
&& bp
[-1] != '\n')
346 /* If chunk has no newlines, use all the chunk. */
348 bp
= buf
+ n_buffered
;
350 /* Output the chars as one output file. */
351 cwrite (true, buf
, bp
- buf
);
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
;
358 memmove (buf
, bp
, n_buffered
);
364 #define FAIL_ONLY_ONE_WAY() \
367 error (0, 0, _("cannot split in more than one way")); \
368 usage (EXIT_FAILURE); \
373 main (int argc
, char **argv
)
375 struct stat stat_buf
;
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 ();
384 static char const multipliers
[] = "bEGKkMmPTYZ0";
386 int digits_optind
= 0;
388 initialize_main (&argc
, &argv
);
389 set_program_name (argv
[0]);
390 setlocale (LC_ALL
, "");
391 bindtextdomain (PACKAGE
, LOCALEDIR
);
392 textdomain (PACKAGE
);
394 atexit (close_stdout
);
396 /* Parse command line options. */
398 infile
= bad_cast ( "-");
399 outbase
= bad_cast ("x");
403 /* This is the argv-index of the option we will read next. */
404 int this_optind
= optind
? optind
: 1;
406 c
= getopt_long (argc
, argv
, "0123456789C:a:b:dl:", longopts
, NULL
);
415 if (xstrtoul (optarg
, NULL
, 10, &tmp
, "") != LONGINT_OK
416 || SIZE_MAX
/ sizeof (size_t) < tmp
)
418 error (0, 0, _("%s: invalid suffix length"), optarg
);
419 usage (EXIT_FAILURE
);
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
432 error (0, 0, _("%s: invalid number of bytes"), optarg
);
433 usage (EXIT_FAILURE
);
435 /* If input is a pipe, we could get more data than is possible
436 to write to a single file, so indicate that immediately
437 rather than having possibly future invocations fail. */
438 if (OFF_T_MAX
< n_units
)
439 error (EXIT_FAILURE
, EFBIG
,
440 _("%s: invalid number of bytes"), optarg
);
445 if (split_type
!= type_undef
)
446 FAIL_ONLY_ONE_WAY ();
447 split_type
= type_lines
;
448 if (xstrtoumax (optarg
, NULL
, 10, &n_units
, "") != LONGINT_OK
451 error (0, 0, _("%s: invalid number of lines"), optarg
);
452 usage (EXIT_FAILURE
);
457 if (split_type
!= type_undef
)
458 FAIL_ONLY_ONE_WAY ();
459 split_type
= type_byteslines
;
460 if (xstrtoumax (optarg
, NULL
, 10, &n_units
, multipliers
) != LONGINT_OK
461 || n_units
== 0 || SIZE_MAX
< n_units
)
463 error (0, 0, _("%s: invalid number of bytes"), optarg
);
464 usage (EXIT_FAILURE
);
466 if (OFF_T_MAX
< n_units
)
467 error (EXIT_FAILURE
, EFBIG
,
468 _("%s: invalid number of bytes"), optarg
);
481 if (split_type
== type_undef
)
483 split_type
= type_digits
;
486 if (split_type
!= type_undef
&& split_type
!= type_digits
)
487 FAIL_ONLY_ONE_WAY ();
488 if (digits_optind
!= 0 && digits_optind
!= this_optind
)
489 n_units
= 0; /* More than one number given; ignore other. */
490 digits_optind
= this_optind
;
491 if (!DECIMAL_DIGIT_ACCUMULATE (n_units
, c
- '0', uintmax_t))
493 char buffer
[INT_BUFSIZE_BOUND (uintmax_t)];
494 error (EXIT_FAILURE
, 0,
495 _("line count option -%s%c... is too large"),
496 umaxtostr (n_units
, buffer
), c
);
501 suffix_alphabet
= "0123456789";
508 case_GETOPT_HELP_CHAR
;
510 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
513 usage (EXIT_FAILURE
);
517 /* Handle default case. */
518 if (split_type
== type_undef
)
520 split_type
= type_lines
;
526 error (0, 0, _("invalid number of lines: 0"));
527 usage (EXIT_FAILURE
);
530 /* Get out the filename arguments. */
533 infile
= argv
[optind
++];
536 outbase
= argv
[optind
++];
540 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
541 usage (EXIT_FAILURE
);
544 /* Open the input file. */
545 if (! STREQ (infile
, "-")
546 && fd_reopen (STDIN_FILENO
, infile
, O_RDONLY
, 0) < 0)
547 error (EXIT_FAILURE
, errno
, _("cannot open %s for reading"),
550 /* Binary I/O is safer when bytecounts are used. */
551 if (O_BINARY
&& ! isatty (STDIN_FILENO
))
552 xfreopen (NULL
, "rb", stdin
);
554 /* No output file is open now. */
557 /* Get the optimal block size of input device and make a buffer. */
559 if (fstat (STDIN_FILENO
, &stat_buf
) != 0)
560 error (EXIT_FAILURE
, errno
, "%s", infile
);
561 in_blk_size
= io_blksize (stat_buf
);
563 buf
= ptr_align (xmalloc (in_blk_size
+ 1 + page_size
- 1), page_size
);
569 lines_split (n_units
, buf
, in_blk_size
);
573 bytes_split (n_units
, buf
, in_blk_size
);
576 case type_byteslines
:
577 line_bytes_split (n_units
);
584 if (close (STDIN_FILENO
) != 0)
585 error (EXIT_FAILURE
, errno
, "%s", infile
);
586 if (output_desc
>= 0 && close (output_desc
) < 0)
587 error (EXIT_FAILURE
, errno
, "%s", outfile
);