1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
3 // Copyright (c) 2001-2003, OpenBeOS
5 // This software is part of the OpenBeOS distribution and is covered
10 // Author: Daniel Reinhold (danielre@users.sf.net)
11 // Description: splits one file into a collection of smaller files
14 // This program was written such that it would have identical output as from
15 // the original chop program included with BeOS R5. However, there are a few
18 // a) using "chop -n" (with no other args) crashes the original version,
21 // b) filenames are enclosed in single quotes here, but are not in the original.
22 // It is generally better to enquote filenames for error messages so that
23 // problems with the name (e.g extra space chars) can be more easily detected.
25 // c) this version checks for validity of the input file (including file size)
26 // before there is any attempt to open it -- this changes the error output
27 // slightly from the original in some situations. It can also prevent some
28 // weirdness. For example, the original version will take a command such as:
30 // chop /dev/ports/serial1
32 // and attempt to open the device. If serial1 is unused, this will actually
33 // block while waiting for data from the serial port. This version will never
34 // encounter that because the device will be found to have size 0 which will
35 // abort immediately. Since the semantics of chop don't make sense for such
36 // devices as the source, this is really the better behavior (provided that
37 // anyone ever attempts to use such strange arguments, which is unlikely).
39 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
52 void chop_file (int, char *, off_t
);
53 void do_chop (char *);
57 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60 #define BLOCKSIZE 64 * 1024 // file data is read in BLOCKSIZE blocks
61 static char Block
[BLOCKSIZE
]; // and stored in the global Block array
63 static int KBytesPerChunk
= 1400; // determines size of output files
69 printf("Usage: chop [-n kbyte_per_chunk] file\n");
70 printf("Splits file into smaller files named file00, file01...\n");
71 printf("Default split size is 1400k\n");
77 main(int argc
, char *argv
[])
82 if ((argc
< 2) || (argc
> 4)) {
89 if (strcmp(first
, "--help") == 0) {
94 if (strcmp(first
, "-n") == 0) {
99 printf("-n option needs a numeric argument\n");
102 KBytesPerChunk
= (b
< 1 ? 1 : b
);
107 printf("no file specified\n");
111 printf("-n option needs a numeric argument\n");
127 // do some checks for validity
128 // then call chop_file() to do the actual read/writes
134 // input file must exist
135 if (stat(fname
, &e
) == -1) {
136 fprintf(stderr
, "'%s': no such file or directory\n", fname
);
140 // and it must be not be a directory
141 if (S_ISDIR(e
.st_mode
)) {
142 fprintf(stderr
, "'%s' is a directory\n", fname
);
146 // needs to be big enough such that splitting it actually does something
148 if (fsize
< (KBytesPerChunk
* 1024)) {
149 fprintf(stderr
, "'%s': file is already small enough\n", fname
);
153 // also, don't chop up if chunk files are already present
160 if (stat(buf
, &e
) >= 0) {
161 fprintf(stderr
, "'%s' already exists - aborting\n", buf
);
166 // finally! chop up the file
167 fd
= open(fname
, O_RDONLY
);
169 fprintf(stderr
, "can't open '%s': %s\n", fname
, strerror(errno
));
171 chop_file(fd
, fname
, fsize
);
178 chop_file(int fdin
, char *fname
, off_t fsize
)
180 const off_t chunk_size
= KBytesPerChunk
* 1024; // max bytes written to any output file
182 bool open_next_file
= true; // when to open a new output file
183 char fnameN
[256]; // name of the current output file (file01, file02, etc.)
184 int index
= 0; // used to generate the next output file name
185 int fdout
= -1; // output file descriptor
187 ssize_t got
; // size of the current data block -- i.e. from the last read()
188 ssize_t put
; // number of bytes just written -- i.e. from the last write()
189 ssize_t needed
; // how many bytes we can safely write to the current output file
190 ssize_t avail
; // how many bytes we can safely grab from the current data block
191 off_t curr_written
= 0; // number of bytes written to the current output file
192 off_t total_written
= 0; // total bytes written out to all output files
194 char *beg
= Block
; // pointer to the beginning of the block data to be written out
195 char *end
= Block
; // end of the current block (init to beginning to force first block read)
197 printf("Chopping up %s into %d kbyte chunks\n", fname
, KBytesPerChunk
);
199 while (total_written
< fsize
) {
201 // read in another block
202 got
= read(fdin
, Block
, BLOCKSIZE
);
207 end
= Block
+ got
- 1;
210 if (open_next_file
) {
211 // start a new output file
212 sprintf(fnameN
, "%s%02d", fname
, index
++);
214 fdout
= open(fnameN
, O_WRONLY
|O_CREAT
);
216 fprintf(stderr
, "unable to create chunk file '%s': %s\n", fnameN
, strerror(errno
));
220 open_next_file
= false;
223 needed
= chunk_size
- curr_written
;
224 avail
= end
- beg
+ 1;
229 put
= write(fdout
, beg
, needed
);
233 total_written
+= put
;
236 if (curr_written
>= chunk_size
) {
237 // the current output file is full
239 open_next_file
= true;
243 // close up the last output file if it's still open