1 /* split.c -- split a file into pieces.
2 Copyright (C) 1988, 1991, 1995-2006 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* By tege@sics.se, with rms.
21 * Implement -t CHAR or -t REGEX to specify break characters other
28 #include <sys/types.h>
32 #include "fd-reopen.h"
34 #include "getpagesize.h"
35 #include "full-read.h"
36 #include "full-write.h"
39 #include "safe-read.h"
42 /* The official name of this program (e.g., no `g' prefix). */
43 #define PROGRAM_NAME "split"
45 #define AUTHORS "Torbjorn Granlund", "Richard M. Stallman"
47 #define DEFAULT_SUFFIX_LENGTH 2
49 /* The name this program was run with. */
52 /* Base name of output files. */
53 static char const *outbase
;
55 /* Name of output files. */
58 /* Pointer to the end of the prefix in OUTFILE.
59 Suffixes are inserted here. */
60 static char *outfile_mid
;
62 /* Length of OUTFILE's suffix. */
63 static size_t suffix_length
= DEFAULT_SUFFIX_LENGTH
;
65 /* Alphabet of characters to use in suffix. */
66 static char const *suffix_alphabet
= "abcdefghijklmnopqrstuvwxyz";
68 /* Name of input file. May be "-". */
71 /* Descriptor on which output file is open. */
72 static int output_desc
;
74 /* If true, print a diagnostic on standard error just before each
75 output file is opened. */
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. */
82 VERBOSE_OPTION
= CHAR_MAX
+ 1
85 static struct option
const longopts
[] =
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
},
101 if (status
!= EXIT_SUCCESS
)
102 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
107 Usage: %s [OPTION] [INPUT [PREFIX]]\n\
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\
117 Mandatory arguments to long options are mandatory for short options too.\n\
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
);
127 --verbose print a diagnostic to standard error just\n\
128 before each output file is opened\n\
130 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
131 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
134 SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
136 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
141 /* Compute the next sequential output file name and store it into the
145 next_file_name (void)
147 /* Index in suffix_alphabet of each character in the suffix. */
148 static size_t *sufindex
;
152 /* Allocate and initialize the first file name. */
154 size_t outbase_length
= strlen (outbase
);
155 size_t outfile_length
= outbase_length
+ suffix_length
;
156 if (outfile_length
+ 1 < outbase_length
)
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
);
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. */
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
);
181 /* Increment the suffix in place, if possible. */
183 size_t i
= suffix_length
;
187 outfile_mid
[i
] = suffix_alphabet
[sufindex
[i
]];
191 outfile_mid
[i
] = suffix_alphabet
[sufindex
[i
]];
193 error (EXIT_FAILURE
, 0, _("Output file suffixes exhausted"));
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. */
202 cwrite (bool new_file_flag
, const char *bp
, size_t bytes
)
206 if (output_desc
>= 0 && close (output_desc
) < 0)
207 error (EXIT_FAILURE
, errno
, "%s", outfile
);
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
));
217 error (EXIT_FAILURE
, errno
, "%s", outfile
);
219 if (full_write (output_desc
, bp
, bytes
) != bytes
)
220 error (EXIT_FAILURE
, errno
, "%s", outfile
);
223 /* Split into pieces of exactly N_BYTES bytes.
224 Use buffer BUF, whose size is BUFSIZE. */
227 bytes_split (uintmax_t n_bytes
, char *buf
, size_t bufsize
)
230 bool new_file_flag
= true;
232 uintmax_t to_write
= n_bytes
;
237 n_read
= full_read (STDIN_FILENO
, buf
, bufsize
);
238 if (n_read
== SAFE_READ_ERROR
)
239 error (EXIT_FAILURE
, errno
, "%s", infile
);
244 if (to_read
< to_write
)
246 if (to_read
) /* do not write 0 bytes! */
248 cwrite (new_file_flag
, bp_out
, to_read
);
250 new_file_flag
= false;
257 cwrite (new_file_flag
, bp_out
, w
);
260 new_file_flag
= true;
265 while (n_read
== bufsize
);
268 /* Split into pieces of exactly N_LINES lines.
269 Use buffer BUF, whose size is BUFSIZE. */
272 lines_split (uintmax_t n_lines
, char *buf
, size_t bufsize
)
275 char *bp
, *bp_out
, *eob
;
276 bool new_file_flag
= true;
281 n_read
= full_read (STDIN_FILENO
, buf
, bufsize
);
282 if (n_read
== SAFE_READ_ERROR
)
283 error (EXIT_FAILURE
, errno
, "%s", infile
);
289 bp
= memchr (bp
, '\n', eob
- bp
+ 1);
292 if (eob
!= bp_out
) /* do not write 0 bytes! */
294 size_t len
= eob
- bp_out
;
295 cwrite (new_file_flag
, bp_out
, len
);
296 new_file_flag
= false;
304 cwrite (new_file_flag
, bp_out
, bp
- bp_out
);
306 new_file_flag
= true;
311 while (n_read
== bufsize
);
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. */
321 line_bytes_split (size_t n_bytes
)
326 size_t n_buffered
= 0;
327 char *buf
= xmalloc (n_bytes
);
331 /* Fill up the full buffer size from the input file. */
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
);
337 n_buffered
+= n_read
;
338 if (n_buffered
!= n_bytes
)
341 /* Find where to end this chunk. */
342 bp
= buf
+ n_buffered
;
343 if (n_buffered
== n_bytes
)
345 while (bp
> buf
&& bp
[-1] != '\n')
349 /* If chunk has no newlines, use all the chunk. */
351 bp
= buf
+ n_buffered
;
353 /* Output the chars as one output file. */
354 cwrite (true, buf
, bp
- buf
);
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
;
361 memmove (buf
, bp
, n_buffered
);
367 #define FAIL_ONLY_ONE_WAY() \
370 error (0, 0, _("cannot split in more than one way")); \
371 usage (EXIT_FAILURE); \
376 main (int argc
, char **argv
)
378 struct stat stat_buf
;
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 ();
388 int digits_optind
= 0;
390 initialize_main (&argc
, &argv
);
391 program_name
= argv
[0];
392 setlocale (LC_ALL
, "");
393 bindtextdomain (PACKAGE
, LOCALEDIR
);
394 textdomain (PACKAGE
);
396 atexit (close_stdout
);
398 /* Parse command line options. */
405 /* This is the argv-index of the option we will read next. */
406 int this_optind
= optind
? optind
: 1;
408 c
= getopt_long (argc
, argv
, "0123456789C:a:b:dl:", longopts
, NULL
);
417 if (xstrtoul (optarg
, NULL
, 10, &tmp
, "") != LONGINT_OK
418 || SIZE_MAX
/ sizeof (size_t) < tmp
)
420 error (0, 0, _("%s: invalid suffix length"), optarg
);
421 usage (EXIT_FAILURE
);
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
434 error (0, 0, _("%s: invalid number of bytes"), optarg
);
435 usage (EXIT_FAILURE
);
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
446 error (0, 0, _("%s: invalid number of lines"), optarg
);
447 usage (EXIT_FAILURE
);
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
)
458 error (0, 0, _("%s: invalid number of bytes"), optarg
);
459 usage (EXIT_FAILURE
);
473 if (split_type
== type_undef
)
475 split_type
= type_digits
;
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))
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
);
493 suffix_alphabet
= "0123456789";
500 case_GETOPT_HELP_CHAR
;
502 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
505 usage (EXIT_FAILURE
);
509 /* Handle default case. */
510 if (split_type
== type_undef
)
512 split_type
= type_lines
;
518 error (0, 0, _("invalid number of lines: 0"));
519 usage (EXIT_FAILURE
);
522 /* Get out the filename arguments. */
525 infile
= argv
[optind
++];
528 outbase
= argv
[optind
++];
532 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
533 usage (EXIT_FAILURE
);
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"),
542 /* Binary I/O is safer when bytecounts are used. */
543 if (O_BINARY
&& ! isatty (STDIN_FILENO
))
544 freopen (NULL
, "rb", stdin
);
546 /* No output file is open now. */
549 /* Get the optimal block size of input device and make a buffer. */
551 if (fstat (STDIN_FILENO
, &stat_buf
) != 0)
552 error (EXIT_FAILURE
, errno
, "%s", infile
);
553 in_blk_size
= ST_BLKSIZE (stat_buf
);
555 buf
= ptr_align (xmalloc (in_blk_size
+ 1 + page_size
- 1), page_size
);
561 lines_split (n_units
, buf
, in_blk_size
);
565 bytes_split (n_units
, buf
, in_blk_size
);
568 case type_byteslines
:
569 line_bytes_split (n_units
);
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
);