home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
EFFO
/
forum23.lzh
/
f23b
/
SOFTWARE
/
SPLIT
/
split.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-07-01
|
7KB
|
283 lines
# include <stdio.h>
# include <math.h>
# ifdef OSK
# include <modes.h>
extern char *malloc();
# endif
# ifdef __TURBOC__
# include <fcntl.h>
# include <sys/stat.h>
# include <io.h>
# include <string.h>
# define _gs_size(X) filelength(X)
# define _gs_eof(X) eof(X)
# define read _read
# define write _write
# include <alloc.h>
# endif
# define FALSE 0
# define TRUE 1
# define ERR -1
# define LINEBUF 1024
# define BLOCKBUF 4096
char help[]=
"Supplementary file maintenance utility\n\
Written by Carsten Emde, may be copied for non-commercial use\n\
Syntax: split [<opts>] <path> [<opts>]\n\
Function: split file <path> into several parts\n\
Options:\n\
-o=<path> output files will be named <path>aa, <path>ab, ...,\n\
<path>zz (default <path> is input file name)\n\
-c=<num> line number to cut at\n\
-b=<num> file length in byte to cut at\n\
-n output files will be named <path>00, <path>01, ...,\n\
<path>99\n\n";
extern int errno;
void prmiss();
void main(argc,argv)
int argc;
char *argv[];
{
char *p, *s, *fs, unknown = FALSE, quest = FALSE, nomatch = FALSE,
numeric = FALSE;
char *outfilnam = NULL, *buffer, outnam[32];
unsigned long binary = 0, lines = 0, bufferlen, toread, actread,
blockremaining, allremaining;
int fn = 0;
int infile, outfile;
register int i, j;
while(--argc > 0 && (*++argv)[0] != '\0') {
s = argv[0];
if(*s == '-')
for (s++; *s != '\0' && !unknown && !nomatch; s++) {
switch(*s) {
case 'b':
if ((*++s)!='=') {
nomatch = TRUE;
prmiss(s);
}
else {
binary = (unsigned long) atoi(++s);
if (binary == 0) {
nomatch = TRUE;
prmiss(s - 1);
}
for(; *s!='\0'; ++s); s--;
}
break;
case 'c':
if ((*++s) != '=') {
nomatch = TRUE;
prmiss(s);
} else {
lines = (unsigned long) atoi(++s);
if (lines == 0) {
nomatch = TRUE;
prmiss(s - 1);
}
for (; *s!='\0'; ++s); s--;
}
break;
case 'n':
numeric = TRUE;
break;
case 'o':
if ((*++s) != '=') {
nomatch = TRUE;
prmiss(s);
} else {
outfilnam = ++s;
for(; *s!='\0'; ++s); s--;
}
break;
case '?':
quest = TRUE;
break;
default:
unknown = TRUE;
p=s;
break;
}
} else {
if (fn > 0)
exit(_errmsg(0,"too many file names\n"));
else {
fs = s;
fn++;
}
}
if (quest || unknown) break;
}
if (quest) {
printf(help);
exit(0);
}
if (unknown) {
fprintf(stderr,help);
exit(_errmsg(1, "unrecognized symbol '%c'.\n",*p));
}
if (lines && binary)
exit(_errmsg(1, "-b and -c options are mutually exclusive.\n"));
if (!lines && !binary)
exit(_errmsg(1, "either -b or -c option MUST be specified.\n"));
if (fn==0)
exit(_errmsg(1, "at least one filename must be given.\n"));
# ifdef OSK
if ((infile = open(fs, S_IREAD)) == ERR)
exit(_errmsg(errno, "can't open '%s' due to ", fs));
# endif
# ifdef __TURBOC__
if ((infile = open(fs, O_RDONLY | O_BINARY)) == ERR)
exit(_errmsg(errno, "can't open '%s' due to ", fs));
# endif
if (binary) {
bufferlen = BLOCKBUF;
allremaining = _gs_size(infile);
}
else
bufferlen = LINEBUF;
if ((buffer = malloc(bufferlen)) == NULL)
exit(_errmsg(errno,"can't get enough memory, program aborted.\n"));
if (outfilnam == NULL)
outfilnam = fs;
i = 0;
for (;;) {
if (numeric)
sprintf(outnam, "%s%02d", outfilnam, i + 1);
else
sprintf(outnam, "%s%c%c", outfilnam, 'a' + i / ('z'-'a'), 'a' + i % ('z' - 'a'));
# ifdef OSK
outfile = create(outnam, S_ISIZE, S_IWRITE + S_IREAD + S_IOREAD, bufferlen * 32);
if(outfile == ERR)
exit(_errmsg(errno, "can't create '%s' due to ", outnam));
close(outfile);
outfile = open(outnam, S_IWRITE);
# endif
# ifdef __TURBOC__
if((outfile = open(outnam, O_CREAT | O_EXCL | O_WRONLY | O_BINARY,
S_IREAD | S_IWRITE)) == ERR)
exit(_errmsg(errno, "can't create '%s' due to ", outnam));
# endif
if(binary) {
if (allremaining < binary)
blockremaining = allremaining;
else
blockremaining = binary;
for(;;) {
if (blockremaining < bufferlen)
toread = blockremaining;
else
toread = bufferlen;
if ((actread = read(infile, buffer, (unsigned) toread)) != (unsigned) toread)
if (!_gs_eof(infile))
exit(_errmsg(errno, "can't read all data from '%s' due to ", outnam));
if ((write(outfile, buffer, (unsigned) actread)) != (unsigned) actread)
exit(_errmsg(errno, "can't write all data to '%s' due to ", fs));
blockremaining -= actread;
allremaining -= actread;
if (blockremaining == 0) break;
}
}
else {
for (j = 0; j < lines; j++) {
if ((actread = (unsigned long) readln(infile, buffer, (unsigned) bufferlen)) == 0)
if (!_gs_eof(infile))
exit(_errmsg(errno, "can't read from '%s' due to ", fs));
if ((writeln(outfile, buffer, (unsigned) actread)) == 0)
exit(_errmsg(errno, "can't write to '%s' due to ", outnam));
if (_gs_eof(infile))
break;
}
}
close(outfile);
if (binary) {
if (allremaining == 0)
break;
}
else {
if (_gs_eof(infile))
break;
}
i++;
}
close(infile);
}
void prmiss(cp)
char *cp; {
fprintf(stderr, help);
exit(_errmsg(1, "missing or illegal size for '-%c' option\n", *(cp - 1)));
}
# ifdef __TURBOC__
int readln(handle, buf, len)
int handle;
char *buf;
unsigned len;
{
int r;
char *cp;
cp = buf;
do
r = read(handle, cp, 1);
while (r == 1 && *(cp++) != '\n' && cp-buf < len);
*cp = '\0';
if (r >= 0)
return(cp - buf);
else
return(ERR);
}
int writeln(handle, buf, len)
int handle;
char *buf;
unsigned len;
{
int towrite;
towrite = strchr(buf, '\n') - buf + 1;
if(towrite > len)
towrite = len;
if(write(handle, buf, towrite) != towrite)
towrite = ERR;
return(towrite);
}
int _errmsg(err, str, arg)
char *str;
int err, arg;
{
fprintf(stderr, "split: ");
fprintf(stderr, str, arg);
if(err)
perror("");
return(err);
}
# endif