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

📄 main.c

📁 自己写的一个ftp client
💻 C
字号:
#include"global.h"#include"controlmodule.h"#include"parser.h"//////////////////////////////////////////////////////void * xrealloc( void *ptr , int size){	void *p=realloc(ptr,size);	if(p==NULL){		perror("realloc() failed!\n");		exit(1);	}	printf("realloc happened!\n");	return p;}void readline(int fd,struct linebuf *lbuf){	char *start;	//used for recording the start of lastest line	char *buf=lbuf->buf;	start=buf;	int size=lbuf->size;	char c='a';	int count=0;	//the total amount that has been read	while( c!='\n' ){//		printf("first read\n");		if( read( fd , &c , sizeof(char) ) == -1){			perror("read()\n");		}		if( (size--)<=1 ){			char * tmp=lbuf->buf;			size+=lbuf->size;			lbuf->size*=2;	//maybe to fast!!!			lbuf->buf=xrealloc( lbuf->buf , lbuf->size );			start= (lbuf->buf-tmp)+start; 			buf= (lbuf->buf-tmp)+buf;		}		count++;		*(buf++)=c;	}	*buf='\0';	if( *(start+3)=='-' ){//then enter the status that server while send more than one line.//		printf("enter get more than one lines\n");		char n1=*start;		char n2=*(start+1);		char n3=*(start+2);//		printf("%c%c%c\n",n1,n2,n3);		c='a';		start=buf;		while( c!='\n' ){//			printf("count:%d %c before read()\n",count,c);			if( read( fd , &c ,sizeof(char) ) ==-1){				perror("read() in more than oneline precess\n");			}			if( (size--)<=1 ){				char * tmp=lbuf->buf;				size+=lbuf->size;				lbuf->size*=2;	//maybe to fast!!!				lbuf->buf=xrealloc( lbuf->buf , lbuf->size );				start= (lbuf->buf-tmp)+start; 				buf= (lbuf->buf-tmp)+buf;			}			count++;			*(buf++)=c;//			printf("count:%d %cafter read()\n",count,c);			if(c=='\n'){				if( *start==n1 && *(start+1)==n2 && *(start+2)==n3 && *(start+3)==' ' ){					break;				}				start=buf;//				printf("start:%c and address:%d\n",*start,start);				c='a';			}				}	}		printf("count:%d lbuf->size:%d\n",count,lbuf->size);		}void getline(char *buf){	char c;	scanf("%c",&c);	while(c!='\n'){		*(buf++)=c;		scanf("%c",&c);	}	*buf='\0';}struct linebuf* inilinebuf(int size){	struct linebuf *tmp=malloc( sizeof(struct linebuf) );	tmp->size=size;	tmp->buf=malloc( size );	if( tmp==NULL && tmp->buf==NULL ){		perror("malloc failed!\n");		exit(1);	}	return tmp;}void freelinebuf(struct linebuf* lbuf){	free(lbuf->buf);	free(lbuf);}	int main(int argc,char **argv){/////////////////////do some initialization for the global variable//////////////////////////////	status=DISCONNECTED;	username=malloc( sizeof(char)*10 );	remoteaddr=malloc( sizeof(char)*16 );	remotecwd=malloc( sizeof(char)*30 );	remoteostype=malloc( sizeof(char)*20 );	transmittype=malloc( sizeof(char) );	if(username==NULL||remoteaddr==NULL||remotecwd==NULL||remoteostype==NULL){		perror("malloc failed!\n");	}	//do sth. about argument	//get user command	char *command=malloc( sizeof(char)*100 );	char c;	char *p=command;	int tag;	printf("ftp:>");	while(1){		tag=0;		scanf("%c",&c);		while(c!='\n'){			*(command+tag++)=c;		//	printf("%c",c);			scanf("%c",&c);		}		*(command+tag)='\0';		printf("%d\n",(int)*p);		for(;*p!='\0';p++){			printf( "%d ",(int)*p );		}		printf("\n%s\n",command);		for(int i=0;i<=tag;i++){			printf("%d ",(int)*(command+i));		}		printf("\n");		parser(command); //parse the user command here!		printf("ftp:>");	}	exit(0);}

⌨️ 快捷键说明

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