?? tftpdfunc.c
字號:
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/ioctl.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdio.h>
#include″tftpdfunc.h″
#define PACKSIZE BLOCKSIZE+4
struct bf{
int counter; /*數據緩沖區的長度或是標志*/
char buf[PACKSIZE]; /*數據緩沖區*/
}buffers[2];
/*bf.counter的標志*/
#define BF_ALLOC-3 /*bf.buf已被分配,但尚未填入數據*/
#define BF_FREE -2 /*bf.buf尚未被分配*/
static int next; /*下一個可用緩沖區*/
static int current; /*當前正在使用的緩沖區*/
/*用于轉換文件傳輸格式的兩個變量*/
int newline=0;
int prevchar=-1; /*前一個字符*/
void read_block(FILE*file,int convert);
int write_block(FILE*file,int convert);
struct tftphdr*rwrite_init(int);
struct tftphdr *write_init(void){return rwrite_init(0);}
struct tftphdr *read_init(void){return rwrite_init(1);}
struct tftpdr*
rwrite_init(int x)
{
newline=0;
prevchar=-1;
buffers[0].counter=BF_ALLOC; /*分配第一塊緩沖區*/
current=0;
buffers[1].counter=BF_FREE;
next=x;
return (struct tftphdr*)buffers[0].buf;
}
int my_read(FILE*file,struct tftphdr **dpp,
int convert/*convert值為1,需轉換成netascii碼*/)
{
struct bf *b;
buffers[current].counter=BF_FREE;
current=!current;
b=&buffers[current];
if(b->counter==BF_FREE) /*緩沖區空*/
read_block(file.convert);
dpp=(struct tftphdr)b->buf;
return b->counter;
}
void read_block(FILE*file,int convert /*值為1, 需轉換成ascii碼*/)
{
int i;
char *p;
int c;
struct bf *b;
struct tftphdr *dp;
b=&buffers[next];
if(b->counter !=BF_FREE)
return;
next=!next;
dp=(struct tftpdr*)b->buf;
if(convert ==0){ /*直接以二進制碼讀出,不需轉換,直接讀出*/
b->counter=read(fileno(file),dp->th_data,BLOCKSIZE);
return;
}
/*需轉換成ascii碼,逐個讀出字符并處理行尾的換行符*/
p=dp->th_data;
for(i=0;i<BLOCKSIZE;i++){
if (newline){
if(prevchar==′\n′)
c=′\n′;
else c=′\0′;
newline=0;
}
else{
c=getc(file);
if(c==EOF)break;
if(c==′\n′||c==′\r′){
prevchar=c;
c=′\r′;
newline=1;
}
}
*p++=c;
}
b->counter=(int)(p-dp->th_data);
}
int my_write(FILE*file,struct tftphdr** dpp,int ct, int convert)
{
buffers[current].counter=ct; /*數據塊的長度*/
current=!current;
if(buffers[current].counter!=BF_FREE)/*有數據*/
write_block(file,convert); /*寫入數據,刷新緩沖區*/
buffers[current].counter=BF_ALLOC; /*標志該緩沖區已分配*/
*dpp=(struct tftphdr)buffers[current].buf;
return ct;
}
int write_block(FILE*file,int convert)
{
char*buf;
int count;
int ct;
char*p;
int c; /*當前讀出的字符*/
struct bf*b;
struct tftphdr*dp;
b=&buffers[next];
if(b->counte <-1) /*數據塊長度*/
b->counter=BF_FREE;
dp=(struct tftphdr*)b->buf;
next=!next;
140 buf=dp->th_data;
if(count<0)
return-1;
if(convert==0)/*不需轉換*/
return= write(fileno(file);buf, count);
p=buf;
ct=count;
while(ct--)
c=*p++;
if(prevchar==′\r′)
if(c==′\n′) /*回車后是換行*/
fseek(file ,-1,1);
else
if(c==′\0′) /*回車后是空字符*/
goto skipit; /*空字符不寫入文件*/
putc(c,file);
skipit;
prevchar=c;
return count;
}
int synch(int f/*套接字*/)
{
int i,j=0;
char rbuf[PACKSIZE];
struct sockaddr_in from;
size_t fromlen;
while(1){
(void) ioctl(f,FIONREAD,&i);
if(i){
j++;
fromlen=sizeof from;
(void)recvfrom(f,rbuf,sizeof(rbuf),0,
(struct sockaddr*)&from,&fromlen);
}else{
return(j);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -