Short:        Cut data from FILE with position, and length (with source)
Author:       LouiSe
Uploader:     LouiSe <louise mail ahiv hu>
Type:         util/file
Architecture: m68k-amigaos

Simple file cutter by LouiSe

Usage:  LSFileCut <sourcefile> <targetfile> <position> <bytes>

---------------------------------------------------------

check out our projects @
- http://AMIGAonly.ahol.com   			AMIGAonly, the hungarian Amiga magazin
- http://AMIGAonly.ahol.com/louise/		LouiSe's home
- http://AMIGAonly.ahol.com/amilinux/	Amiga Linux page

--- CUT HERE for THE SOURCE ! ---------------------------

#include	<stdio.h>
#include	<string.h>
#include	<stdlib.h>
#include	<clib/dos_protos.h>

#define	BUFFSIZE 200000UL

void	main(int argc, char *argv[])
{
	BPTR	fhi, fho;
	long	pos,bytes;
	char	buffer[BUFFSIZE];
	long	allbytes=0;
	long	fr=0;
	
	if(argc<5)
	{
		printf("usage:\n");
		printf("      LSFileCut <source filename> <target filename> <position> <bytes>\n");
		exit(0);
	}
	

	if(strchr(argv[3],'x')!=NULL) sscanf(argv[3],"%x",&pos);
	else						      pos=atol(argv[3]);
	if(strchr(argv[4],'x')!=NULL) sscanf(argv[4],"%x",&bytes);
	else						      bytes=atol(argv[4]);

	printf("FileCut v1.05 by LouiSe 1999\n\n");
	printf("Input file   : %s\n",argv[1]);
	printf("Output file  : %s\n",argv[2]);
	printf("Cut position : %lu\n",pos);
	printf("Bytes to Cut : %lu\n",bytes);

	if((fhi=Open(argv[1],MODE_OLDFILE))==NULL)
	{
		printf("*** couldn't open input file: []\n",argv[1]);
		exit(-1);
	}
	else
		{
			if((fho=Open(argv[2],MODE_NEWFILE))==NULL)
			{
				printf("*** couldn't open output file: []\n",argv[2]);
				exit(-2);
			}
			else
				{
					allbytes=0;
					Seek(fhi,pos,OFFSET_BEGINNING);
					while(1)
					{
						fr=Read(fhi,buffer,sizeof(buffer));
						if(fr<=0) break;
						allbytes=allbytes+fr;
						// printf("fr=%lu, allbytes=%lu\n",fr,allbytes);

						if(allbytes<bytes)
						{
							Write(fho,buffer,fr);
						}
						else
							{
								if(fr>bytes)
								{
									Write(fho,buffer,bytes);
								}
								else
									{
										Write(fho,buffer,allbytes-bytes);
									}
									break;
							}
					}
					
					Close(fhi);
					Close(fho);
				}
	
		}

}