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

📄 ftp.c

📁 tftp的示例代码,写得不错,值得一看
💻 C
📖 第 1 页 / 共 3 页
字号:
	}	if (proxy) {		proxtrans(cmd, local, remote);		return;	}	if (curtype != type)		changetype(type, 0);	closefunc = NULL;	oldintr = NULL;	oldintp = NULL;	lmode = "w";	if (setjmp(sendabort)) {		while (cpend) {			getreply(0);		}		if (data >= 0) {			close(data);			data = -1;		}		if (oldintr)			signal(SIGINT,oldintr);		if (oldintp)			signal(SIGPIPE,oldintp);		code = -1;		return;	}	oldintr = signal(SIGINT, abortsend);	if (strcmp(local, "-") == 0)		fin = stdin;	else if (*local == '|') {		oldintp = signal(SIGPIPE,SIG_IGN);		fin = popen(local + 1, "r");		if (fin == NULL) {			error (0, errno, "%s", local + 1);			signal(SIGINT, oldintr);			signal(SIGPIPE, oldintp);			code = -1;			return;		}		closefunc = pclose;	} else {		fin = fopen(local, "r");		if (fin == NULL) {			error (0, errno, "local: %s", local);			signal(SIGINT, oldintr);			code = -1;			return;		}		closefunc = fclose;		if (fstat(fileno(fin), &st) < 0 ||		    (st.st_mode&S_IFMT) != S_IFREG) {			fprintf(stdout, "%s: not a plain file.\n", local);			signal(SIGINT, oldintr);			fclose(fin);			code = -1;			return;		}	}	if (initconn()) {		signal(SIGINT, oldintr);		if (oldintp)			signal(SIGPIPE, oldintp);		code = -1;		if (closefunc != NULL)			(*closefunc)(fin);		return;	}	if (setjmp(sendabort))		goto abort;	if (restart_point &&	    (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {		int rc;		switch (curtype) {		case TYPE_A:			rc = fseek(fin, (long) restart_point, SEEK_SET);			break;		case TYPE_I:		case TYPE_L:			rc = lseek(fileno(fin), restart_point, SEEK_SET);			break;		}		if (rc < 0) {			error (0, errno, "local: %s", local);			restart_point = 0;			if (closefunc != NULL)				(*closefunc)(fin);			return;		}		if (command("REST %ld", (long) restart_point)			!= CONTINUE) {			restart_point = 0;			if (closefunc != NULL)				(*closefunc)(fin);			return;		}		restart_point = 0;		lmode = "r+w";	}	if (remote) {		if (command("%s %s", cmd, remote) != PRELIM) {			signal(SIGINT, oldintr);			if (oldintp)				signal(SIGPIPE, oldintp);			if (closefunc != NULL)				(*closefunc)(fin);			return;		}	} else		if (command("%s", cmd) != PRELIM) {			signal(SIGINT, oldintr);			if (oldintp)				signal(SIGPIPE, oldintp);			if (closefunc != NULL)				(*closefunc)(fin);			return;		}	dout = dataconn(lmode);	if (dout == NULL)		goto abort;	gettimeofday(&start, (struct timezone *)0);	oldintp = signal(SIGPIPE, SIG_IGN);	switch (curtype) {	case TYPE_I:	case TYPE_L:		errno = d = 0;		while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {			bytes += c;			for (bufp = buf; c > 0; c -= d, bufp += d)				if ((d = write(fileno(dout), bufp, c)) <= 0)					break;			if (hash) {				while (bytes >= local_hashbytes) {					putchar('#');					local_hashbytes += hashbytes;				}				fflush(stdout);			}		}		if (hash && bytes > 0) {			if (bytes < local_hashbytes)				putchar('#');			putchar('\n');			fflush(stdout);		}		if (c < 0)			error (0, errno, "local: %s", local);		if (d < 0) {			if (errno != EPIPE)				error (0, errno, "netout");			bytes = -1;		}		break;	case TYPE_A:		while ((c = getc(fin)) != EOF) {			if (c == '\n') {				while (hash && (bytes >= local_hashbytes)) {					putchar('#');					fflush(stdout);					local_hashbytes += hashbytes;				}				if (ferror(dout))					break;				putc('\r', dout);				bytes++;			}			putc(c, dout);			bytes++;	/*		if (c == '\r') {			  	*/	/*		(void)	putc('\0', dout);  // this violates rfc */	/*			bytes++;				*/	/*		}                          			*/		}		if (hash) {			if (bytes < local_hashbytes)				putchar('#');			putchar('\n');			fflush(stdout);		}		if (ferror(fin))			error (0, errno, "local: %s", local);		if (ferror(dout)) {			if (errno != EPIPE)				error (0, errno, "netout");			bytes = -1;		}		break;	}	if (closefunc != NULL)		(*closefunc)(fin);	fclose(dout);	gettimeofday(&stop, (struct timezone *)0);	getreply(0);	signal(SIGINT, oldintr);	if (oldintp)		signal(SIGPIPE, oldintp);	if (bytes > 0)		ptransfer("sent", bytes, &start, &stop);	return;abort:	signal(SIGINT, oldintr);	if (oldintp)		signal(SIGPIPE, oldintp);	if (!cpend) {		code = -1;		return;	}	if (data >= 0) {		close(data);		data = -1;	}	if (dout)		fclose(dout);	getreply(0);	code = -1;	if (closefunc != NULL && fin != NULL)		(*closefunc)(fin);	gettimeofday(&stop, (struct timezone *)0);	if (bytes > 0)		ptransfer("sent", bytes, &start, &stop);}jmp_buf	recvabort;voidabortrecv(sig)  int sig;{	mflag = 0;	abrtflag = 0;	printf("\nreceive aborted\nwaiting for remote to finish abort\n");	fflush(stdout);	longjmp(recvabort, 1);}voidrecvrequest(cmd, local, remote, lmode, printnames)	char *cmd, *local, *remote, *lmode;	int printnames;{	FILE *fout, *din = 0;	int (*closefunc) (FILE *);	sig_t oldintr, oldintp;	int c, d, is_retr, tcrflag, bare_lfs = 0, blksize;	static int bufsize=0;	static char *buf;	long bytes = 0, local_hashbytes = hashbytes;	struct timeval start, stop;	is_retr = strcmp(cmd, "RETR") == 0;	if (is_retr && verbose && printnames) {		if (local && *local != '-')			printf("local: %s ", local);		if (remote)			printf("remote: %s\n", remote);	}	if (proxy && is_retr) {		proxtrans(cmd, local, remote);		return;	}	closefunc = NULL;	oldintr = NULL;	oldintp = NULL;	tcrflag = !crflag && is_retr;	if (setjmp(recvabort)) {		while (cpend) {			getreply(0);		}		if (data >= 0) {			close(data);			data = -1;		}		if (oldintr)			signal(SIGINT, oldintr);		code = -1;		return;	}	oldintr = signal(SIGINT, abortrecv);	if (strcmp(local, "-") && *local != '|') {		if (runique && (local = gunique(local)) == NULL) {			signal(SIGINT, oldintr);			code = -1;			return;		}	}	if (!is_retr) {		if (curtype != TYPE_A)			changetype(TYPE_A, 0);	} else if (curtype != type)		changetype(type, 0);	if (initconn()) {		signal(SIGINT, oldintr);		code = -1;		return;	}	if (setjmp(recvabort))		goto abort;	if (is_retr && restart_point &&	    command("REST %ld", (long) restart_point) != CONTINUE)		return;	if (remote) {		if (command("%s %s", cmd, remote) != PRELIM) {			signal(SIGINT, oldintr);			return;		}	} else {		if (command("%s", cmd) != PRELIM) {			signal(SIGINT, oldintr);			return;		}	}	din = dataconn("r");	if (din == NULL)		goto abort;	if (strcmp(local, "-") == 0)		fout = stdout;	else if (*local == '|') {		oldintp = signal(SIGPIPE, SIG_IGN);		fout = popen(local + 1, "w");		if (fout == NULL) {			error (0, errno, "%s", local+1);			goto abort;		}		closefunc = pclose;	} else {		fout = fopen(local, lmode);		if (fout == NULL) {			error (0, errno, "local: %s", local);			goto abort;		}		closefunc = fclose;	}	blksize = BUFSIZ;	if (blksize > bufsize) {		if (buf)			free(buf);		buf = malloc((unsigned)blksize);		if (buf == NULL) {			error (0, errno, "malloc");			bufsize = 0;			goto abort;		}		bufsize = blksize;	}	gettimeofday(&start, (struct timezone *)0);	switch (curtype) {	case TYPE_I:	case TYPE_L:		if (restart_point &&		    lseek(fileno(fout), restart_point, SEEK_SET) < 0) {			error (0, errno, "local: %s", local);			if (closefunc != NULL)				(*closefunc)(fout);			return;		}		errno = d = 0;		while ((c = read(fileno(din), buf, bufsize)) > 0) {			if ((d = write(fileno(fout), buf, c)) != c)				break;			bytes += c;			if (hash) {				while (bytes >= local_hashbytes) {					putchar('#');					local_hashbytes += hashbytes;				}				fflush(stdout);			}		}		if (hash && bytes > 0) {			if (bytes < local_hashbytes)				putchar('#');			putchar('\n');			fflush(stdout);		}		if (c < 0) {			if (errno != EPIPE)				error (0, errno, "netin");			bytes = -1;		}		if (d < c) {			if (d < 0)				error (0, errno, "local: %s", local);			else				error (0, 0, "%s: short write", local);		}		break;	case TYPE_A:		if (restart_point) {			int i, n, ch;			if (fseek(fout, 0L, SEEK_SET) < 0)				goto done;			n = restart_point;			for (i = 0; i++ < n;) {				if ((ch = getc(fout)) == EOF)					goto done;				if (ch == '\n')					i++;			}			if (fseek(fout, 0L, SEEK_CUR) < 0) {done:				error (0, errno, "local: %s", local);				if (closefunc != NULL)					(*closefunc)(fout);				return;			}		}		while ((c = getc(din)) != EOF) {			if (c == '\n')				bare_lfs++;			while (c == '\r') {				while (hash && (bytes >= local_hashbytes)) {					putchar('#');					fflush(stdout);					local_hashbytes += hashbytes;				}				bytes++;				if ((c = getc(din)) != '\n' || tcrflag) {					if (ferror(fout))						goto break2;					putc('\r', fout);					if (c == '\0') {						bytes++;						goto contin2;					}					if (c == EOF)						goto contin2;				}			}			putc(c, fout);			bytes++;	contin2:	;		}break2:		if (bare_lfs) {			printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs);			printf("File may not have transferred correctly.\n");		}		if (hash) {			if (bytes < local_hashbytes)				putchar('#');			putchar('\n');			fflush(stdout);		}		if (ferror(din)) {			if (errno != EPIPE)				error (0, errno, "netin");			bytes = -1;		}		if (ferror(fout))			error (0, errno, "local: %s", local);		break;	}	if (closefunc != NULL)		(*closefunc)(fout);	signal(SIGINT, oldintr);	if (oldintp)		signal(SIGPIPE, oldintp);	fclose(din);	gettimeofday(&stop, (struct timezone *)0);	getreply(0);	if (bytes > 0 && is_retr)		ptransfer("received", bytes, &start, &stop);	return;abort:/* abort using RFC959 recommended IP,SYNC sequence  */	if (oldintp)		signal(SIGPIPE, oldintr);	signal(SIGINT, SIG_IGN);	if (!cpend) {		code = -1;		signal(SIGINT, oldintr);		return;	}	abort_remote(din);	code = -1;	if (data >= 0) {		close(data);		data = -1;	}	if (closefunc != NULL && fout != NULL)		(*closefunc)(fout);	if (din)		fclose(din);	gettimeofday(&stop, (struct timezone *)0);	if (bytes > 0)		ptransfer("received", bytes, &start, &stop);	signal(SIGINT, oldintr);}/* * Need to start a listen on the data channel before we send the command, * otherwise the server's connect may fail. */intinitconn(){	char *p, *a;	int result, len, tmpno = 0;	int on = 1;	int a0, a1, a2, a3, p0, p1;	if (passivemode) {		data = socket(AF_INET, SOCK_STREAM, 0);		if (data < 0) {			perror("ftp: socket");			return(1);		}		if ((options & SO_DEBUG) &&		    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,			       sizeof (on)) < 0)			perror("ftp: setsockopt (ignored)");		if (command("PASV") != COMPLETE) {			printf("Passive mode refused.\n");			goto bad;		}

⌨️ 快捷键说明

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