www.pudn.com > onlyC.rar > chhost.c


#include "fileOper.h" 
int NetInsideIs(); 
void CoverFile(); 
 
 
void main() 
{ 
	printf("%s\n","操作中。。。"); 
	CoverFile(); 
	printf("%s\n","操作成功完成!"); 
	printf("%s\n","您现在的IP是"); 
	if(NetInsideIs()==NET_IS_INSIDE) 
	{ 
		printf("一定是内网"); 
	}else  
	{ 
		printf("多数是外网"); 
	} 
 
} 
/* 
 _______________________________________________ 
|FUNCTION		:CoverFile 
|OPERATIN		:把hostfile内的相关地址转为对应的内容 
|RETURN			: 
|PARAMETER		: 
| 
| 
 _______________________________________________ 
 */ 
void CoverFile() 
{ 
	char net_in[3][16]; 
	char net_out[3][16]; 
	char net_host[3][21]; 
	char net_line[100],retchar[3]; 
	char filename[]={"c:\\windows\\Hosts.sam"}; 
	int int_i; 
	FILE *fp; 
	 
	strcpy(net_in[0],"10.2.1.1"); 
	strcpy(net_in[1],"10.2.100"); 
	strcpy(net_in[2],"10.1.254.3"); 
	strcpy(net_out[0],"61.144.246.85"); 
	strcpy(net_out[1],"210.39.32.35"); 
	strcpy(net_out[2],"61.144.246.87"); 
	strcpy(net_host[0],"    www.szpt.net"); 
	strcpy(net_host[1],"    www.szpt.edu.cn"); 
	strcpy(net_host[2],"    oa.szpt.net"); 
	retchar[0]=13,retchar[1]=10;retchar[2]=0; 
 
	/*hosts文件的路径*/ 
	if((fp=fopen(filename,"rb"))==NULL)fp=fopen(filename,"wb+"); 
	fclose(fp); 
 
 
	if(NetInsideIs()==NET_IS_INSIDE) 
	{ 
		/*内网*/ 
		for(int_i=0;int_i<3;int_i++) 
		{ 
			if(!FileReplace(filename,net_out[int_i],net_in[int_i],32)) 
			{ 
				if(FileFindString(filename,net_in[int_i],0,32)==-1) 
				{ 
					strcpy(net_line,retchar); 
					strcat(net_line,net_in[int_i]); 
					strcat(net_line,net_host[int_i]); 
					FileAddString(filename,net_line);//添加一行 
				} 
			} 
		} 
 
	}else if(NetInsideIs()==NET_IS_OUTSIDE) 
	{ 
		/*外网*/ 
		for(int_i=0;int_i<3;int_i++) 
		{ 
			if(!FileReplace(filename,net_in[int_i],net_out[int_i],32)) 
			{ 
				if(FileFindString(filename,net_out[int_i],0,32)==-1) 
				{ 
					strcpy(net_line,retchar); 
					strcat(net_line,net_out[int_i]); 
					strcat(net_line,net_host[int_i]); 
					FileAddString(filename,net_line);//添加一行 
				} 
			} 
		} 
 
	}else  
	{ 
		printf("\n              Net Error\n"); 
	} 
 
 
} 
 
 
/* 
 _______________________________________________ 
|FUNCTION		:NetInsideIs 
|OPERATIN		:判断是否为内网 
|RETURN			:有IP" 10."则为内网(0),NET_IS_INSIDE 
|				 有IP" 61."则为外网(1),NET_ISOUTSIDE 
				 如果都没有就出错(2).NET_IS_ERROR 
|PARAMETER		: 
| 
| 
 _______________________________________________ 
 */ 
int NetInsideIs() 
{ 
	char netInside[]={" 10."},netOutside[]={" 61."}; 
	char tempFile[]={"~xxx0101.tmp"}; 
	char strCommand[30]; 
	int ret_value; 
	strcpy(strCommand,"ipconfig >"); 
	strcat(strCommand,tempFile); 
	system(strCommand); 
 
	if(FileFindString(tempFile,netInside,0,32)!=-1) 
	{ 
		ret_value=NET_IS_INSIDE; 
		goto FunctionQuit; 
	}else if(FileFindString(tempFile,netOutside,0,32)!=-1) 
	{ 
		ret_value=NET_IS_OUTSIDE; 
		goto FunctionQuit; 
	}else 
	{ 
		ret_value=NET_IS_ERROR; 
		goto FunctionQuit; 
	} 
 
FunctionQuit: 
	if(unlink(tempFile)==-1)printf("\n\nFuck worry\n"); 
	return ret_value; 
 
}