#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

/* simple program to join pieces of a file split with "splitit".

   Copyright (c) 1993 Pearl Software, Oakland, CA.

*/

char diskbuf[8192];

#ifndef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif

main(argc, argv)
int argc;
char **argv;
{
    FILE *file;
    char outname[1024];
    char basename[1024];
    int next = 0;

#ifdef MSDOS
    _fmode = O_BINARY;
#endif

    if (argc < 3) {
	fprintf(stderr, "Usage: joinit output_file input_base\n");
	return 1;
    }
    if (!strcmp(argv[1], "-"))
	file = stdout;
    else
	file = fopen(argv[1], "w");
    if (!file) {
	perror("joinit");
	return 1;
    }
    strcpy(basename, argv[2]);
    while (1) {
	char buf[8];
	FILE *file2;

	strcpy(outname, basename);
	sprintf(buf, ".%d", next);
	strcat(outname, buf);
	file2 = fopen(outname, "r");
	if (!file2)
	    break;
	while (1) {
	    long actsize = fread(diskbuf, 1, 8192, file2);
	    if (!actsize)
		break;
	    fwrite(diskbuf, 1, actsize, file);
	}
	fclose(file2);
	next++;
    }
    fclose(file);
}

