1 /* split.c -- split a file into pieces.
2 Copyright (C) 1988, 1991, 1995 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
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
35 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
39 # define INT_MAX ((int) (UINT_MAX >> 1))
50 /* The name this program was run with. */
53 /* Base name of output files. */
56 /* Pointer to the end of the prefix in OUTFILE.
57 Suffixes are inserted here. */
58 static char *outfile_mid
;
60 /* Pointer to the end of OUTFILE. */
61 static char *outfile_end
;
63 /* Name of input file. May be "-". */
66 /* Descriptor on which input file is open. */
67 static int input_desc
;
69 /* Descriptor on which output file is open. */
70 static int output_desc
;
72 /* If nonzero, display usage information and exit. */
75 /* If nonzero, print the version on standard output then exit. */
76 static int show_version
;
78 /* If nonzero, print a diagnostic on standard error just before each
79 output file is opened. */
82 static struct option
const longopts
[] =
84 {"bytes", required_argument
, NULL
, 'b'},
85 {"lines", required_argument
, NULL
, 'l'},
86 {"line-bytes", required_argument
, NULL
, 'C'},
87 {"verbose", no_argument
, NULL
, 2},
88 {"help", no_argument
, &show_help
, 1},
89 {"version", no_argument
, &show_version
, 1},
94 usage (int status
, const char *reason
)
97 fprintf (stderr
, "%s: %s\n", program_name
, reason
);
100 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
105 Usage: %s [OPTION] [INPUT [PREFIX]]\n\
109 Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default\n\
110 PREFIX is `x'. With no INPUT, or when INPUT is -, read standard input.\n\
112 -C, --line-bytes=SIZE put at most SIZE bytes of lines per output file\n\
113 -b, --bytes=SIZE put SIZE bytes per output file\n\
114 -l, --lines=NUMBER put NUMBER lines per output file\n\
115 --verbose print a diagnostic to standard error just\n\
116 before each output file is opened\n\
117 -NUMBER same as -l NUMBER\n\
118 --help display this help and exit\n\
119 --version output version information and exit\n\
121 SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
127 /* Compute the next sequential output file name suffix and store it
128 into the string `outfile' at the position pointed to by `outfile_mid'. */
131 next_file_name (void)
137 static int first_call
= 1;
139 /* Status for outfile name generation. */
140 static unsigned outfile_count
= 0;
141 static unsigned outfile_name_limit
= 25 * 26;
142 static unsigned outfile_name_generation
= 1;
147 if (outfile_count
< outfile_name_limit
)
149 for (ne
= outfile_end
- 1; ; ne
--)
161 outfile_name_limit
*= 26;
162 outfile_name_generation
++;
163 *outfile_mid
++ = 'z';
164 for (i
= 0; i
<= outfile_name_generation
; i
++)
165 outfile_mid
[i
] = 'a';
169 /* Write BYTES bytes at BP to an output file.
170 If NEW_FILE_FLAG is nonzero, open the next output file.
171 Otherwise add to the same output file already in use. */
174 cwrite (int new_file_flag
, const char *bp
, int bytes
)
178 if (output_desc
>= 0 && close (output_desc
) < 0)
179 error (1, errno
, "%s", outfile
);
183 fprintf (stderr
, _("creating file `%s'\n"), outfile
);
184 output_desc
= open (outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
186 error (1, errno
, "%s", outfile
);
188 if (full_write (output_desc
, bp
, bytes
) < 0)
189 error (1, errno
, "%s", outfile
);
192 /* Read NCHARS bytes from the input file into BUF.
193 Return the number of bytes successfully read.
194 If this is less than NCHARS, do not call `stdread' again. */
197 stdread (char *buf
, int nchars
)
200 int to_be_read
= nchars
;
204 n_read
= safe_read (input_desc
, buf
, to_be_read
);
209 to_be_read
-= n_read
;
212 return nchars
- to_be_read
;
215 /* Split into pieces of exactly NCHARS bytes.
216 Use buffer BUF, whose size is BUFSIZE. */
219 bytes_split (int nchars
, char *buf
, int bufsize
)
222 int new_file_flag
= 1;
224 int to_write
= nchars
;
229 n_read
= stdread (buf
, bufsize
);
231 error (1, errno
, "%s", infile
);
236 if (to_read
< to_write
)
238 if (to_read
) /* do not write 0 bytes! */
240 cwrite (new_file_flag
, bp_out
, to_read
);
248 cwrite (new_file_flag
, bp_out
, to_write
);
256 while (n_read
== bufsize
);
259 /* Split into pieces of exactly NLINES lines.
260 Use buffer BUF, whose size is BUFSIZE. */
263 lines_split (int nlines
, char *buf
, int bufsize
)
266 char *bp
, *bp_out
, *eob
;
267 int new_file_flag
= 1;
272 n_read
= stdread (buf
, bufsize
);
274 error (1, errno
, "%s", infile
);
280 while (*bp
++ != '\n')
281 ; /* this semicolon takes most of the time */
284 if (eob
!= bp_out
) /* do not write 0 bytes! */
286 cwrite (new_file_flag
, bp_out
, eob
- bp_out
);
294 cwrite (new_file_flag
, bp_out
, bp
- bp_out
);
301 while (n_read
== bufsize
);
304 /* Split into pieces that are as large as possible while still not more
305 than NCHARS bytes, and are split on line boundaries except
306 where lines longer than NCHARS bytes occur. */
309 line_bytes_split (int nchars
)
315 char *buf
= (char *) xmalloc (nchars
);
319 /* Fill up the full buffer size from the input file. */
321 n_read
= stdread (buf
+ n_buffered
, nchars
- n_buffered
);
323 error (1, errno
, "%s", infile
);
325 n_buffered
+= n_read
;
326 if (n_buffered
!= nchars
)
329 /* Find where to end this chunk. */
330 bp
= buf
+ n_buffered
;
331 if (n_buffered
== nchars
)
333 while (bp
> buf
&& bp
[-1] != '\n')
337 /* If chunk has no newlines, use all the chunk. */
339 bp
= buf
+ n_buffered
;
341 /* Output the chars as one output file. */
342 cwrite (1, buf
, bp
- buf
);
344 /* Discard the chars we just output; move rest of chunk
345 down to be the start of the next chunk. Source and
346 destination probably overlap. */
347 n_buffered
-= bp
- buf
;
349 memmove (buf
, bp
, n_buffered
);
356 main (int argc
, char **argv
)
358 struct stat stat_buf
;
359 int num
; /* numeric argument from command line */
362 type_undef
, type_bytes
, type_byteslines
, type_lines
, type_digits
363 } split_type
= type_undef
;
364 int in_blk_size
; /* optimal block size of input file device */
365 char *buf
; /* file i/o buffer */
369 int digits_optind
= 0;
371 program_name
= argv
[0];
372 setlocale (LC_ALL
, "");
373 bindtextdomain (PACKAGE
, LOCALEDIR
);
374 textdomain (PACKAGE
);
376 /* Parse command line options. */
383 /* This is the argv-index of the option we will read next. */
384 int this_optind
= optind
? optind
: 1;
387 c
= getopt_long (argc
, argv
, "0123456789vb:l:C:", longopts
, (int *) 0);
397 if (split_type
!= type_undef
)
398 usage (2, _("cannot split in more than one way"));
399 split_type
= type_bytes
;
400 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "bkm") != LONGINT_OK
401 || tmp_long
< 0 || tmp_long
> INT_MAX
)
402 usage (2, _("invalid number of bytes"));
403 accum
= (int) tmp_long
;
407 if (split_type
!= type_undef
)
408 usage (2, _("cannot split in more than one way"));
409 split_type
= type_lines
;
410 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
411 || tmp_long
< 0 || tmp_long
> INT_MAX
)
412 usage (2, _("invalid number of lines"));
413 accum
= (int) tmp_long
;
417 if (split_type
!= type_undef
)
418 usage (2, _("cannot split in more than one way"));
419 split_type
= type_byteslines
;
420 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "bkm") != LONGINT_OK
421 || tmp_long
< 0 || tmp_long
> INT_MAX
)
422 usage (2, _("invalid number of bytes"));
423 accum
= (int) tmp_long
;
436 if (split_type
!= type_undef
&& split_type
!= type_digits
)
437 usage (2, _("cannot split in more than one way"));
438 if (digits_optind
!= 0 && digits_optind
!= this_optind
)
439 accum
= 0; /* More than one number given; ignore other. */
440 digits_optind
= this_optind
;
441 split_type
= type_digits
;
442 accum
= accum
* 10 + c
- '0';
450 usage (2, (char *)0);
456 printf ("split - %s\n", PACKAGE_VERSION
);
461 usage (0, (char *)0);
463 /* Handle default case. */
464 if (split_type
== type_undef
)
466 split_type
= type_lines
;
471 usage (2, _("invalid number"));
474 /* Get out the filename arguments. */
477 infile
= argv
[optind
++];
480 outbase
= argv
[optind
++];
483 usage (2, _("too many arguments"));
485 /* Open the input file. */
486 if (!strcmp (infile
, "-"))
490 input_desc
= open (infile
, O_RDONLY
);
492 error (1, errno
, "%s", infile
);
495 /* No output file is open now. */
498 /* Copy the output file prefix so we can add suffixes to it.
499 26**29 is certainly enough output files! */
501 outfile
= xmalloc (strlen (outbase
) + 30);
502 strcpy (outfile
, outbase
);
503 outfile_mid
= outfile
+ strlen (outfile
);
504 outfile_end
= outfile_mid
+ 2;
505 memset (outfile_mid
, 0, 30);
506 outfile_mid
[0] = 'a';
507 outfile_mid
[1] = 'a' - 1; /* first call to next_file_name makes it an 'a' */
509 /* Get the optimal block size of input device and make a buffer. */
511 if (fstat (input_desc
, &stat_buf
) < 0)
512 error (1, errno
, "%s", infile
);
513 in_blk_size
= ST_BLKSIZE (stat_buf
);
515 buf
= xmalloc (in_blk_size
+ 1);
521 lines_split (num
, buf
, in_blk_size
);
525 bytes_split (num
, buf
, in_blk_size
);
528 case type_byteslines
:
529 line_bytes_split (num
);
536 if (close (input_desc
) < 0)
537 error (1, errno
, "%s", infile
);
538 if (output_desc
>= 0 && close (output_desc
) < 0)
539 error (1, errno
, "%s", outfile
);