int32_t Shell_dir(int32_t argc, char *argv[] ) { /* Body */ bool print_usage, shorthelp = FALSE; int32_t return_code = SHELL_EXIT_SUCCESS; int32_t len; MQX_FILE_PTR fs_ptr; char *path_ptr, *mode_ptr; void *dir_ptr; char *buffer = NULL; SHELL_CONTEXT_PTR shell_ptr = Shell_get_context( argv ); int32_t error = 0; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if (argc > 3) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else { path_ptr ="*.*"; mode_ptr = "m"; if (argc >= 2) { path_ptr = argv[1]; if (argc== 3) { mode_ptr = argv[2]; } } fs_ptr = Shell_get_current_filesystem(argv); /* check if filesystem is mounted */ if (fs_ptr == NULL) { printf("Error, file system not mounted\n"); return_code = SHELL_EXIT_ERROR; } else { buffer = _mem_alloc(BUFFER_SIZE); error = ioctl(fs_ptr, IO_IOCTL_CHANGE_CURRENT_DIR, shell_ptr->CURRENT_DIR); if (buffer && !error) { dir_ptr = _io_mfs_dir_open(fs_ptr, path_ptr, mode_ptr ); if (dir_ptr == NULL) { printf("File not found.\n"); return_code = SHELL_EXIT_ERROR; } else { while ((_io_is_fs_valid(fs_ptr)) && (len = _io_mfs_dir_read(dir_ptr, buffer, BUFFER_SIZE)) > 0) { printf(buffer); } _io_mfs_dir_close(dir_ptr); } _mem_free(buffer); } else { if(buffer == NULL){ printf("Error, unable to allocate space.\n" ); } else { printf("Error, directory does not exist.\n" ); } return_code = SHELL_EXIT_ERROR; } } } } if (print_usage) { if (shorthelp) { printf("%s [<filespec>] [<attr>]]\n", argv[0]); } else { printf("Usage: %s [<filespec> [<attr>]]\n", argv[0]); printf(" <filespec> = files to list\n"); printf(" <attr> = attributes of files: adhrsv*\n"); } } return return_code; } /* Endbody */
int32_t ftpsrv_list(FTPSRV_SESSION_STRUCT* session) { int32_t length; char* path; void* dir_ptr; uint32_t sock; char* dir_param; char* full_path; char* temp; uint32_t path_length; uint32_t wrong_path; if (session->cmd_arg == NULL) { path = ""; } else { rtcs_url_decode(session->cmd_arg); path = rtcs_path_strip_delimiters(session->cmd_arg); } /* Translate relative path to absolute. */ full_path = ftpsrv_get_full_path(session, path, &wrong_path); if (full_path == NULL) { if (wrong_path) { session->message = (char*) ftpsrvmsg_no_file; } else { session->message = (char*) ftpsrvmsg_no_memory; } return(FTPSRV_ERROR); } path_length = strlen(full_path); /* Allocate space for path + appendix, copy full path and add appendix to it. */ /* This is required because MFS cannot list directories, only files. */ temp = RTCS_mem_alloc_zero(path_length+sizeof(FTPSRV_PATH_APPENDIX)); _mem_copy(full_path, temp, path_length); _mem_copy(FTPSRV_PATH_APPENDIX, temp+path_length, sizeof(FTPSRV_PATH_APPENDIX)-1); _mem_free(full_path); full_path = temp; /* Open directory. Unix format for LIST command, simple file list for NLIST. */ if (!strcmp(session->command, "LIST")) { /* Unix */ dir_param = "u*"; } else { /* File list */ dir_param = "f*"; } /* Open directory, get list, cleanup and return. */ dir_ptr = _io_mfs_dir_open(session->fs_ptr, full_path, dir_param); if (dir_ptr == NULL) { session->message = (char*) ftpsrvmsg_no_file; return(FTPSRV_ERROR); } /* Send initialization message */ ftpsrv_send_msg(session, ftpsrvmsg_opening_datacon); /* Open data connection */ sock = ftpsrv_open_data_connection(session); if (sock == RTCS_SOCKET_ERROR) { session->message = (char*) ftpsrvmsg_locerr; _mem_free(full_path); _io_mfs_dir_close(dir_ptr); return(FTPSRV_ERROR); } /* Send data (directory listing). */ while ((length = _io_mfs_dir_read(dir_ptr, session->buffer, FTPSRV_BUF_SIZE)) > 0) { /* If we are in root do not list "one level up" nor "current dir" link */ if ((strstr(session->buffer, " .. ") || strstr(session->buffer, " . ")) && !strcmp(session->cur_dir, "\\")) { _mem_zero(session->buffer, length); } else { if (send(sock, session->buffer, length, 0) != length) { ftpsrv_send_msg(session, ftpsrvmsg_writefail); break; } } } /* Cleanup */ closesocket(sock); _io_mfs_dir_close(dir_ptr); _mem_free(full_path); session->message = (char*) ftpsrvmsg_trans_complete; return FTPSRV_OK; }