14#if defined(RT_USING_FINSH) && defined(DFS_USING_POSIX)
29static int msh_readline(
int fd,
char *line_buf,
int size)
36 if (
read(fd, &ch, 1) != 1)
42 while (ch ==
'\n' || ch ==
'\r');
45 line_buf[index ++] = ch;
49 if (
read(fd, &ch, 1) == 1)
51 if (ch ==
'\n' || ch ==
'\r')
53 line_buf[index] =
'\0';
57 line_buf[index++] = ch;
61 line_buf[index] =
'\0';
74 int length, cmd_length = 0;
76 if (size == 0)
return -RT_ERROR;
79 while ((cmd_line[cmd_length] !=
' ' && cmd_line[cmd_length] !=
'\t') && cmd_length < size)
83 length = cmd_length + 32;
87 if (pg_name ==
RT_NULL)
return -RT_ENOMEM;
90 rt_memcpy(pg_name, cmd_line, cmd_length);
91 pg_name[cmd_length] =
'\0';
93 if (strstr(pg_name,
".sh") !=
RT_NULL || strstr(pg_name,
".SH") !=
RT_NULL)
96 fd =
open(pg_name, O_RDONLY, 0);
101 rt_snprintf(pg_name, length - 1,
"/bin/%.*s", cmd_length, cmd_line);
102 fd =
open(pg_name, O_RDONLY, 0);
113 line_buf = (
char *)
rt_malloc(RT_CONSOLEBUF_SIZE);
123 length = msh_readline(fd, line_buf, RT_CONSOLEBUF_SIZE);
129 for (index = 0; index < length; index ++)
131 ch = line_buf[index];
132 if (ch ==
' ' || ch ==
'\t')
continue;
155#ifdef DFS_USING_WORKDIR
159static int cmd_ls(
int argc,
char **argv)
161 extern void ls(
const char *pathname);
165#ifdef DFS_USING_WORKDIR
184#ifdef RT_USING_DFS_V2
185static int cmd_ln(
int argc,
char **argv)
190 rt_kprintf(
"Make symbolic link between files.\n");
194 for(
int i = 0; i + 3 <= argc; i ++)
204static int cmd_link(
int argc,
char **argv)
213 for(
int i = 0; i + 3 <= argc; i ++)
224static int cmd_cp(
int argc,
char **argv)
226 void copy(
const char *src,
const char *dst);
235 copy(argv[1], argv[2]);
242static int cmd_mv(
int argc,
char **argv)
247 rt_kprintf(
"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n");
256 fd =
open(argv[2], O_DIRECTORY, 0);
271 src = argv[1] + rt_strlen(argv[1]);
272 while (src != argv[1])
274 if (*src ==
'/')
break;
278 rt_snprintf(dest,
DFS_PATH_MAX - 1,
"%s/%s", argv[2], src);
282 fd =
open(argv[2], O_RDONLY, 0);
301static int cmd_cat(
int argc,
char **argv)
304 extern void cat(
const char *filename);
313 for (index = 1; index < argc; index ++)
322static void directory_delete_for_msh(
const char *pathname,
char f,
char v)
325 struct dirent *dirent = NULL;
351 if (rt_strcmp(
".", dirent->d_name) != 0 &&
352 rt_strcmp(
"..", dirent->d_name) != 0)
354 if (strlen(pathname) + 1 + strlen(dirent->d_name) >
DFS_PATH_MAX)
356 rt_kprintf(
"cannot remove '%s/%s', path too long.\n", pathname, dirent->d_name);
359 rt_sprintf(full_path,
"%s/%s", pathname, dirent->d_name);
360 if (dirent->d_type != DT_DIR)
362 if (
unlink(full_path) != 0)
365 rt_kprintf(
"cannot remove '%s'\n", full_path);
374 directory_delete_for_msh(full_path, f, v);
380 if (
rmdir(pathname) != 0)
387 rt_kprintf(
"removed directory '%s'\n", pathname);
391static int cmd_rm(
int argc,
char **argv)
394 char f = 0, r = 0, v = 0;
403 if (argv[1][0] ==
'-')
405 for (n = 0; argv[1][n]; n++)
421 rt_kprintf(
"Error: Bad option: %c\n", argv[1][n]);
429 for (index = 1; index < argc; index ++)
432#ifdef RT_USING_DFS_V2
435 if (
stat(argv[index], &s) == 0)
438 if (S_ISDIR(s.st_mode))
441 rt_kprintf(
"cannot remove '%s': Is a directory\n", argv[index]);
443 directory_delete_for_msh(argv[index], f, v);
447 if (
unlink(argv[index]) != 0)
450 rt_kprintf(
"cannot remove '%s'\n", argv[index]);
460 rt_kprintf(
"cannot remove '%s': No such file or directory\n", argv[index]);
467#ifdef DFS_USING_WORKDIR
468static int cmd_cd(
int argc,
char **argv)
476 if (chdir(argv[1]) != 0)
478 rt_kprintf(
"No such directory: %s\n", argv[1]);
486static int cmd_pwd(
int argc,
char **argv)
494static int cmd_mkdir(
int argc,
char **argv)
498 rt_kprintf(
"Usage: mkdir [OPTION] DIRECTORY\n");
499 rt_kprintf(
"Create the DIRECTORY, if they do not already exist.\n");
510static int cmd_mkfs(
int argc,
char **argv)
521 if (strcmp(argv[1],
"-t") == 0)
533 if (result != RT_EOK)
535 rt_kprintf(
"mkfs failed, result=%d\n", result);
550static int cmd_mount(
int argc,
char **argv)
554#ifdef RT_USING_DFS_V2
556 rt_kprintf(
"filesystem device mountpoint refcount\n");
557 rt_kprintf(
"---------- ------ ---------- --------\n");
561 extern struct dfs_filesystem filesystem_table[];
562 struct dfs_filesystem *iter;
567 for (iter = &filesystem_table[0];
568 iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
570 if ((iter != NULL) && (iter->path != NULL))
573 iter->ops->name, iter->dev_id->parent.name, iter->path);
581 char *device = argv[1];
582 char *path = argv[2];
583 char *fstype = argv[3];
587 rt_kprintf(
"mount device %s(%s) onto %s ... ", device, fstype, path);
588 if (strcmp(fstype,
"nfs") == 0)
594 if (
dfs_mount(device, path, fstype, 0, data) == 0)
607 char *path = argv[1];
608 char *fstype = argv[2];
611 rt_kprintf(
"mount (%s) onto %s ... ", fstype, path);
612 if (
dfs_mount(NULL, path, fstype, 0, 0) == 0)
625 rt_kprintf(
"Usage: mount <device> <mountpoint> <fstype>.\n");
632static int cmd_umount(
int argc,
char **argv)
634#ifndef RT_USING_DFS_V2
635 char *path = argv[1];
656 char *path = argv[1];
660 rt_kprintf(
"Usage: unmount [-f] <mountpoint>.\n");
666 flags = strcmp(argv[1],
"-f") == 0 ?
MNT_FORCE : 0;
685static int cmd_df(
int argc,
char **argv)
687#ifndef RT_USING_DFS_V2
688 extern int df(
const char *path);
696 if ((strcmp(argv[1],
"--help") == 0) || (strcmp(argv[1],
"-h") == 0))
711static int cmd_echo(
int argc,
char **argv)
721 fd =
open(argv[2], O_RDWR | O_APPEND | O_CREAT, 0);
724 write(fd, argv[1], strlen(argv[1]));
729 rt_kprintf(
"open file:%s failed!\n", argv[2]);
734 rt_kprintf(
"Usage: echo \"string\" [filename]\n");
741static int cmd_tail(
int argc,
char **argv)
754 rt_kprintf(
"Usage: tail [-n numbers] <filename>\n");
762 else if (rt_strcmp(argv[1],
"-n") == 0)
764 if (argv[2][0] !=
'+')
766 required_lines = atoi(argv[2]);
770 start_line = atoi(&argv[2][1]);
776 rt_kprintf(
"Usage: tail [-n numbers] <filename>\n");
780 fd =
open(file_name, O_RDONLY);
787 while ((
read(fd, &c,
sizeof(
char))) > 0)
799 rt_kprintf(
"\nTotal Number of lines:%d\n", total_lines);
803 if (total_lines >= start_line)
805 required_lines = total_lines - start_line + 1;
809 rt_kprintf(
"\nError:Required lines are more than total number of lines\n");
815 if (required_lines > total_lines)
817 rt_kprintf(
"\nError:Required lines are more than total number of lines\n");
821 rt_kprintf(
"Required Number of lines:%d\n", required_lines);
823 target_line = total_lines - required_lines;
824 lseek(fd, 0, SEEK_SET);
826 while ((
read(fd, &c,
sizeof(
char))) > 0)
828 if (current_line >= target_line)
844#ifdef RT_USING_DFS_V2
846static void directory_setattr(
const char *pathname,
struct dfs_attr *attr,
char f,
char v)
849 struct dirent *dirent = NULL;
875 if (rt_strcmp(
".", dirent->d_name) != 0 &&
876 rt_strcmp(
"..", dirent->d_name) != 0)
878 if (strlen(pathname) + 1 + strlen(dirent->d_name) >
DFS_PATH_MAX)
880 rt_kprintf(
"'%s/%s' setattr failed, path too long.\n", pathname, dirent->d_name);
883 rt_sprintf(full_path,
"%s/%s", pathname, dirent->d_name);
884 if (dirent->d_type == DT_REG)
890 rt_kprintf(
"'%s' setattr failed, no such file or directory\n", full_path);
898 else if (dirent->d_type == DT_DIR)
900 directory_setattr(full_path, attr, f, v);
910 rt_kprintf(
"'%s' setattr failed, no such file or directory\n", pathname);
919static int cmd_chmod(
int argc,
char **argv)
923 rt_kprintf(
"Usage: chmod [OPTION]... MODE[,MODE]... FILE...\n");
924 rt_kprintf(
" chmod [-f|v|r] [u|g|o|a][+/-/=][r|w|x] file...\n");
925 rt_kprintf(
" -f suppress most error messages\n");
926 rt_kprintf(
" -v output a diagnostic for every file processed\n");
927 rt_kprintf(
" -r change files and directories recursively\n");
928 rt_kprintf(
"Change the mode of each FILE to MODE.\n");
933 char f = 0, r = 0, v = 0;
935 if (argv[argv_c][0] ==
'-')
937 for (
int i = 1; argv[argv_c][i]; i++)
939 switch (argv[argv_c][i])
951 rt_kprintf(
"Error: Bad option: %c\n", argv[argv_c][i]);
958 if (argc - argv_c > 1)
960 int U = 1, G = 2, O = 4, ALL = 7;
961 int off[5] = {0, 6, 3, 0, 0};
962 int ADD = 1, SUB = 2, SET = 4;
963 int R = 4, W = 2, X = 1;
964 int user[3] = {0}, change[3] = {0}, mode[3] = {0};
966 char *cmd = argv[argv_c];
967 int index = 0, num = 0;
969 while (cmd[index] !=
'\0')
996 while (cmd[index] !=
'\0')
1020 while (cmd[index] !=
'\0')
1043 for (
int i = 0; i <= num; i++)
1045 if (change[i] == ADD)
1049 attr.
st_mode |= mode[i] << off[user[i] & U];
1054 attr.
st_mode |= mode[i] << off[user[i] & G];
1059 attr.
st_mode |= mode[i] << off[user[i] & O];
1062 else if (change[i] == SUB)
1066 attr.
st_mode &= ~(mode[i] << off[user[i] & U]);
1071 attr.
st_mode &= ~(mode[i] << off[user[i] & G]);
1076 attr.
st_mode &= ~(mode[i] << off[user[i] & O]);
1079 else if (change[i] == SET)
1083 attr.
st_mode &= ~(7 << off[user[i] & U]);
1084 attr.
st_mode |= mode[i] << off[user[i] & U];
1089 attr.
st_mode &= ~(7 << off[user[i] & G]);
1090 attr.
st_mode |= mode[i] << off[user[i] & G];
1095 attr.
st_mode &= ~(7 << off[user[i] & O]);
1096 attr.
st_mode |= mode[i] << off[user[i] & O];
1103 for (
int i = argv_c; i < argc; i++)
1108 if (
stat(argv[i], &s) == 0)
1110 if (S_ISDIR(s.st_mode))
1112 directory_setattr(argv[i], &attr, f, v);
1116 rt_kprintf(
"'%s' is not a directory\n", argv[i]);
1126 rt_kprintf(
"'%s' setattr failed, no such file or directory\n", argv[i]);
int dfs_file_lstat(const char *path, struct stat *buf)
void copy(const char *src, const char *dst)
void ls(const char *pathname)
void cat(const char *filename)
int dfs_file_setattr(const char *path, struct dfs_attr *attr)
int dfs_file_symlink(const char *target, const char *linkpath)
int dfs_file_link(const char *oldname, const char *newname)
int dfs_mkfs(const char *fs_name, const char *device_name)
int dfs_mount(const char *device_name, const char *path, const char *filesystemtype, unsigned long rwflag, const void *data)
int dfs_unmount(const char *specialfile)
int dfs_umount(const char *specialfile, int flags)
int dfs_mnt_list(struct dfs_mnt *mnt)
ssize_t write(int fd, const void *buf, size_t len)
ssize_t read(int fd, void *buf, size_t len)
int rename(const char *old_file, const char *new_file)
struct dirent * readdir(DIR *d)
DIR * opendir(const char *name)
int mkdir(const char *path, mode_t mode)
int rmdir(const char *pathname)
off_t lseek(int fd, off_t offset, int whence)
int open(const char *file, int flags,...)
int stat(const char *file, struct stat *buf)
int unlink(const char *pathname)
rt_weak void rt_free(void *ptr)
This function will release the previously allocated memory block by rt_malloc. The released memory bl...
rt_weak void * rt_malloc(rt_size_t size)
Allocate a block of memory with a minimum of 'size' bytes.
#define MSH_CMD_EXPORT_ALIAS(...)
int msh_exec(char *cmd, rt_size_t length)
int msh_exec_script(const char *cmd_line, int size)
struct finsh_shell * shell