📄 cmds.c
字号:
/* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";#endif /* not lint */#include "tip.h"#include "pathnames.h"/* * tip * * miscellaneous commands */int quant[] = { 60, 60, 24 };char null = '\0';char *sep[] = { "second", "minute", "hour" };static char *argv[10]; /* argument vector for take and put */void timeout(); /* timeout function called on alarm */void stopsnd(); /* SIGINT handler during file transfers */void intcopy(); /* interrupt routine for file transfers *//* * FTP - remote ==> local * get a file from the remote host */getfl(c) char c;{ char buf[256], *cp, *expand(); putchar(c); /* * get the UNIX receiving file's name */ if (prompt("Local file name? ", copyname)) return; cp = expand(copyname); if ((sfd = creat(cp, 0666)) < 0) { printf("\r\n%s: cannot creat\r\n", copyname); return; } /* * collect parameters */ if (prompt("List command for remote system? ", buf)) { unlink(copyname); return; } transfer(buf, sfd, value(EOFREAD));}/* * Cu-like take command */cu_take(cc) char cc;{ int fd, argc; char line[BUFSIZ], *expand(), *cp; if (prompt("[take] ", copyname)) return; if ((argc = args(copyname, argv)) < 1 || argc > 2) { printf("usage: <take> from [to]\r\n"); return; } if (argc == 1) argv[1] = argv[0]; cp = expand(argv[1]); if ((fd = creat(cp, 0666)) < 0) { printf("\r\n%s: cannot create\r\n", argv[1]); return; } sprintf(line, "cat %s;echo \01", argv[0]); transfer(line, fd, "\01");}static jmp_buf intbuf;/* * Bulk transfer routine -- * used by getfl(), cu_take(), and pipefile() */transfer(buf, fd, eofchars) char *buf, *eofchars;{ register int ct; char c, buffer[BUFSIZ]; register char *p = buffer; register int cnt, eof; time_t start; sig_t f; char r; pwrite(FD, buf, size(buf)); quit = 0; kill(pid, SIGIOT); read(repdes[0], (char *)&ccc, 1); /* Wait until read process stops */ /* * finish command */ r = '\r'; pwrite(FD, &r, 1); do read(FD, &c, 1); while ((c&0177) != '\n'); ioctl(0, TIOCSETC, &defchars); (void) setjmp(intbuf); f = signal(SIGINT, intcopy); start = time(0); for (ct = 0; !quit;) { eof = read(FD, &c, 1) <= 0; c &= 0177; if (quit) continue; if (eof || any(c, eofchars)) break; if (c == 0) continue; /* ignore nulls */ if (c == '\r') continue; *p++ = c; if (c == '\n' && boolean(value(VERBOSE))) printf("\r%d", ++ct); if ((cnt = (p-buffer)) == number(value(FRAMESIZE))) { if (write(fd, buffer, cnt) != cnt) { printf("\r\nwrite error\r\n"); quit = 1; } p = buffer; } } if (cnt = (p-buffer)) if (write(fd, buffer, cnt) != cnt) printf("\r\nwrite error\r\n"); if (boolean(value(VERBOSE))) prtime(" lines transferred in ", time(0)-start); ioctl(0, TIOCSETC, &tchars); write(fildes[1], (char *)&ccc, 1); signal(SIGINT, f); close(fd);}/* * FTP - remote ==> local process * send remote input to local process via pipe */pipefile(){ int cpid, pdes[2]; char buf[256]; int status, p; extern int errno; if (prompt("Local command? ", buf)) return; if (pipe(pdes)) { printf("can't establish pipe\r\n"); return; } if ((cpid = fork()) < 0) { printf("can't fork!\r\n"); return; } else if (cpid) { if (prompt("List command for remote system? ", buf)) { close(pdes[0]), close(pdes[1]); kill (cpid, SIGKILL); } else { close(pdes[0]); signal(SIGPIPE, intcopy); transfer(buf, pdes[1], value(EOFREAD)); signal(SIGPIPE, SIG_DFL); while ((p = wait(&status)) > 0 && p != cpid) ; } } else { register int f; dup2(pdes[0], 0); close(pdes[0]); for (f = 3; f < 20; f++) close(f); execute(buf); printf("can't execl!\r\n"); exit(0); }}/* * Interrupt service routine for FTP */voidstopsnd(){ stop = 1; signal(SIGINT, SIG_IGN);}/* * FTP - local ==> remote * send local file to remote host * terminate transmission with pseudo EOF sequence */sendfile(cc) char cc;{ FILE *fd; char *fnamex; char *expand(); putchar(cc); /* * get file name */ if (prompt("Local file name? ", fname)) return; /* * look up file */ fnamex = expand(fname); if ((fd = fopen(fnamex, "r")) == NULL) { printf("%s: cannot open\r\n", fname); return; } transmit(fd, value(EOFWRITE), NULL); if (!boolean(value(ECHOCHECK))) { struct sgttyb buf; ioctl(FD, TIOCGETP, &buf); /* this does a */ ioctl(FD, TIOCSETP, &buf); /* wflushtty */ }}/* * Bulk transfer routine to remote host -- * used by sendfile() and cu_put() */transmit(fd, eofchars, command) FILE *fd; char *eofchars, *command;{ char *pc, lastc; int c, ccount, lcount; time_t start_t, stop_t; sig_t f; kill(pid, SIGIOT); /* put TIPOUT into a wait state */ stop = 0; f = signal(SIGINT, stopsnd); ioctl(0, TIOCSETC, &defchars); read(repdes[0], (char *)&ccc, 1); if (command != NULL) { for (pc = command; *pc; pc++) send(*pc); if (boolean(value(ECHOCHECK))) read(FD, (char *)&c, 1); /* trailing \n */ else { struct sgttyb buf; ioctl(FD, TIOCGETP, &buf); /* this does a */ ioctl(FD, TIOCSETP, &buf); /* wflushtty */ sleep(5); /* wait for remote stty to take effect */ } } lcount = 0; lastc = '\0'; start_t = time(0); while (1) { ccount = 0; do { c = getc(fd); if (stop) goto out; if (c == EOF) goto out; if (c == 0177 && !boolean(value(RAWFTP))) continue; lastc = c; if (c < 040) { if (c == '\n') { if (!boolean(value(RAWFTP))) c = '\r'; } else if (c == '\t') { if (!boolean(value(RAWFTP))) { if (boolean(value(TABEXPAND))) { send(' '); while ((++ccount % 8) != 0) send(' '); continue; } } } else if (!boolean(value(RAWFTP))) continue; } send(c); } while (c != '\r' && !boolean(value(RAWFTP))); if (boolean(value(VERBOSE))) printf("\r%d", ++lcount); if (boolean(value(ECHOCHECK))) { timedout = 0; alarm((int)value(ETIMEOUT)); do { /* wait for prompt */ read(FD, (char *)&c, 1); if (timedout || stop) { if (timedout) printf("\r\ntimed out at eol\r\n"); alarm(0); goto out; } } while ((c&0177) != character(value(PROMPT))); alarm(0); } }out: if (lastc != '\n' && !boolean(value(RAWFTP))) send('\r'); for (pc = eofchars; *pc; pc++) send(*pc); stop_t = time(0); fclose(fd); signal(SIGINT, f); if (boolean(value(VERBOSE))) if (boolean(value(RAWFTP))) prtime(" chars transferred in ", stop_t-start_t); else prtime(" lines transferred in ", stop_t-start_t); write(fildes[1], (char *)&ccc, 1); ioctl(0, TIOCSETC, &tchars);}/* * Cu-like put command */cu_put(cc) char cc;{ FILE *fd; char line[BUFSIZ]; int argc; char *expand(); char *copynamex; if (prompt("[put] ", copyname)) return; if ((argc = args(copyname, argv)) < 1 || argc > 2) { printf("usage: <put> from [to]\r\n"); return; } if (argc == 1) argv[1] = argv[0]; copynamex = expand(argv[0]); if ((fd = fopen(copynamex, "r")) == NULL) { printf("%s: cannot open\r\n", copynamex); return; } if (boolean(value(ECHOCHECK))) sprintf(line, "cat>%s\r", argv[1]); else sprintf(line, "stty -echo;cat>%s;stty echo\r", argv[1]); transmit(fd, "\04", line);}/* * FTP - send single character * wait for echo & handle timeout */send(c) char c;{ char cc; int retry = 0; cc = c; pwrite(FD, &cc, 1);#ifdef notdef if (number(value(CDELAY)) > 0 && c != '\r') nap(number(value(CDELAY)));#endif if (!boolean(value(ECHOCHECK))) {#ifdef notdef if (number(value(LDELAY)) > 0 && c == '\r') nap(number(value(LDELAY)));#endif return; }tryagain: timedout = 0; alarm((int)value(ETIMEOUT)); read(FD, &cc, 1); alarm(0); if (timedout) { printf("\r\ntimeout error (%s)\r\n", ctrl(c)); if (retry++ > 3) return; pwrite(FD, &null, 1); /* poke it */ goto tryagain; }}void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -