int main(int argc, char **argv) { skod_t skod; ftp_t ftp; skod_init(&skod); skod_parse_cla(argc, argv, &skod); signal(SIGINT, signal_handler); /* Init FTP*/ ftp.user = skod.user; ftp.password = skod.password; ftp.server = skod.server; ftp.port = skod.port; ftp.alarm_sec = 1; /* High risk */ ftp_mkcon(&ftp); /* --dest, -e*/ if ( skod.dest != NULL ) ftp_cwd(&ftp, skod.dest); else if ( skod.dest == NULL && flag == 4 ) die("You need to pass --dest (destination folder) with --upload."); switch(flag) { case 1: ftp_list(&ftp, skod.path, 1); break; case 2: ftp_remove(&ftp, skod.path); break; case 3: ftp_download_single(&ftp, skod.path); break; case 4: ftp_upload_single(&ftp, skod.path); break; case 5: ftp_cat(&ftp, skod.path); break; case 6: printf("%d\n", ftp_size(&ftp, skod.path)); break; case 7: printf("%s\n", ftp_pwd(&ftp)); break; case 8: ftp_delete(&ftp, skod.path); break; case 10: ftp_mdtm(&ftp, skod.path); break; case 99: skod_scan(&skod, &ftp); break; } ftp_close(&ftp); return 0; }
void cmd_pwd(int argc, char **argv) { OPT_HELP_NEW(_("Print the current working directory."), "pwd [options]", NULL); maxargs(optind - 1); need_connected(); need_loggedin(); ftp_pwd(); }
void cmd_pwd(int argc, char **argv) { OPT_HELP("Print the current working directory. Usage:\n" " pwd [options]\n" "Options:\n" " -h, --help show this help\n"); maxargs(optind - 1); need_connected(); need_loggedin(); ftp_pwd(); }
/** * Generates response message for client * @param cmd Current command * @param state Current connection state */ void response(Command *cmd, State *state) { switch(lookup_cmd(cmd->command)){ case USER: ftp_user(cmd,state); break; case PASS: ftp_pass(cmd,state); break; case PASV: ftp_pasv(cmd,state); break; case LIST: ftp_list(cmd,state); break; case CWD: ftp_cwd(cmd,state); break; case PWD: ftp_pwd(cmd,state); break; case MKD: ftp_mkd(cmd,state); break; case RMD: ftp_rmd(cmd,state); break; case RETR: ftp_retr(cmd,state); break; case STOR: ftp_stor(cmd,state); break; case DELE: ftp_dele(cmd,state); break; case SIZE: ftp_size(cmd,state); break; case ABOR: ftp_abor(state); break; case QUIT: ftp_quit(state); break; case TYPE: ftp_type(cmd,state); break; case CDUP: ftp_cdup(state); break; case HELP: ftp_help(cmd, state); break; case NLST: ftp_nlst(cmd, state); break; case RNFR: ftp_rnfr(cmd, state); break; case RNTO: ftp_rnto(cmd, state); break; case APPE: ftp_appe(cmd, state); break; case NOOP: if(state->logged_in){ state->message = "200 Zzz...\n"; }else{ state->message = "530 Please login with USER and PASS\n"; } write_state(state); break; default: state->message = "500 Unknown command\n"; write_state(state); break; } }
/* * Change working directory to the directory that contains the specified * file. */ static int ftp_cwd(conn_t *conn, const char *path, int subdir) { const char *beg, *end; char *pwd, *dst; int e, i, len; if (*path != '/') { ftp_seterr(501); return (-1); } ++path; /* Simple case: still in the home directory and no directory change. */ if (conn->ftp_home == NULL && strchr(path, '/') == NULL && (!subdir || *path == '\0')) return 0; if ((e = ftp_cmd(conn, "PWD\r\n")) != FTP_WORKING_DIRECTORY || (e = ftp_pwd(conn, &pwd)) != FTP_OK) { ftp_seterr(e); return (-1); } if (conn->ftp_home == NULL && (conn->ftp_home = strdup(pwd)) == NULL) { fetch_syserr(); free(pwd); return (-1); } if (*path == '/') { while (path[1] == '/') ++path; dst = strdup(path); } else if (strcmp(conn->ftp_home, "/") == 0) { dst = strdup(path - 1); } else { if (asprintf(&dst, "%s/%s", conn->ftp_home, path) == -1) dst = NULL; } if (dst == NULL) { fetch_syserr(); free(pwd); return (-1); } if (subdir) end = dst + strlen(dst); else end = strrchr(dst, '/'); for (;;) { len = strlen(pwd); /* Look for a common prefix between PWD and dir to fetch. */ for (i = 0; i <= len && i <= end - dst; ++i) if (pwd[i] != dst[i]) break; /* Keep going up a dir until we have a matching prefix. */ if (strcmp(pwd, "/") == 0) break; if (pwd[i] == '\0' && (dst[i - 1] == '/' || dst[i] == '/')) break; free(pwd); if ((e = ftp_cmd(conn, "CDUP\r\n")) != FTP_FILE_ACTION_OK || (e = ftp_cmd(conn, "PWD\r\n")) != FTP_WORKING_DIRECTORY || (e = ftp_pwd(conn, &pwd)) != FTP_OK) { ftp_seterr(e); free(dst); return (-1); } } free(pwd); #ifdef FTP_COMBINE_CWDS /* Skip leading slashes, even "////". */ for (beg = dst + i; beg < end && *beg == '/'; ++beg, ++i) /* nothing */ ; /* If there is no trailing dir, we're already there. */ if (beg >= end) { free(dst); return (0); } /* Change to the directory all in one chunk (e.g., foo/bar/baz). */ e = ftp_cmd(conn, "CWD %.*s\r\n", (int)(end - beg), beg); if (e == FTP_FILE_ACTION_OK) { free(dst); return (0); } #endif /* FTP_COMBINE_CWDS */ /* That didn't work so go back to legacy behavior (multiple CWDs). */ for (beg = dst + i; beg < end; beg = dst + i + 1) { while (*beg == '/') ++beg, ++i; for (++i; dst + i < end && dst[i] != '/'; ++i) /* nothing */ ; e = ftp_cmd(conn, "CWD %.*s\r\n", (int)(dst + i - beg), beg); if (e != FTP_FILE_ACTION_OK) { free(dst); ftp_seterr(e); return (-1); } } free(dst); return (0); }
int main(int argc,char **argv) { printf("ftp>"); char command[MAXSIZE]; char*cmd; scanf("%s",command); cmd=command; while(*(cmd)==' ') cmd++; if(strncmp(cmd,"login",5)==0) { login(); if(login_yes==1) { while(1) { comm: sleep(1); printf("ftp>"); zeromery(command,1024); scanf("%s",command); cmd=command; while(*(cmd)==' ') cmd++; if(strncmp(cmd,"pasv",4)==0) { ftp_list(control_sockfd); } if(strncmp(cmd,"port",4)==0) { ftp_list(control_sockfd); } if(strncmp(cmd,"list",4)==0) { ftp_pwd(control_sockfd); ftp_list(control_sockfd); } if(strncmp(cmd,"pwd",3)==0) { ftp_pwd(control_sockfd); } if(strncmp(cmd,"mkdir",5)==0) { char path[60]; zeromery(path,60); printf("创建的路径名: "); scanf("%s",path); printf("s\n",path); ftp_creat_mkd(path,control_sockfd); } if(strncmp(cmd,"back",4)==0) { ftp_back(control_sockfd); ftp_pwd(control_sockfd); } if(strncmp(cmd,"cd",2)==0) { int i; char path[60]; zeromery(path,60); printf("要到的路径:"); scanf("%s",path); printf("%s\n",path); ftp_changdir(path,control_sockfd); } if(strncmp(cmd,"get",3)==0) { ftp_pwd(control_sockfd); ftp_download(control_sockfd); } if(strncmp(cmd,"up",3)==0) { ftp_pwd(control_sockfd); ftp_up(control_sockfd); } if(strncmp(cmd,"quit",4)==0) { printf("bye^_^\n"); close(control_sockfd); break; } printf("支持 list,pwd,mkdir,back,cd,up,get\n"); } } else if(login_yes==0) { int i;//不成功登录下最多还有两次机会,如果不能在两次登录,则,关闭链接。 printf("Can not login vsftpd"); for(i=2;i>0;i--) { printf("你还有 %d 登录机会\n",i); login(); if(login_yes==1) { goto comm; } } if(i==0) { printf("你不能在登录!\n"); close(control_sockfd); } } else if (strncmp(cmd,"quit",4)==0) { ftp_quit(control_sockfd); close(control_sockfd); } } return 0; }