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); }
static void find_on_path(const char *prog, char *dir_out) { const char *path = getenv("PATH"), *p, *q; if (path != NULL || *path != '\0') { p = path; while (*p != '\0') { while (*p == PATH_SEP) { ++p; } if (*p == '\0') break; q = p; while (*q != '\0' && *q != PATH_SEP) ++q; if (is_in_dir(prog, p, q)) { if (q - p > PATH_MAX) { q = p + PATH_MAX; } memcpy(dir_out, p, q - p); dir_out[q - p] = '\0'; return; } p = q; } } *dir_out = '\0'; }