void change_dir(char *currdir, const char *dir) { char tempdir[1024]; if(dir == NULL) { printf("ERROR: Must supply a directory argument\n"); return; } if(strcmp(dir, "..") == 0) { up_dir(currdir); return; } (void) snprintf(tempdir, sizeof(tempdir), "%s/%s", currdir, dir); if(check_dir(tempdir)) { strcpy(currdir, tempdir); } else { printf("ERROR: Invalid directory '%s'\n", tempdir); } }
static void cmd_cd(conn_item* item) { int len; cmd_header* ch; cmd_header cmd; cmd_header* respond = &cmd; char to[256]; assert(item && item->current_path && item->cmd_rd_buf); ch = (cmd_header*)item->cmd_rd_buf; // assert(cmd->cmd_type == CMD_CD_DIR); respond->magic = htonl(MAGIC_NUM); snprintf(to, item->cmd_rd_curr - sizeof(cmd_header) + 1, "%s", item->cmd_rd_buf + sizeof(cmd_header)); if(strcmp(to, "..") == 0) { if(strcmp(item->current_path, DEFAULT_FILE_PATH) == 0) { respond->cmd_type = htonl(CMD_REQUEST_ERROR); len = sprintf(item->data_wt_buf + sizeof(cmd_header), "already reach the root dir\n"); item->data_wt_curr = len + sizeof(cmd_header); respond->package_len = htonl(item->data_wt_curr); *(cmd_header*)item->data_wt_buf = cmd; return; } up_dir(item->current_path); cmd_ls(item); return; } if(is_in_dir(item->current_path, to) == T_FTP_FAIL) { respond->cmd_type = htonl(CMD_REQUEST_ERROR); len = sprintf(item->data_wt_buf + sizeof(cmd_header), "%s is not a dir\n", to); item->data_wt_curr = len + sizeof(cmd_header); respond->package_len = htonl(item->data_wt_curr); *((cmd_header*)item->data_wt_buf) = cmd; return; } sprintf(item->current_path + strlen(item->current_path), "/%s", to); cmd_ls(item); }