⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileio.c

📁 用c写的ftp客户端源代码,可以在UNIX,Windows下编译通过,学习的好代码!
💻 C
字号:
/* dftp: FTP client example * Copyright (c) 1996 by Donald C. Asonye * Email: donald@uh.edu * * fileio.c. * * This and other files are hereby released	to the public for  * educational purposes.  You may redistribute this and other files * or routines as long as you are not compensated for it, and as  * long as this notice is included with it.  You may not use the  * released source codes in anyway commercial without permission. * * It'd be nice if you share your modifications with me and everyone * else.  I encourage you to make the code better as I am very busy. * * Share your knowledge :) */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <signal.h>#include <setjmp.h>#include "enums.h"#include "extfuncs.h"#if( defined(WIN32) || defined(_WIN32))#include <io.h>#include <conio.h>#endifextern int hControlSocket,	   hDataSocket,	   bMode;extern char szBuffer[1024];/* * not used 'cause it did not work :(( */static ControlCHit=0;jmp_buf abortfile;void SigHandler(int unused){   ControlCHit = 1;   longjmp(abortfile,1); }/* * GetFile * called to retrive a file from remote host */void GetFile( char *fname){   FILE *fp=NULL;   int fd, nTotal=0, nBytesRead=0, retval, aborted=0;   char *abortstr = "ABOR\r\n", ch;   /*/void (*OldHandler)(int); */   /*    * did we get a filename?    */   if( !fname || ! (*fname)) {      printf("No file specified.\n");      return;   }   /*    * open the file with current mode    */   if(! (fp=fopen(fname,(bMode==ASCII) ? "wt" : "wb"))) {      perror("file open");      return;   }   /*    * obtain a listen socket    */   if( GetListenSocket() < 0) {       fclose(fp);       return;   }      /*    * send command to server and read response    */   sprintf(szBuffer,"RETR %s\r\n",fname);   if(!SendControlMsg(szBuffer,strlen(szBuffer))) {      fclose(fp);      return;   }   GetReply();      /*    * accept server connection    */   if( AcceptConnection() <= 0) {       fclose(fp);       return;   }   /*     * now get file and store    */      fd = fileno(fp);      /*/ this did not work :((    *  so i am taking it out.  well, i am commenting it out    *  if you figure it out, then use it.  the way i did it    *  now works well.   *//*/   OldHandler = signal(SIGINT,SigHandler);     retval = setjmp(abortfile);   if( retval != 0 ) {      printf("\r\nAborting\r\n");      fflush(stdout);      close(fd);      CloseDataConnection(hDataSocket);      if( OldHandler)	  (void)signal(SIGINT,OldHandler);      ControlCHit = 0;      GetReply();      return;   }*/#if (defined(_WIN32) || defined(WIN32))	   printf("Press ESC to abort\r\n");#else   printf("Type q and hit return to abort\r\n");#endif   while( (nBytesRead=ReadDataMsg(szBuffer,1024)) > 0) {       	   	   write(fd,szBuffer,nBytesRead);	   nTotal+=nBytesRead;	   printf("%s : %d received\r",fname,nTotal);	   if( CheckInput() ) {#if (defined(_WIN32) || defined(WIN32))			if( !(ch = getch()) ) getch();			if( ch == 27 )				aborted = 1;#else		    ch = getchar();			if( ch != '\n') {				while( getchar() != '\n') ;      /* read 'til new line */			}			if( ch == 'q') 				aborted = 1;#endif	   }	   	   /*	    * did we abort?	    */	   if( aborted ) {		  		   printf("\r\nAbort: Waiting for server to finish.");		   SendControlMsg(abortstr,strlen(abortstr));		   break;	   }   }   if( aborted ) {         // ignore everything if aborted.	   while( (nBytesRead=ReadDataMsg(szBuffer,1024)) > 0);	   GetReply();   } /*  (void)signal(SIGINT,OldHandler); */   printf("\r\n");   close(fd);   CloseDataConnection(hDataSocket); /*/  ControlCHit = 0; */   GetReply();}/* * PutFile * called to transfer a file to the remote host using the current * file transfer mode.  it's just like GetFile. * * i have commented out lines that would have helped with trapping * ctrl-c because longjmp did not work :(( * if you figure it out, then uncomment them */void PutFile( char *fname){   FILE *fp=NULL;   int fd, nTotal=0, nBytesRead=0, retval, aborted=0;   char *abortstr = "ABOR\r\n", ch;  /* void (*OldHandler)(int); */   if( !fname || ! (*fname)) {      printf("No file specified.\n");      return;   }   if(! (fp=fopen(fname,(bMode==ASCII) ? "rt" : "rb"))) {      perror("file open");      return;   }   if( GetListenSocket() < 0) {       fclose(fp);       return;   }      /*    * send command to server & read reply    */   sprintf(szBuffer,"STOR %s\r\n",fname);   if(!SendControlMsg(szBuffer,strlen(szBuffer))) {      fclose(fp);      return;   }   GetReply();      /*    * accept server connection    */   if( AcceptConnection() <= 0) {       fclose(fp);       return;   }   /*     * now send file    */      fd = fileno(fp);/*   OldHandler = signal(SIGINT,SigHandler);   retval = setjmp(abortfile);   if( retval != 0 ) {      printf("Aborting\r\n");      fflush(stdout);      close(fd);      CloseDataConnection(hDataSocket);      if( OldHandler)	  (void)signal(SIGINT,OldHandler);      ControlCHit = 0;      GetReply();      return;   }  */#if (defined(_WIN32) || defined(WIN32))	   printf("Press ESC to abort\r\n");#else   printf("Type q and hit return to abort\r\n");#endif   while( (nBytesRead=read(fd,szBuffer,1024)) > 0) {      SendDataMsg(szBuffer,nBytesRead);      nTotal+=nBytesRead;      printf("%s : %d sent\r",fname,nTotal);	   if( CheckInput() ) {#if (defined(_WIN32) || defined(WIN32))			if( !(ch = getch()) ) getch();			if( ch == 27 )				aborted = 1;#else			ch = getchar();			if( ch != '\n') {				while( getchar() != '\n') ;      /* read 'til new line */			}			if( ch == 'q') 				aborted = 1;#endif	   }	   /*	    * send an abort command to server if we aborted.	    */	   if( aborted ) {		  		   printf("\r\nAbort: Waiting for server to finish.");		   SendControlMsg(abortstr,strlen(abortstr));		   break;	   }   }   /*(void)signal(SIGINT,OldHandler); */   printf("\r\n");   /*    * close data connection    */   CloseDataConnection(hDataSocket);   close(fd);   GetReply();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -