1 /* split.c -- split a file into pieces.
2 Copyright (C) 88, 91, 95, 96, 1997, 1998 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., 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>
37 /* The name this program was run with. */
40 /* Base name of output files. */
43 /* Pointer to the end of the prefix in OUTFILE.
44 Suffixes are inserted here. */
45 static char *outfile_mid
;
47 /* Pointer to the end of OUTFILE. */
48 static char *outfile_end
;
50 /* Name of input file. May be "-". */
53 /* Descriptor on which input file is open. */
54 static int input_desc
;
56 /* Descriptor on which output file is open. */
57 static int output_desc
;
59 /* If nonzero, display usage information and exit. */
62 /* If nonzero, print the version on standard output then exit. */
63 static int show_version
;
65 /* If nonzero, print a diagnostic on standard error just before each
66 output file is opened. */
69 static struct option
const longopts
[] =
71 {"bytes", required_argument
, NULL
, 'b'},
72 {"lines", required_argument
, NULL
, 'l'},
73 {"line-bytes", required_argument
, NULL
, 'C'},
74 {"verbose", no_argument
, NULL
, 2},
75 {"help", no_argument
, &show_help
, 1},
76 {"version", no_argument
, &show_version
, 1},
84 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
89 Usage: %s [OPTION] [INPUT [PREFIX]]\n\
93 Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default\n\
94 PREFIX is `x'. With no INPUT, or when INPUT is -, read standard input.\n\
96 -b, --bytes=SIZE put SIZE bytes per output file\n\
97 -C, --line-bytes=SIZE put at most SIZE bytes of lines per output file\n\
98 -l, --lines=NUMBER put NUMBER lines per output file\n\
99 -NUMBER same as -l NUMBER\n\
100 --verbose print a diagnostic to standard error just\n\
101 before each output file is opened\n\
102 --help display this help and exit\n\
103 --version output version information and exit\n\
105 SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
107 puts (_("\nReport bugs to <textutils-bugs@gnu.org>."));
109 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
112 /* Compute the next sequential output file name suffix and store it
113 into the string `outfile' at the position pointed to by `outfile_mid'. */
116 next_file_name (void)
122 static int first_call
= 1;
124 /* Status for outfile name generation. */
125 static unsigned outfile_count
= 0;
126 static unsigned outfile_name_limit
= 25 * 26;
127 static unsigned outfile_name_generation
= 1;
132 if (outfile_count
< outfile_name_limit
)
134 for (ne
= outfile_end
- 1; ; ne
--)
146 outfile_name_limit
*= 26;
147 outfile_name_generation
++;
148 *outfile_mid
++ = 'z';
149 for (i
= 0; i
<= outfile_name_generation
; i
++)
150 outfile_mid
[i
] = 'a';
154 /* Write BYTES bytes at BP to an output file.
155 If NEW_FILE_FLAG is nonzero, open the next output file.
156 Otherwise add to the same output file already in use. */
159 cwrite (int new_file_flag
, const char *bp
, int bytes
)
163 if (output_desc
>= 0 && close (output_desc
) < 0)
164 error (EXIT_FAILURE
, errno
, "%s", outfile
);
168 fprintf (stderr
, _("creating file `%s'\n"), outfile
);
169 output_desc
= open (outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
171 error (EXIT_FAILURE
, errno
, "%s", outfile
);
173 if (full_write (output_desc
, bp
, bytes
) < 0)
174 error (EXIT_FAILURE
, errno
, "%s", outfile
);
177 /* Read NCHARS bytes from the input file into BUF.
178 Return the number of bytes successfully read.
179 If this is less than NCHARS, do not call `stdread' again. */
182 stdread (char *buf
, int nchars
)
185 int to_be_read
= nchars
;
189 n_read
= safe_read (input_desc
, buf
, to_be_read
);
194 to_be_read
-= n_read
;
197 return nchars
- to_be_read
;
200 /* Split into pieces of exactly NCHARS bytes.
201 Use buffer BUF, whose size is BUFSIZE. */
204 bytes_split (int nchars
, char *buf
, int bufsize
)
207 int new_file_flag
= 1;
209 int to_write
= nchars
;
214 n_read
= stdread (buf
, bufsize
);
216 error (EXIT_FAILURE
, errno
, "%s", infile
);
221 if (to_read
< to_write
)
223 if (to_read
) /* do not write 0 bytes! */
225 cwrite (new_file_flag
, bp_out
, to_read
);
233 cwrite (new_file_flag
, bp_out
, to_write
);
241 while (n_read
== bufsize
);
244 /* Split into pieces of exactly NLINES lines.
245 Use buffer BUF, whose size is BUFSIZE. */
248 lines_split (int nlines
, char *buf
, int bufsize
)
251 char *bp
, *bp_out
, *eob
;
252 int new_file_flag
= 1;
257 n_read
= stdread (buf
, bufsize
);
259 error (EXIT_FAILURE
, errno
, "%s", infile
);
265 while (*bp
++ != '\n')
266 ; /* this semicolon takes most of the time */
269 if (eob
!= bp_out
) /* do not write 0 bytes! */
271 cwrite (new_file_flag
, bp_out
, eob
- bp_out
);
279 cwrite (new_file_flag
, bp_out
, bp
- bp_out
);
286 while (n_read
== bufsize
);
289 /* Split into pieces that are as large as possible while still not more
290 than NCHARS bytes, and are split on line boundaries except
291 where lines longer than NCHARS bytes occur. */
294 line_bytes_split (int nchars
)
300 char *buf
= (char *) xmalloc (nchars
);
304 /* Fill up the full buffer size from the input file. */
306 n_read
= stdread (buf
+ n_buffered
, nchars
- n_buffered
);
308 error (EXIT_FAILURE
, errno
, "%s", infile
);
310 n_buffered
+= n_read
;
311 if (n_buffered
!= nchars
)
314 /* Find where to end this chunk. */
315 bp
= buf
+ n_buffered
;
316 if (n_buffered
== nchars
)
318 while (bp
> buf
&& bp
[-1] != '\n')
322 /* If chunk has no newlines, use all the chunk. */
324 bp
= buf
+ n_buffered
;
326 /* Output the chars as one output file. */
327 cwrite (1, buf
, bp
- buf
);
329 /* Discard the chars we just output; move rest of chunk
330 down to be the start of the next chunk. Source and
331 destination probably overlap. */
332 n_buffered
-= bp
- buf
;
334 memmove (buf
, bp
, n_buffered
);
341 main (int argc
, char **argv
)
343 struct stat stat_buf
;
344 int num
; /* numeric argument from command line */
347 type_undef
, type_bytes
, type_byteslines
, type_lines
, type_digits
348 } split_type
= type_undef
;
349 int in_blk_size
; /* optimal block size of input file device */
350 char *buf
; /* file i/o buffer */
354 int digits_optind
= 0;
356 program_name
= argv
[0];
357 setlocale (LC_ALL
, "");
358 bindtextdomain (PACKAGE
, LOCALEDIR
);
359 textdomain (PACKAGE
);
361 /* Parse command line options. */
368 /* This is the argv-index of the option we will read next. */
369 int this_optind
= optind
? optind
: 1;
372 c
= getopt_long (argc
, argv
, "0123456789vb:l:C:", longopts
, (int *) 0);
382 if (split_type
!= type_undef
)
384 error (0, 0, _("cannot split in more than one way"));
385 usage (EXIT_FAILURE
);
387 split_type
= type_bytes
;
388 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "bkm") != LONGINT_OK
389 || tmp_long
< 0 || tmp_long
> INT_MAX
)
391 error (0, 0, _("%s: invalid number of bytes"), optarg
);
392 usage (EXIT_FAILURE
);
394 accum
= (int) tmp_long
;
398 if (split_type
!= type_undef
)
400 error (0, 0, _("cannot split in more than one way"));
401 usage (EXIT_FAILURE
);
403 split_type
= type_lines
;
404 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
405 || tmp_long
< 0 || tmp_long
> INT_MAX
)
407 error (0, 0, _("%s: invalid number of lines"), optarg
);
408 usage (EXIT_FAILURE
);
410 accum
= (int) tmp_long
;
414 if (split_type
!= type_undef
)
416 error (0, 0, _("cannot split in more than one way"));
417 usage (EXIT_FAILURE
);
420 split_type
= type_byteslines
;
421 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "bkm") != LONGINT_OK
422 || tmp_long
< 0 || tmp_long
> INT_MAX
)
424 error (0, 0, _("%s: invalid number of bytes"), optarg
);
425 usage (EXIT_FAILURE
);
427 accum
= (int) tmp_long
;
440 if (split_type
!= type_undef
&& split_type
!= type_digits
)
442 error (0, 0, _("cannot split in more than one way"));
443 usage (EXIT_FAILURE
);
445 if (digits_optind
!= 0 && digits_optind
!= this_optind
)
446 accum
= 0; /* More than one number given; ignore other. */
447 digits_optind
= this_optind
;
448 split_type
= type_digits
;
449 accum
= accum
* 10 + c
- '0';
457 usage (EXIT_FAILURE
);
463 printf ("split (%s) %s\n", GNU_PACKAGE
, VERSION
);
470 /* Handle default case. */
471 if (split_type
== type_undef
)
473 split_type
= type_lines
;
479 error (0, 0, _("invalid number"));
480 usage (EXIT_FAILURE
);
484 /* Get out the filename arguments. */
487 infile
= argv
[optind
++];
490 outbase
= argv
[optind
++];
494 error (0, 0, _("too many arguments"));
495 usage (EXIT_FAILURE
);
498 /* Open the input file. */
499 if (!strcmp (infile
, "-"))
503 input_desc
= open (infile
, O_RDONLY
);
505 error (EXIT_FAILURE
, errno
, "%s", infile
);
508 /* No output file is open now. */
511 /* Copy the output file prefix so we can add suffixes to it.
512 26**29 is certainly enough output files! */
514 outfile
= xmalloc (strlen (outbase
) + 30);
515 strcpy (outfile
, outbase
);
516 outfile_mid
= outfile
+ strlen (outfile
);
517 outfile_end
= outfile_mid
+ 2;
518 memset (outfile_mid
, 0, 30);
519 outfile_mid
[0] = 'a';
520 outfile_mid
[1] = 'a' - 1; /* first call to next_file_name makes it an 'a' */
522 /* Get the optimal block size of input device and make a buffer. */
524 if (fstat (input_desc
, &stat_buf
) < 0)
525 error (EXIT_FAILURE
, errno
, "%s", infile
);
526 in_blk_size
= ST_BLKSIZE (stat_buf
);
528 buf
= xmalloc (in_blk_size
+ 1);
534 lines_split (num
, buf
, in_blk_size
);
538 bytes_split (num
, buf
, in_blk_size
);
541 case type_byteslines
:
542 line_bytes_split (num
);
549 if (close (input_desc
) < 0)
550 error (EXIT_FAILURE
, errno
, "%s", infile
);
551 if (output_desc
>= 0 && close (output_desc
) < 0)
552 error (EXIT_FAILURE
, errno
, "%s", outfile
);