/* filecut.c my own version of file cut utility, which does not have a memory requirement */ #include #include #include #include #include #define BUFFER_SIZE 5000000 int main(int argc, char *argv[]) { struct stat filestat; FILE *infile, *outfile; off_t len, offset; off_t insize; unsigned char buf[BUFFER_SIZE]; int i; int num_reads, tail_len; /* checking command line parameters */ if (argc != 4 && argc != 5) { fprintf(stderr, "Usage: filecut infile outfile length [offset]\n"); exit(1); } if ( (infile=fopen(argv[1], "rb")) == NULL ) { fprintf(stderr, "Can not open input file!\n"); exit(1); } if ( (outfile=fopen(argv[2], "wb")) == NULL ) { fprintf(stderr, "Can not open output file!\n"); exit(1); } if ( (len=atol(argv[3])) < 0 ) { fprintf(stderr, "Invalid length specification!\n"); exit(1); } if (argc == 5) { if ( (offset=atol(argv[4])) < 0 ) { fprintf(stderr, "Invalid offset specification!\n"); exit(1); } } else offset = 0; /* get entir file length */ if ( stat(argv[1], &filestat) ) { fprintf(stderr, "Can not get input file length\n"); exit(1); } insize = filestat.st_size; if (insize < (offset+len) ) { fprintf(stderr, "Offset + Len is larger than filesize\n"); exit(1); } /* cut file */ if ( offset != 0 ) fseek(infile, offset, SEEK_SET); num_reads = len / BUFFER_SIZE; tail_len = len % BUFFER_SIZE; for (i=0;i