📄 fileio.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/stat.h>
#include "enums.h"
#include "scon.h"
#include "command.h"
#include "fileio.h"
extern int hControlSocket;
extern int hDataSocket;
extern int bMode;
extern char szBuffer[1025];
off_t restart_point=0;
static int ControlCHit=0;
jmp_buf abortfile;
char szBuff[1025];
char TotalSizeBuf[128];
int TotalSize;
int valuesize;
char percent[40];
extern char DnszBuff[128];
extern int SubStringV3(const char * OriString, const char * StartToken, const char * EndToken, const int MaxNewStringfLenth, char NewString [ ]);
extern int BsAtoU(const char * ValueString);
extern void BsItoa(const int value, char Buffer [ ]);
void SigHandler(int unused)
{
ControlCHit = 1;
longjmp(abortfile,1);
}
int Get_DownFile_size(char bufsiz[],char bufsiz1[])
{
int countnum=0;
if(SubStringV3(bufsiz,"("," Bytes",128,bufsiz1)!=0)
printf("get Totalsize error!\n");
countnum=BsAtoU(bufsiz1);
return countnum;
}
void GetFile( char *fname1)
{
FILE *fp=NULL;
int fd, nTotal=0, nBytesRead=0, retval, aborted=0;
char *abortstr = "ABOR\r\n", ch;
TotalSizeBuf[0]='\0';
percent[0]='\0';
TotalSize=0;
valuesize=0;
if( !fname1 || ! (*fname1)) {
printf("No file specified.\n");
return;
}
printf("%s\n",fname1);
printf("open file to write!\n");
/*
* open the file with current mode
*/
if(! (fp=fopen(fname1,(bMode==ASCII) ? "wt" : "wb"))) {
perror("file open");
return;
}
if( GetListenSocket() < 0) {
fclose(fp);
return;
}
sprintf(szBuffer,"RETR %s\r\n",fname1);
if(!SendControlMsg(szBuffer,strlen(szBuffer))) {
fclose(fp);
return;
}
GetReply();
if( AcceptConnection() <= 0) {
fclose(fp);
return;
}
fd = fileno(fp);
printf("Type q and hit return to abort\r\n");
printf("size=%s\n",DnszBuff);
/*Get downloading file total size*/
TotalSize=Get_DownFile_size(DnszBuff,TotalSizeBuf);
/*Read Data to local file from server file*/
while( (nBytesRead=ReadDataMsg(szBuffer,1024)) > 0) {
write(fd,szBuffer,nBytesRead);
nTotal+=nBytesRead;
/*calculate file downloading percentage*/
valuesize=100*(nTotal)/(TotalSize);
//printf("%d\n",valuesize);
BsItoa(valuesize,percent);
strcat(percent,"%");
//printf("%s ",percent);
if( CheckInput() ) {
ch = getchar();
if( ch != '\n') {
while( getchar() != '\n') ;
}
if( ch == 'q')
aborted = 1;
}
if( aborted ) {
printf("\r\nAbort: Waiting for server to finish.");
SendControlMsg(abortstr,strlen(abortstr));
break;
}
}
if( aborted ) {
while( (nBytesRead=ReadDataMsg(szBuffer,1024)) > 0);
GetReply();
}
printf("\r\n");
close(fd);
/*close data connection*/
CloseDataConnection(hDataSocket);
GetReply();
}
void Rgetfile(char *rfilename,char *lcfilename)
{
FILE *din=NULL,*fout=NULL,*fp=NULL;
int fd, nTotal=0, nBytesRead=0, retval, aborted=0,completeflag=0;
char *abortstr = "ABOR\r\n", ch;
char localfile[80];
register int c,d;
TotalSizeBuf[0]='\0';
percent[0]='\0';
TotalSize=0;
valuesize=0;
if( !rfilename || ! (*rfilename)) {
printf("No file specified.\n");
return;
}
struct stat stbuf;
int ret;
char *q=NULL,ql=NULL;
/*get filename full path the same with server filename on local directory */
strcpy(localfile,lcfilename);
strcat(localfile,"/");
strcat(localfile,rfilename);
ret=stat(localfile,&stbuf);
if(ret<0)
printf("error stat file!\n");
/*get restart point of old file*/
restart_point=stbuf.st_size;
/*
* open the file with current mode
*/
if(!(fout=fopen(localfile,(bMode==ASCII) ? "wt+" : "wb+"))){
perror("file open");
return;
}
if(! (fp=fopen(rfilename,(bMode==ASCII) ? "rt" : "rb"))) {
perror("file open");
return;
}
if( GetListenSocket() < 0) {
//fclose(fp);
return;
}
sprintf(szBuffer,"REST %ld\r\n",(long)restart_point);
if(!SendControlMsg(szBuffer,strlen(szBuffer))){
// fclose(fp);
return;
}
GetReply();
sprintf(szBuffer,"RETR %s\r\n",rfilename);
if(!SendControlMsg(szBuffer,strlen(szBuffer))) {
printf("error retr!");
//fclose(fp);
return;
}
GetReply();
if( AcceptConnection() <= 0) {
printf("connection error!\n");
fclose(fp);
return;
}
/*Get downloading file total size*/
TotalSize=Get_DownFile_size(DnszBuff,TotalSizeBuf);
//if(restart_point==TotalSize)
//return;
din=data_conn("r");
if(din==NULL)
{
printf("fdopen file error!\n");
return;
}
printf("Type q and hit return to abort\r\n");
if(restart_point&&
lseek(fileno(fout),(long)restart_point,SEEK_SET)<0){
printf("local file error!\n");
}
while((c=read(fileno(din),szBuffer,1024))>0){
if((d=write(fileno(fout),szBuffer,c))!=c)
break;
nBytesRead+=c;
// if(restart_point==TotalSize)
// {
// strcpy(percent,"100%");
// completeflag=1;
// }
// if(completeflag==1) break;
valuesize=100*((int)restart_point+nBytesRead)/(TotalSize);
BsItoa(valuesize,percent);
strcat(percent,"%");
if( CheckInput() ) {
ch = getchar();
if( ch != '\n') {
while( getchar() != '\n') ;
}
if( ch == 'q')
aborted = 1;
}
if( aborted ) {
printf("\r\nAbort: Waiting for server to finish.");
SendControlMsg(abortstr,strlen(abortstr));
break;
}
}
if( aborted ) {
while( (nBytesRead=ReadDataMsg(szBuff,1024)) > 0);
GetReply();
}
printf("\r\n");
close(fd);
CloseDataConnection(hDataSocket);
GetReply();
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -