#ifdef	ournix
#include "ournix.h"
#endif
char sccsID[] = "@(#) split.c V1.0 Copyright Julian H. Stacey 1990 07 05.\n";

/* FUNCTION :  like a crude unix split */

#include	<stdio.h>
#include	<fcntl.h>
#ifdef ns32000	/* { */
#define BSD
#endif		/* } */
#ifdef	BSD	/* { */
#include	<sys/file.h>
#endif		/* } */
char **ARGV;
typedef	char	FLAG ;
typedef	int	fd ;

main(argc,argv)
	int	argc ;
	char	**argv ;
	{
	extern int atoi() ;
	extern char *malloc() ;
	int	block_size ;
	long 	wr_cumulative = 0L ;
	long 	rd_cumulative = 0L ;
	fd	fd_rd, fd_wr ;
	char	out_name[30] ;
	FLAG	flush_f ;
	unsigned char *base_p, *tmp_p ;
	unsigned contents ;
	int	got ;
	int	want ;
	FLAG	verbose_f = 1 ;
	int	write_no = 0 ;
	char 	*in_name ;

	ARGV = argv ;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */
	if (argc != 3) syntax() ;
	block_size = atoi(*++argv) ;
	in_name = *++argv ;
	if (block_size <= 0) syntax() ;
	/* make a buffer */
	if ((base_p = (unsigned char *)malloc((unsigned)block_size)) ==
		(unsigned char *)0)
		{
		fprintf(stderr,"%s: Error: malloc(%d) failed.\n",*ARGV,
			block_size) ;
		perror(*ARGV) ;
		exit(1) ;
		}
		if ((fd_rd = open(in_name,O_RDONLY  
#ifdef	MSDOS	/* { */
							| O_BINARY
#endif		/* } */
				)) == -1)
			{
			fprintf(stderr,
				"%s: Failed to open(read) %s\n",*ARGV,in_name) ;
			perror(*ARGV) ;
			exit(1) ;
			}
	for(;;)
		{
		/* write blocks forever */
		tmp_p = base_p ;
		contents = 0 ;
		flush_f = 0 ;
		while((contents < block_size) && (flush_f == 0) )
			{
			/* read enough for a block */
			want = block_size - contents ;
			if (verbose_f) fprintf(stderr,
				"%s Started reading %d bytes.\n",
				*ARGV, (int)want) ;
				fflush(stderr);
			got = read(fd_rd, (char *)tmp_p, (int)want ) ;
			if (verbose_f) fprintf(stderr,
				"%s Finished reading %d bytes.\n",
				*ARGV, got) ; 
				fflush(stderr);
			if (got > 0) 
				{
				tmp_p += got ;
				contents += got ;
				rd_cumulative += got ;
				}
			if (got == 0) flush_f = 1 ;
			if (got < 0) 
				{
				fprintf(stderr,
				"%s: Error: read error after %ld previous bytes.\n",
					rd_cumulative ) ;
				exit(1) ;
				}
			}
		sprintf(out_name,"s_%06d",write_no++);
		if (verbose_f) fprintf(stderr,
			"%s About to write %d bytes to %s.\n",
			*ARGV, (int)contents,out_name) ;
			fflush(stderr);
		if ((fd_wr = open(out_name,O_WRONLY | O_CREAT | O_TRUNC
#ifdef	MSDOS	/* { */
#define S_IWRITE 0000200	/* write permission, owner JJ fix def later*/
			| O_BINARY , S_IWRITE
#else		/* } { */
				,0640
#endif		/* } */
				)) == -1)
			{
			fprintf(stderr,
				"%s: Failed to open %s\n",*ARGV,out_name) ;
			perror(*ARGV) ;
			exit(1) ;
			}
		if ( (got = write(fd_wr, (char *)base_p, (int)contents ) )
			!= contents )
			{
			fprintf(stderr,
	"%s: Error: Wrote %d bytes after %ld bytes previously written.\n",
				*ARGV, got, wr_cumulative) ;
			exit(1) ;
			}
		if (verbose_f) fprintf(stderr,
			"%s Finished writing %d bytes.\n",
			*ARGV, got) ;
			fflush(stderr);
		wr_cumulative += contents ;
		if (flush_f == 1) break ;
		close(fd_wr);
		}
	close(fd_rd);
	exit(0);
	}

syntax()
	{
	fprintf(stderr,
	"Syntax Error:\nWriting_process | %s block_size | Reading_process\n",
		*ARGV);
	exit(1) ;
	}
