int_32 Shell_cd(int_32 argc, char_ptr argv[] ) { /* Body */ boolean print_usage, shorthelp = FALSE, dev_in_path = FALSE; int_32 error = 0, return_code = SHELL_EXIT_SUCCESS; MQX_FILE_PTR fs_ptr; char_ptr abs_path = NULL; SHELL_CONTEXT_PTR shell_ptr = Shell_get_context( argv ); int_16 devlen = 0; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if (argc != 2) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else { if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) { printf("Error, unable to allocate memory for paths\n" ); return_code = SHELL_EXIT_ERROR; } else { devlen = _io_get_dev_for_path(abs_path, &dev_in_path, PATHNAME_SIZE, (char *)argv[1], Shell_get_current_filesystem_name(shell_ptr)); fs_ptr = _io_get_fs_by_name(abs_path); if (fs_ptr == NULL) { printf("Device \"%s\" not available\n", abs_path); return_code = SHELL_EXIT_ERROR; } else { error = _io_rel2abs(abs_path, shell_ptr->CURRENT_DIR, (char *)argv[1], PATHNAME_SIZE, shell_ptr->CURRENT_DEVICE_NAME); if(!error) { // check if path exist error = ioctl(fs_ptr, IO_IOCTL_CHECK_DIR_EXIST,(pointer)abs_path ); } if (error) { printf("Error changing directory %s\n", argv[1]); } else { if(dev_in_path == TRUE) { // there is device name in input path //separate device name abs_path[devlen] = '\0'; Shell_set_current_filesystem_by_name(argv,abs_path); // add "\" back to the string abs_path[devlen] = '\\'; } // change shell current dir strcpy(shell_ptr->CURRENT_DIR,abs_path+devlen); } } MFS_free_path(abs_path); } } } if (print_usage) { if (shorthelp) { printf("%s <directory> \n", argv[0]); } else { printf("Usage: %s <directory>\n", argv[0]); printf(" <directory> = name of directory to change to\n"); } } return return_code; }
int_32 Shell_type(int_32 argc, char_ptr argv[] ) { /* Body */ boolean print_usage, shorthelp = FALSE; int_32 return_code = SHELL_EXIT_SUCCESS; MQX_FILE_PTR fd; char_ptr abs_path; _mqx_int c; SHELL_CONTEXT_PTR shell_ptr = Shell_get_context( argv ); int_32 error = 0; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if (argc == 2) { /* check if filesystem is mounted */ if (NULL == Shell_get_current_filesystem(argv)) { printf("Error, file system not mounted\n" ); return_code = SHELL_EXIT_ERROR; } else if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) { printf("Error, unable to allocate memory for paths\n" ); return_code = SHELL_EXIT_ERROR; } else { error = _io_rel2abs(abs_path,shell_ptr->CURRENT_DIR,(char *) argv[1],PATHNAME_SIZE,shell_ptr->CURRENT_DEVICE_NAME); if(!error) { fd = fopen(abs_path, "r"); } MFS_free_path(abs_path); if (fd && !error) { do { c = fgetc(fd); if (c!= IO_EOF) { fputc((char)c, stdout); } } while (c!=IO_EOF); fclose(fd); printf("\n"); } else { printf("Error, unable to open file %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } } } else { printf("Error, %s invoked with incorrect number of arguments\n", argv[0]); return_code = SHELL_EXIT_ERROR; print_usage = TRUE; } } if (print_usage) { if (shorthelp) { printf("%s <filename>\n", argv[0]); } else { printf("Usage: %s <filename>\n", argv[0]); printf(" <filename> = filename to display\n"); } } return return_code; } /* Endbody */
int32_t Shell_del(int32_t argc, char *argv[] ) { /* Body */ bool print_usage, shorthelp = FALSE, temp; int32_t error = 0, return_code = SHELL_EXIT_SUCCESS; MQX_FILE_PTR fs_ptr; SHELL_CONTEXT_PTR shell_ptr = Shell_get_context( argv ); char *abs_path; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if (argc != 2) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } /* check if filesystem is mounted */ else if (NULL == Shell_get_current_filesystem(argv)) { printf("Error, file system not mounted\n" ); return_code = SHELL_EXIT_ERROR; } else { if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) { printf("Error, unable to allocate memory for paths\n" ); return_code = SHELL_EXIT_ERROR; } else { _io_get_dev_for_path(abs_path,&temp,PATHNAME_SIZE,(char *)argv[1],Shell_get_current_filesystem_name(shell_ptr)); fs_ptr = _io_get_fs_by_name(abs_path); if (fs_ptr == NULL) { printf("Error, file system not mounted\n" ); return_code = SHELL_EXIT_ERROR; } else { error = _io_rel2abs(abs_path,shell_ptr->CURRENT_DIR,(char *) argv[1],PATHNAME_SIZE,Shell_get_current_filesystem_name(shell_ptr)); if (!error) { error = ioctl(fs_ptr, IO_IOCTL_DELETE_FILE, (void *) abs_path); } if (error) { printf("Error deleting file %s\n", argv[1]); } } MFS_free_path(abs_path); } } } if (print_usage) { if (shorthelp) { printf("%s <file> \n", argv[0]); } else { printf("Usage: %s <file>\n", argv[0]); printf(" <file> = name of file to delete\n"); } } return return_code; }
int32_t Shell_create(int32_t argc, char *argv[] ) { /* Body */ bool print_usage, shorthelp = FALSE; int32_t return_code = SHELL_EXIT_SUCCESS; char *mode; int fd = -1; char *abs_path; 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 < 2) || (argc > 3)) { fprintf(shell_ptr->STDOUT, "Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage = TRUE; } /* check if filesystem is mounted */ else if (0 > Shell_get_current_filesystem(argv)) { fprintf(shell_ptr->STDOUT, "Error, file system not mounted\n" ); return_code = SHELL_EXIT_ERROR; } else { /*if (argc != 3) { mode = "a"; } else { mode = argv[2]; } */ if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) { fprintf(shell_ptr->STDOUT, "Error, unable to allocate memory for paths\n" ); return_code = SHELL_EXIT_ERROR; } else { error = _io_rel2abs(abs_path,shell_ptr->CURRENT_DIR,(char *) argv[1],PATHNAME_SIZE,shell_ptr->CURRENT_DEVICE_NAME); if(!error) { fd = open(abs_path, O_WRONLY | O_CREAT); } MFS_free_path(abs_path); if (0 <= fd && !error) { close(fd); } else { fprintf(shell_ptr->STDOUT, "Error, unable to open file %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } } } } if (print_usage) { if (shorthelp) { fprintf(shell_ptr->STDOUT, "%s <filename> [<mode>] \n", argv[0]); } else { fprintf(shell_ptr->STDOUT, "Usage: %s <filename> [<mode>]\n", argv[0]); fprintf(shell_ptr->STDOUT, " <filename> = filename to create\n"); fprintf(shell_ptr->STDOUT, " <bytes> = open mode\n"); } } return return_code; } /* Endbody */
int32_t Shell_compare(int32_t argc, char *argv[] ) { /* Body */ bool print_usage, shorthelp = FALSE; int32_t size1, size2, return_code = SHELL_EXIT_SUCCESS; MQX_FILE_PTR in_fd_1=NULL, in_fd_2=NULL; char *file1=NULL, *file2=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; } /* check if filesystem is mounted */ else if (NULL == Shell_get_current_filesystem(argv)) { printf("Error, file system not mounted\n"); return_code = SHELL_EXIT_ERROR; } else if (MFS_alloc_path(&file1) != MFS_NO_ERROR) { printf("Error, unable to allocate memory for paths\n" ); return_code = SHELL_EXIT_ERROR; } else { error = _io_rel2abs(file1,shell_ptr->CURRENT_DIR,(char *) argv[1],PATHNAME_SIZE,shell_ptr->CURRENT_DEVICE_NAME); if(!error) { in_fd_1 = fopen(file1, "r"); } error = _io_rel2abs(file1,shell_ptr->CURRENT_DIR,(char *) argv[2],PATHNAME_SIZE,shell_ptr->CURRENT_DEVICE_NAME); if(!error) { in_fd_2 = fopen(file1, "r"); } MFS_free_path(file1); if (in_fd_1 == NULL) { printf("Error, unable to open file %s\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } else if (in_fd_2 == NULL) { printf("Error, unable to open file %s\n", argv[2] ); return_code = SHELL_EXIT_ERROR; } else { file1 = _mem_alloc_zero(COMPARE_BLOCK_SIZE); file2 = _mem_alloc_zero(COMPARE_BLOCK_SIZE); if ((file1==NULL) || (file2==NULL)) { printf("Error, unable to allocate buffer space" ); return_code = SHELL_EXIT_ERROR; } else { do { size1 = read(in_fd_1, file1, COMPARE_BLOCK_SIZE); size2 = read(in_fd_2, file2, COMPARE_BLOCK_SIZE); if (size1!=size2) { printf("Compare failed, files have different sizes\n" ); return_code = SHELL_EXIT_ERROR; } else if (size1 > 0) { if (memcmp(file1,file2,COMPARE_BLOCK_SIZE)!=0) { printf("Compare failed, files are different\n" ); return_code = SHELL_EXIT_ERROR; } } } while ((size1>0) && (return_code == SHELL_EXIT_SUCCESS)); if (return_code == SHELL_EXIT_SUCCESS) { printf("The files are identical\n" ); } } if (file1) _mem_free(file1); if (file2) _mem_free(file2); } if (in_fd_1) fclose(in_fd_1); if (in_fd_2) fclose(in_fd_2); } } if (print_usage) { if (shorthelp) { printf("%s <file1> <file2> \n", argv[0]); } else { printf("Usage: %s <file1> <file2>\n", argv[0]); printf(" <file1> = first file to compare\n"); printf(" <file2> = second file to compare\n"); } } return return_code; } /* Endbody */
int_32 Shell_write_binary(int_32 argc, char_ptr argv[] ,uint_32 num,uchar_ptr data) { /* Body */ boolean print_usage, shorthelp = FALSE; int_32 return_code = SHELL_EXIT_SUCCESS; uint_32 count=0, i; int_32 offset=0; int_32 seek_mode=0; char c; MQX_FILE_PTR fd = NULL; char_ptr abs_path; boolean cache_enabled=TRUE; SHELL_CONTEXT_PTR shell_ptr = Shell_get_context( argv ); char buf[SHELL_BLOCK_SIZE]; char * argv5_p; int_32 error = 0; _mqx_int bi; /* wk @130514 --> */ uint_32 nums;uchar left; /* wk @130514 --> */ print_usage = Shell_check_help_request(argc, argv, &shorthelp ); // wk @130405 --> 简化程序减小时间 if (!print_usage) { if ((argc < 3)) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } /* check if filesystem is mounted */ else if (NULL == Shell_get_current_filesystem(argv)) { printf("Error, file system not mounted\n"); return_code = SHELL_EXIT_ERROR; } else { count = 0; offset = 0; seek_mode = IO_SEEK_CUR; // 默认模式 if (argc >= 4) { // if (! Shell_parse_uint_32(argv[2], &count )) {// 检查输入长度 // printf("Error, invalid length\n"); // return_code = SHELL_EXIT_ERROR; // print_usage=TRUE; // } else { if (argc >= 4) { if (strcmp(argv[2], "begin") == 0) { seek_mode = IO_SEEK_SET; } else if (strcmp(argv[2], "end") == 0) { seek_mode = IO_SEEK_END; } else if (strcmp(argv[2], "current") == 0) { seek_mode = IO_SEEK_CUR; } else { printf("Error, invalid seek type\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } if (return_code == SHELL_EXIT_SUCCESS) { if (! Shell_parse_int_32(argv[3], &offset )) { printf("Error, invalid seek value\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } } } } } } if (return_code == SHELL_EXIT_SUCCESS) { if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) { printf("Error, unable to allocate memory for paths\n" ); return_code = SHELL_EXIT_ERROR; } else { error = _io_rel2abs(abs_path,shell_ptr->CURRENT_DIR,(char *) argv[1],PATHNAME_SIZE,shell_ptr->CURRENT_DEVICE_NAME); if(!error) { fd = fopen(abs_path, "a"); } if (fd && !error) { if (fseek(fd, offset, seek_mode ) != IO_ERROR) { // generate data to buf // argv5_p = (char*)argv[5];// by dx // for (i = 0, c = '0', bi = 0; i < count;i++) {// 写入txt,从字符0到z // // buf[bi++] = c; // // if (bi > SHELL_BLOCK_SIZE - 1) { // write(fd, buf, SHELL_BLOCK_SIZE); // bi = 0; // } // // if (c == 'z') // c='0'; // else // c++; // } // // if (bi) // count = ( strlen(argv5_p)<count )?( strlen(argv5_p) ):(count);// 修改长度,若超过最大长度则用最大长度 // write(fd, argv5_p, count);// 写入SD,相应路径 /* wk @130405 --> 写我二进制数据 */ // for(i = 0; i < num; i++) // { //// write(fd,data+i,1); // } /* wk @130514 --> */ nums=num>>7; left=(uchar)num&0x7f; for(uint_32 i=0;i<nums;i++) write(fd,&data[i<<7],SHELL_BLOCK_SIZE); write(fd,&data[nums<<7],left); /* wk @130514 --> end*/ // write(fd,data,num); /* wk @130405 --> 写二进制数据 <--END */ fclose(fd); } else { printf("Error, unable to seek file %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } } else { printf("Error, unable to open file %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } MFS_free_path(abs_path); }
int_32 Shell_create(int_32 argc, char_ptr argv[] ) { /* Body */ boolean print_usage, shorthelp = FALSE; int_32 return_code = SHELL_EXIT_SUCCESS; char_ptr mode; MQX_FILE_PTR fd = NULL; char_ptr abs_path; SHELL_CONTEXT_PTR shell_ptr = Shell_get_context( argv ); int_32 error = 0; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if ((argc < 2) || (argc > 3)) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage = TRUE; } /* check if filesystem is mounted */ else if (NULL == Shell_get_current_filesystem(argv)) { printf("Error, file system not mounted\n" ); return_code = SHELL_EXIT_ERROR; } else { if (argc != 3) { mode = "a"; } else { mode = argv[2]; } if (MFS_alloc_path(&abs_path) != MFS_NO_ERROR) { printf("Error, unable to allocate memory for paths\n" ); return_code = SHELL_EXIT_ERROR; } else { error = _io_rel2abs(abs_path,shell_ptr->CURRENT_DIR,(char *) argv[1],PATHNAME_SIZE,shell_ptr->CURRENT_DEVICE_NAME); if(!error) { fd = fopen(abs_path, mode); } MFS_free_path(abs_path); if (fd && !error) { fclose(fd); } else { printf("Error, unable to open file %s.\n", argv[1] ); return_code = SHELL_EXIT_ERROR; } } } } if (print_usage) { if (shorthelp) { printf("%s <filename> [<mode>] \n", argv[0]); } else { printf("Usage: %s <filename> [<mode>]\n", argv[0]); printf(" <filename> = filename to create\n"); printf(" <bytes> = open mode\n"); } } return return_code; } /* Endbody */