1 /* split - split a file Author: Michiel Huisjes */
8 #include <minix/minlib.h>
10 #define CHUNK_SIZE 1024
17 int main(int argc
, char **argv
);
32 if (argc
> 4) usage();
33 for (i
= 1; i
< argc
; i
++) {
34 if (argv
[i
][0] == '-') {
35 if (argv
[i
][1] >= '0' && argv
[i
][1] <= '9'
37 cut_line
= atoi(argv
[i
]);
38 else if (argv
[i
][1] == '\0' && infile
== -1)
42 } else if (infile
== -1) {
43 if ((infile
= open(argv
[i
], O_RDONLY
)) < 0) {
44 std_err("Cannot open input file.\n");
48 strcpy(out_file
, argv
[i
]);
50 if (infile
== -1) infile
= 0;
51 strcat(out_file
, "aa");
52 for (suffix
= out_file
; *suffix
; suffix
++);
55 /* Appendix now points to last `a' of "aa". We have to decrement it by one */
64 register char *index
, *base
;
70 while ((n
= read(infile
, buf
, CHUNK_SIZE
)) > 0) {
74 if (++lines
% cut_line
== 0) {
75 if (fd
== -1) fd
= newfile();
76 if (write(fd
, base
, (int) (index
- base
)) != (int) (index
- base
))
83 if (index
== base
) continue;
84 if (fd
== -1) fd
= newfile();
85 if (write(fd
, base
, (int) (index
- base
)) != (int) (index
- base
))
94 if (++*suffix
> 'z') { /* Increment letter */
95 *suffix
= 'a'; /* Reset last letter */
96 ++*(suffix
- 1); /* Previous letter must be incremented */
97 /* E.g. was `filename.az' */
98 /* Now `filename.ba' */
100 if ((fd
= creat(out_file
, 0644)) < 0) {
101 std_err("Cannot create new file.\n");
109 std_err("Usage: split [-n] [file [name]].\n");
115 std_err("split: write error\n");