📄 cmds.c
字号:
*/voidsetprompt(void){ interactive = !interactive; printf("Interactive mode %s.\n", onoff(interactive)); code = interactive;}/* * Toggle metacharacter interpretation * on local file names. */voidsetglob(void){ doglob = !doglob; printf("Globbing %s.\n", onoff(doglob)); code = doglob;}/* * Set debugging mode on/off and/or * set level of debugging. */voidsetdebug(int argc, char *argv[]){ int val; if (argc > 1) { val = atoi(argv[1]); if (val < 0) { printf("%s: bad debugging value.\n", argv[1]); code = -1; return; } } else val = !debug; debug = val; if (debug) options |= SO_DEBUG; else options &= ~SO_DEBUG; printf("Debugging %s (debug=%d).\n", onoff(debug), debug); code = debug > 0;}/* * Set current working directory * on remote machine. */voidcd(int argc, char *argv[]){ if (argc < 2 && !another(&argc, &argv, "remote-directory")) { printf("usage: %s remote-directory\n", argv[0]); code = -1; return; } if (command("CWD %s", argv[1]) == ERROR && code == 500) { if (verbose) printf("CWD command not recognized, trying XCWD\n"); (void) command("XCWD %s", argv[1]); }}/* * Set current working directory * on local machine. */voidlcd(int argc, char *argv[]){ char buf[PATH_MAX]; const char *dir = NULL; if (argc == 1) { /*dir = home;*/ dir = "."; } else if (argc != 2) { printf("usage: %s local-directory\n", argv[0]); code = -1; return; } else { dir = globulize(argv[1]); } if (!dir) { code = -1; return; } if (chdir(dir) < 0) { fprintf(stderr, "local: %s: %s\n", dir, strerror(errno)); code = -1; return; } if (!getcwd(buf, sizeof(buf))) { if (errno==ERANGE) strcpy(buf, "<too long>"); else strcpy(buf, "???"); } printf("Local directory now %s\n", buf); code = 0;}/* * Delete a single file. */voiddelete_cmd(int argc, char *argv[]){ if (argc < 2 && !another(&argc, &argv, "remote-file")) { printf("usage: %s remote-file\n", argv[0]); code = -1; return; } (void) command("DELE %s", argv[1]);}/* * Delete multiple files. */voidmdelete(int argc, char *argv[]){ void (*oldintr)(int); int ointer; char *cp; if (argc < 2 && !another(&argc, &argv, "remote-files")) { printf("usage: %s remote-files\n", argv[0]); code = -1; return; } mname = argv[0]; mflag = 1; oldintr = signal(SIGINT, mabort); (void) sigsetjmp(jabort, 1); while ((cp = remglob(argv,0)) != NULL) { if (*cp == '\0') { mflag = 0; continue; } if (mflag && confirm(argv[0], cp)) { (void) command("DELE %s", cp); if (!mflag && fromatty) { ointer = interactive; interactive = 1; if (confirm("Continue with", "mdelete")) { mflag++; } interactive = ointer; } } } (void) signal(SIGINT, oldintr); mflag = 0;}/* * Rename a remote file. */voidrenamefile(int argc, char *argv[]){ if (argc < 2 && !another(&argc, &argv, "from-name")) goto usage; if (argc < 3 && !another(&argc, &argv, "to-name")) {usage: printf("%s from-name to-name\n", argv[0]); code = -1; return; } if (command("RNFR %s", argv[1]) == CONTINUE) (void) command("RNTO %s", argv[2]);}/* * Get a directory listing * of remote files. */voidls(int argc, char *argv[]){ static char foo[2] = "-"; const char *cmd; if (argc < 2) { argc++, argv[1] = NULL; } if (argc < 3) { argc++, argv[2] = foo; } if (argc > 3) { printf("usage: %s remote-directory local-file\n", argv[0]); code = -1; return; } cmd = argv[0][0] == 'n' ? "NLST" : "LIST"; if (strcmp(argv[2], "-") && (argv[2] = globulize(argv[2]))==NULL) { code = -1; return; } if (strcmp(argv[2], "-") && *argv[2] != '|') if ((argv[2] = globulize(argv[2]))==NULL || !confirm("output to local-file:", argv[2])) { code = -1; return; } recvrequest(cmd, argv[2], argv[1], "w", 0);}/* * Get a directory listing * of multiple remote files. */voidmls(int argc, char *argv[]){ void (*oldintr)(int); int ointer, i; const char *volatile cmd; char *volatile dest; const char *modestr; if (argc < 2 && !another(&argc, &argv, "remote-files")) goto usage; if (argc < 3 && !another(&argc, &argv, "local-file")) {usage: printf("usage: %s remote-files local-file\n", argv[0]); code = -1; return; } dest = argv[argc - 1]; argv[argc - 1] = NULL; if (strcmp(dest, "-") && *dest != '|') if ((dest = globulize(dest))==NULL || !confirm("output to local-file:", dest)) { code = -1; return; } cmd = argv[0][1] == 'l' ? "NLST" : "LIST"; mname = argv[0]; mflag = 1; oldintr = signal(SIGINT, mabort); /* * This just plain seems wrong. */ (void) sigsetjmp(jabort, 1); for (i = 1; mflag && i < argc-1; ++i) { modestr = (i == 1) ? "w" : "a"; recvrequest(cmd, dest, argv[i], modestr, 0); if (!mflag && fromatty) { ointer = interactive; interactive = 1; if (confirm("Continue with", argv[0])) { mflag ++; } interactive = ointer; } } (void) signal(SIGINT, oldintr); mflag = 0;}/* * Do a shell escape */voidshell(const char *arg){ int pid; void (*old1)(int); void (*old2)(int); char shellnam[40]; const char *theshell, *namep; old1 = signal (SIGINT, SIG_IGN); old2 = signal (SIGQUIT, SIG_IGN); if ((pid = fork()) == 0) { for (pid = 3; pid < 20; pid++) (void) close(pid); (void) signal(SIGINT, SIG_DFL); (void) signal(SIGQUIT, SIG_DFL); theshell = getenv("SHELL"); if (theshell == NULL) theshell = _PATH_BSHELL; namep = strrchr(theshell, '/'); if (namep == NULL) namep = theshell; else namep++; (void) strcpy(shellnam,"-"); (void) strcat(shellnam, namep); if (strcmp(namep, "sh") != 0) shellnam[0] = '+'; if (debug) { printf("%s\n", theshell); (void) fflush (stdout); } if (arg) { execl(theshell, shellnam, "-c", arg, NULL); } else { execl(theshell, shellnam, NULL); } perror(theshell); code = -1; exit(1); } if (pid > 0) while (wait(NULL) != pid); (void) signal(SIGINT, old1); (void) signal(SIGQUIT, old2); if (pid == -1) { perror("Try again later"); code = -1; } else { code = 0; }}/* * Send new user information (re-login) */voiduser(int argc, char *argv[]){ char theacct[80]; int n, aflag = 0; if (argc < 2) (void) another(&argc, &argv, "username"); if (argc < 2 || argc > 4) { printf("usage: %s username [password] [account]\n", argv[0]); code = -1; return; } n = command("USER %s", argv[1]); if (n == CONTINUE) { if (argc < 3 ) argv[2] = getpass("Password: "), argc++; n = command("PASS %s", argv[2]); } if (n == CONTINUE) { if (argc < 4) { printf("Account: "); (void) fflush(stdout); fgets(theacct, sizeof(theacct), stdin); argv[3] = theacct; argc++; } n = command("ACCT %s", argv[3]); aflag++; } if (n != COMPLETE) { fprintf(stdout, "Login failed.\n"); return; } if (!aflag && argc == 4) { (void) command("ACCT %s", argv[3]); }}/* * Print working directory. */voidpwd(void){ int oldverbose = verbose; /* * If we aren't verbose, this doesn't do anything! */ verbose = 1; if (command("PWD") == ERROR && code == 500) { printf("PWD command not recognized, trying XPWD\n"); (void) command("XPWD"); } verbose = oldverbose;}/* * Make a directory. */voidmakedir(int argc, char *argv[]){ if (argc < 2 && !another(&argc, &argv, "directory-name")) { printf("usage: %s directory-name\n", argv[0]); code = -1; return; } if (command("MKD %s", argv[1]) == ERROR && code == 500) { if (verbose) printf("MKD command not recognized, trying XMKD\n"); (void) command("XMKD %s", argv[1]); }}/* * Remove a directory. */voidremovedir(int argc, char *argv[]){ if (argc < 2 && !another(&argc, &argv, "directory-name")) { printf("usage: %s directory-name\n", argv[0]); code = -1; return; } if (command("RMD %s", argv[1]) == ERROR && code == 500) { if (verbose) printf("RMD command not recognized, trying XRMD\n"); (void) command("XRMD %s", argv[1]); }}/* * Send a line, verbatim, to the remote machine. */voidquote(int argc, char *argv[]){ if (argc < 2 && !another(&argc, &argv, "command line to send")) { printf("usage: %s line-to-send\n", argv[0]); code = -1; return; } quote1("", argc, argv);}/* * Send a SITE command to the remote machine. The line * is sent verbatim to the remote machine, except that the * word "SITE" is added at the front. */voidsite(int argc, char *argv[]){ if (argc < 2 && !another(&argc, &argv, "arguments to SITE command")) { printf("usage: %s line-to-send\n", argv[0]); code = -1; return; } quote1("SITE ", argc, argv);}/* * Turn argv[1..argc) into a space-separated string, then prepend initial text. * Send the result as a one-line command and get response. */static voidquote1(const char *initial, int argc, char **argv){ register int i, len; char buf[BUFSIZ]; /* must be >= sizeof(line) */ (void) strcpy(buf, initial); if (argc > 1) { len = strlen(buf); len += strlen(strcpy(&buf[len], argv[1])); for (i = 2; i < argc; i++) { buf[len++] = ' '; len += strlen(strcpy(&buf[len], argv[i])); } } if (command("%s", buf) == PRELIM) { while (getreply(0) == PRELIM); }}voiddo_chmod(int argc, char *argv[]){ if (argc < 2 && !another(&argc, &argv, "mode")) goto usage; if (argc < 3 && !another(&argc, &argv, "file-name")) {usage: printf("usage: %s mode file-name\n", argv[0]); code = -1; return; } (void) command("SITE CHMOD %s %s", argv[1], argv[2]);}voiddo_umask(int argc, char *argv[]){ int oldverbose = verbose; verbose = 1; (void) command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]); verbose = oldverbose;}voididle_cmd(int argc, char *argv[]){ int oldverbose = verbose; verbose = 1; (void) command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]); verbose = oldverbose;}/* * Ask the other side for help. */voidrmthelp(int argc, char *argv[]){ int oldverbose = verbose; verbose = 1; (void) command(argc == 1 ? "HELP" : "HELP %s", argv[1]); verbose = oldverbose;}/* * Terminate session and exit. */voidquit(void){ if (connected) disconnect(); pswitch(1); if (connected) { disconnect(); } exit(0);}/* * Terminate session, but don't exit. */voiddisconnect(void){ if (!connected) return; (void) command("QUIT"); if (cout) { (void) fclose(cout); } cout = NULL; connected = 0; data = -1; if (!proxy) { macnum = 0; }}static intconfirm(const char *cmd, const char *file){ char lyne[BUFSIZ]; if (!interactive) return (1);#ifdef __USE_READLINE__ if (fromatty && !rl_inhibit) { char *lineread; snprintf(lyne, BUFSIZ, "%s %s? ", cmd, file); lineread = readline(lyne); if (!lineread) return 0; strcpy(lyne, lineread); free(lineread); } else {#endif printf("%s %s? ", cmd, file); fflush(stdout); if (fgets(lyne, sizeof(lyne), stdin) == NULL) { return 0; }#ifdef __USE_READLINE__ }#endif return (*lyne != 'n' && *lyne != 'N');}void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -