#include #include #include /* This program cuts the specified file into parts not to exceed the specified number of bytes. The names of the pieces are generated by replacing the extension of the original file name with `001', `002', etc. E.g., from foo.arj, files foo.001, foo.002, foo.003, etc. will be produced. Those parts can be later put together with `cat' or `copy', e.g. cat foo.001 foo.002 foo.003 > foo.arj or copy /b foo.001 + foo.002 + foo.003 foo.arj */ typedef char * str; typedef int logical; #define oops(s) { fprintf(stderr, "%s\n", s); exit(1); } const int LB = 16384; static long int N=0; static char name[80], ext[80], outname[80]; static void ini_out(), term_out(), write_out(const char b[], int d); int main(int argc, str argv[]) { FILE *f; char *buffer; char *p, *fname; int d; if (argc != 3) { printf("Usage:\n"); printf("\t%s \n", argv[0]); printf("to divide file into pieces bytes each\n"); exit(0); } N = atol(argv[1]); if (N <=0) { printf("Invalid part size specified: %d\n", N); exit(0); } buffer = (char*)malloc(LB); if (!buffer) oops("Out of memory!"); fname = argv[2]; f = fopen( fname, "rb"); if (!f) { fprintf(stderr, "Can't open file `%s'\n", fname); exit(1); } p = strchr( fname, '.'); /* pointer to the '.' in source file name */ /* Finding the _last_ `.' in the file name */ while(p) { str q = strchr(p+1,'.'); if (!q) break; p = q; } /* Name & extension of the source file */ if (p) strncpy(name, fname, (int) (p-fname)); else strcpy( name, fname); if (p) strcpy( ext, p); else ext[0]=0; ini_out(); while( d = fread( buffer, 1, LB, f), d) write_out( buffer, d); term_out(); fclose(f); return 0; } static FILE *g; static long int count; static int ou_no; static void ini_out() { g = NULL; count = 0; ou_no = 0; } static void term_out() { fclose(g); } /* Open a new output file */ static FILE * new_file() { FILE *g; char ou_name[128]; ou_no++; sprintf(ou_name, "%s.%03d", name, ou_no); g = fopen( ou_name, "wb"); if (!g) { fprintf(stderr,"Can't write to `%s'\n", ou_name); exit(2); } else printf("Writing `%s'...\n", ou_name); return g; } /* Writes out d characters */ static void write_out(const char b[], int d) { int k; logical final; /* Open new output file, if necessary */ if (!g) g = new_file(); final = (count + d >= N); k = final ? (N - count) : d; fwrite( b, 1, k, g); if (final) { fclose(g); g = NULL; count = 0; if (k