windows下(cygwin),检查文件是否存在,将结果通过ftp上传

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#define UP_FTP_NAME "up_ftp.txt"

#define COUNT 2

int n=0;

void handle_file(char *file) {
	char c;
	int ret;

	FILE *s_fp1,*d_fp2;

	char hostname[128];
	char writefile[128];
	char nowtime[128];

	struct tm *p1;
	time_t timep;

	timep=time(NULL);
	p1=localtime(&timep);

	// cygwin env
	char cygwin[128];
	sprintf(cygwin,"set CYGWIN=nodosfilewarning");
	system(cygwin);

	gethostname(hostname,sizeof(hostname));
	// printf("%s/n",hostname);
	sprintf(nowtime,"%d-%02d-%02d-%02d-%02d/n",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday,p1->tm_hour,p1->tm_min,p1->tm_sec);
	sprintf(writefile,"%s_%d-%02d-%02d-%02d-%02d.txt",hostname,1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday,p1->tm_hour,p1->tm_min,p1->tm_sec);
	// printf("%s/n",writefile);
	d_fp2=fopen(writefile,"a+");
	if(d_fp2==NULL) {
		fprintf(stderr,"fopen %s error./n",writefile);
		exit(-1);
	}

	if(n++==0) {
		fprintf(d_fp2,"%s/n",nowtime);
		fprintf(d_fp2,"%s/n/n",hostname);
	}

	s_fp1=fopen(file,"r");
	if(s_fp1==NULL) {
		fprintf(stderr,"warning!! %s not exist./n",file);
		fprintf(d_fp2,"warning!! %s not exist./n",file);
	}
	else {
		fprintf(stderr,"ok!! %s exitst./n",file);
		fprintf(d_fp2,"ok!! %s exitst./n",file);

		ret=fseek(s_fp1,0,SEEK_SET);
		if(ret==-1) {
			fprintf(stderr,"fseek error./n");
		}

		if(fscanf(s_fp1,"%c",&c)==EOF) {
			fprintf(stderr,"warning!! %s size is 0./n",file);
			fprintf(d_fp2,"warning!! %s size is 0./nn",file);
		}
		else {
			fprintf(stderr,"ok!! %s size is not 0./n",file);
			fprintf(d_fp2,"ok!! %s size is not 0./n",file);
		}
	}

	ret=fclose(s_fp1);
	if(ret==EOF) {
		fprintf(stderr,"fclose %s error./n",file);
		exit(-1);
	}

	ret=fclose(d_fp2);
	if(ret==EOF) {
		fprintf(stderr,"fclose %s error./n",file);
		exit(-1);
	}
}

ftp_up_cmd() {
	//----------ftp define----------------------
	FILE *ftp_fp;
	int i=0,ret;

	char *up_ftp_parm[9]={"open 127.0.0.1","xunjian","123456","cd shipinshangchuan","bin","put GATEWAY1*","!rm GATEWAY1*","close","bye"};

	char ftp_cmd[128];
	//------------------------------------------

	//----------------------------------------------------------------------
	ftp_fp=fopen(UP_FTP_NAME,"a");
	if(ftp_fp==NULL) {
		fprintf(stderr,"fopen %s error./n",UP_FTP_NAME);
		exit(-1);
	}

	for(i=0;i<9;i++) {
		ret=fprintf(ftp_fp,"%s/n",up_ftp_parm[i]);
		if(ret<0) {
			fprintf(stderr,"fprintf error./n");
			exit(-1);
		}
	}

	ret=fclose(ftp_fp);
	if(ret==EOF) {
		fprintf(stderr,"fclose %s error./n",UP_FTP_NAME);
		exit(-1); 
	}

	ret=sprintf(ftp_cmd,"ftp -s:%s",UP_FTP_NAME);
	if(ret<0) {
		fprintf(stderr,"sprintf ftp_cmd error./n");
		exit(-1);
	}

	// popen(ftp_cmd); // for system
	system(ftp_cmd);

	unlink(UP_FTP_NAME);
	//----------------------------------------------------------------------
}

int main(void) {
	int i;

	struct tm *p1;
	time_t timep;

	char openfile1[128];
	char openfile2[128];

	char *openfile[COUNT];

	timep=time(NULL);
	p1=localtime(&timep);

	/*
	   sprintf(openfile[0],"d://oracle_exp_bak//bwdasic-%d-%02d-%02d.dmp",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday);
	   sprintf(openfile[1],"e://orabak//bwdasic-%d-%02d-%02d.dmp",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday);
	 */
	sprintf(openfile1,"d://oracle_exp_bak//bwdasic-%d-%02d-%02d.dmp",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday);
	sprintf(openfile2,"e://orabak//bwdasic-%d-%02d-%02d.dmp",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday);
	// printf("%s/n",openfile1);

	// handle_file(openfile);

	// handle file cmd
	handle_file(openfile1);

	handle_file(openfile2);

	// execute ftp up cmd
	ftp_up_cmd();

	unlink(UP_FTP_NAME);

	exit(0);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值