home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 9
/
FreshFishVol9-CD2.bin
/
bbs
/
util
/
ssplit-1.3.lha
/
SSplit
/
ssplit.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-14
|
10KB
|
267 lines
/*
* ssplit
*
* Copyright © 1992-1994 Stefan Sticht
* All Rights Reserved
*
* V1.2 5-12-93 Stefan made options[] global to initialize it with 0s
* V1.3 14-02-93 Stefan recompiled with SAS/C 6.51
*/
#define VERSION "1.3"
#define PROGNAME "ssplit"
#define DATE "14.2.94"
/******************************************************************************
* includes *
******************************************************************************/
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#define USE_BUILTIN_MATH
#include <string.h>
#include <exec/execbase.h>
#include <exec/memory.h>
#include <clib/alib_protos.h>
#include <clib/alib_stdio_protos.h>
#include <clib/dos_protos.h>
#include <pragmas/dos_pragmas.h>
#include <clib/exec_protos.h>
#include <pragmas/exec_sysbase_pragmas.h>
/******************************************************************************
* global data *
******************************************************************************/
/*
* programtitle and version for Version command
*/
static char version[] = "\0$VER: " PROGNAME " " VERSION " (" DATE ") © 1992-1994 Stefan Sticht";
#define TITLE_OFFSET 7l
/*
* command line args
*/
#define TEMPLATE "FROM/A,TO/A,SIZE/K/N,BUFFER/K/N,DDOFS/S,DDMSDOS/S,HDFFS/S,HDMSDOS/S"
#define OPTN_FROM 0
#define OPTN_TO 1
#define OPTN_SIZE 2
#define OPTN_BUFFER 3
#define OPTN_DDOFS 4
#define OPTN_DDMSDOS 5
#define OPTN_HDFFS 6
#define OPTN_HDMSDOS 7
#define OPTN_COUNT 8
#define FROM (unsigned char *)options[OPTN_FROM]
#define TO (unsigned char *)options[OPTN_TO]
#define DDOFS options[OPTN_DDOFS]
#define DDMSDOS options[OPTN_DDMSDOS]
#define HDFFS options[OPTN_HDFFS]
#define HDMSDOS options[OPTN_HDMSDOS]
#define DEFAULT_SIZE 1730 /* double density fastfilesystem */
#define BLOCKSIZE 512
#define MAXFILENAMELEN 30
ULONG __saveds
pseudomain(void)
{
struct Library *SysBase = (*(struct Library **)4);
struct Library *DOSBase;
struct Process *thisprocess;
ULONG rc = 0;
thisprocess = (struct Process *)FindTask(NULL);
if (!(thisprocess->pr_CLI))
{
/*
* startet from Workbench: return immediatly
*/
struct WBStartup *msg;
WaitPort(&(thisprocess->pr_MsgPort));
msg = (struct WBStartup *)GetMsg(&(thisprocess->pr_MsgPort));
Forbid();
ReplyMsg((struct Message *)msg);
return(0);
}
if (DOSBase = OpenLibrary("dos.library", 37))
{
long options[OPTN_COUNT];
struct RDArgs *args;
memset(options, 0, sizeof(long) * OPTN_COUNT);
/*
* parse command line
*/
if (args = ReadArgs((UBYTE *)TEMPLATE, options, NULL))
{
unsigned long maxbuffers = ULONG_MAX;
unsigned long size = DEFAULT_SIZE;
if (options[OPTN_BUFFER]) maxbuffers = *((long *)options[OPTN_BUFFER]);
if (options[OPTN_SIZE]) size = *((long *)options[OPTN_SIZE]);
else if (DDOFS) size = 1648;
else if (DDMSDOS) size = 1425;
else if (HDFFS) size = 3466;
else if (HDMSDOS) size = 2854;
size *= BLOCKSIZE;
if (FROM && *FROM)
{
BPTR fromfh;
if (fromfh = Open(FROM, MODE_OLDFILE))
{
char *dest;
unsigned long destsize = (TO ? strlen(TO) : 0) + MAXFILENAMELEN + 2;
if (dest = AllocMem(destsize, MEMF_ANY))
{
char *filename = FilePart(FROM);
void *buffer;
strcpy(dest, TO);
if (AddPart(dest, filename, destsize))
{
unsigned long buffersize = AvailMem(MEMF_LARGEST) / 2;
buffersize = min(size, buffersize);
buffersize = min(maxbuffers, buffersize);
if (buffer = AllocMem(buffersize, MEMF_ANY))
{
BPTR destfh = NULL;
long readsize;
unsigned long part = 1;
unsigned long read;
unsigned long toread;
unsigned short destlen = strlen(dest);
unsigned short filenamelen = strlen(filename);
unsigned short newfilenamelen;
unsigned char ready = FALSE;
char partbuffer[12];
while (!ready)
{
read = 0;
toread = buffersize;
while ((read < size) && !ready)
{
readsize = Read(fromfh, buffer, toread);
if (readsize == -1)
{
PrintFault(IoErr(), NULL);
ready = TRUE;
rc = RETURN_ERROR;
}
else if (readsize != 0)
{
read += readsize;
toread = min(buffersize, size - read);
if (!destfh)
{
sprintf(partbuffer, ".%lu", part);
newfilenamelen = filenamelen + strlen(partbuffer);
strcpy(&dest[(newfilenamelen > MAXFILENAMELEN) ? destlen - (newfilenamelen - MAXFILENAMELEN) : destlen], partbuffer);
if (!(destfh = Open(dest, MODE_NEWFILE)))
{
PrintFault(IoErr(), NULL);
ready = TRUE;
rc = RETURN_ERROR;
}
}
if (!rc)
{
if (Write(destfh, buffer, readsize) != readsize)
{
PrintFault(IoErr(), NULL);
ready = TRUE;
rc = RETURN_ERROR;
}
}
}
else ready = TRUE; /* EOF reached */
} /* while ((read <= size) && !ready) */
if (destfh)
{
Close(destfh);
destfh = NULL;
}
if (!rc)
{
if (part < ULONG_MAX) part++;
else
{
PrintFault(ERROR_BUFFER_OVERFLOW, NULL);
ready = TRUE;
rc