|
|
Power 6/32 Unix version 1.21
#include <stdio.h>
/*-----------------------------------------
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>> NOTE >>>>>>>>>>>>> Must be identical to those in "tftpd"
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
read from local file and transform lines ending with '\n' to lines
ending with '\r' and '\n'; also transform '\r' to '\r' and '\0'
when we read a '\n' or '\r' from a file, we do not automatically
store '\r''\n' or '\r' '\0' into buf as we would overflow "buf" but
"delayed write" is used instead. Thus '\r' '\n' can span across two
data packets.
----------------------------------------*/
tftpread(fd, buf, bufsize, fsize)
FILE *fd;
register char *buf;
int bufsize;
int *fsize; /* count # bytes read from file */
{
register char *p;
int i;
/* oldc is to indicate if the transformation is unfinished in the
last block */
static char oldc;
*fsize = 0;
for (p=buf; p < buf+bufsize;) {
switch(oldc) {
case '\n':
*p++ = '\n'; /* already output '\r' */
oldc = '\0';
break;
case '\r':
*p++ = '\0'; /* already output '\r' */
oldc = '\0';
break;
default:
i = fread(&oldc, sizeof(oldc), 1, fd);
if (i<0)
return(i);
if (i==0)
goto endit;
++(*fsize);
switch(oldc) {
case '\n':
case '\r':
*p++ = '\r';
/* will complete the transformation in next
... loop iteration */
break;
default:
*p++ = oldc;
break;
}
}
}
endit:
return(p-buf);
}
/*-----------------------------------------
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> NOTE >>>>>>>>> Must be identical to those "tftpd"
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
write to local file and transform lines ending with '\r''\n' to lines
ending with and '\n'; also transform '\r' '\0' to '\r'.
----------------------------------------*/
tftpwrite(fd, buf, bufsize, fsize)
FILE *fd;
register char *buf;
int bufsize;
int *fsize; /* count # of bytes write to file */
{
register char *p;
char c;
int i;
/* oldc is to indicate if the transformation is unfinished in the
last block */
static char oldc;
*fsize = 0;
for (p=buf; p<buf + bufsize; p++) {
c = *p;
if (oldc == '\r') {
char writethisc;
switch(c) {
case '\0':
writethisc = oldc;
break;
case '\n':
writethisc = '\n';
break;
default:
writethisc = oldc;
break;
}
i = fwrite(&writethisc, sizeof(writethisc), 1, fd);
if (i!=1)
return(-1);
++(*fsize);
if (c=='\0' ||
c=='\n') {
oldc = '\0';
continue;
}
}
if ((oldc = c) == '\r')
continue;
i = fwrite(&c, sizeof(c), 1, fd);
if (i!=1)
return(-1);
++(*fsize);
}
return(*fsize);
}
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.