之前做过bash的简单实现,github上有很多的代码
    实现bash的简单功能,比如ls,使用的就是调用内核的接口,找出文件的fstat,然后将其列出打印,实现模拟ls的功能,但是不清楚系统的ls命令是否就是这样实现的

    1. #include <stdio.h>
    2. #include<stdlib.h>
    3. #include <dirent.h>
    4. #include <stdlib.h>
    5. #include <string.h>
    6. #include <sys/types.h>
    7. #include <sys/stat.h>
    8. #include<time.h>
    9. #include<string.h>
    10. #include<pwd.h>
    11. #include<grp.h>
    12. void dir_oper(char const*path);
    13. int main(int argc, char const *argv[])
    14. {
    15. //FILE *file_p;
    16. FILE *fp = fopen("inf.txt", "w+");
    17. if(!fp)
    18. {
    19. printf("open filepath failed\n");
    20. return 0;
    21. }
    22. char const* path = argv[1];
    23. struct stat s_buf;
    24. stat(path, &s_buf);
    25. if(S_ISDIR(s_buf.st_mode))
    26. {
    27. dir_oper(path);
    28. }
    29. //若输入的路径就是文件
    30. else if(S_ISREG(s_buf.st_mode))
    31. {
    32. printf("[%s] 是普通文件 %d \n",
    33. path, (int)s_buf.st_size);
    34. fprintf(fp, "[%s]是普通文件 %d \n",
    35. path, (int)s_buf.st_size);
    36. }
    37. return 0;
    38. }
    39. void dir_oper(char const*path)
    40. {
    41. FILE *fp = fopen("inf.txt", "w");
    42. if(!fp)
    43. {
    44. printf("filepath open failed\n");
    45. exit(0);
    46. }
    47. printf("%s/ 是目录. \n", path);
    48. fprintf(fp, "%s/ 是目录. \n", path);
    49. struct dirent *filename;
    50. struct stat s_buf;
    51. DIR *dp = opendir(path);
    52. //readdir()循环
    53. while(filename = readdir(dp))
    54. {
    55. char file_path[200];
    56. bzero(file_path,200);
    57. strcat(file_path,path);
    58. strcat(file_path,"/");
    59. strcat(file_path,filename->d_name);
    60. //排除目录下的隐藏文件. ..
    61. if(strcmp(filename->d_name, ".") == 0 ||
    62. strcmp(filename->d_name, "..") == 0)
    63. {
    64. continue;
    65. }
    66. stat(file_path,&s_buf);
    67. if(S_ISDIR(s_buf.st_mode))
    68. {
    69. //printf("进来了, 是目录\n");
    70. dir_oper(file_path);
    71. printf("\n");
    72. }
    73. //char *name = ctime(&s_buf.st_mtim);
    74. //char mtime[512] = {0};
    75. //strncpy(mtime, time, strlen(time) - 1);
    76. char *file_user = getpwuid(s_buf.st_uid)->pw_name;
    77. char power[11] = {0};
    78. // 文件所有者
    79. power[0] = '-';
    80. power[1] = (s_buf.st_mode & S_IRUSR) ? 'r' : '-';
    81. power[2] = (s_buf.st_mode & S_IWUSR) ? 'w' : '-';
    82. power[3] = (s_buf.st_mode & S_IXUSR) ? 'x' : '-';
    83. // 所属组
    84. power[4] = (s_buf.st_mode & S_IRGRP) ? 'r' : '-';
    85. power[5] = (s_buf.st_mode & S_IWGRP) ? 'w' : '-';
    86. power[6] = (s_buf.st_mode & S_IXGRP) ? 'x' : '-';
    87. // 其他
    88. power[7] = (s_buf.st_mode & S_IROTH) ? 'r' : '-';
    89. power[8] = (s_buf.st_mode & S_IWOTH) ? 'w' : '-';
    90. power[9] = (s_buf.st_mode & S_IXOTH) ? 'x' : '-';
    91. if(S_ISREG(s_buf.st_mode))
    92. {
    93. printf("[%s]是普通文件 %s %d %s \n",
    94. file_path, power, (int)s_buf.st_size, file_user);
    95. fprintf(fp, "[%s] 是普通文件%s %d %s \n",
    96. file_path, power, (int)s_buf.st_size, file_user);
    97. }
    98. }
    99. }

    问题1:这个代码中使用了FILE。DIR,dirent,stat等VFS提供的结构体,然后使用了一些简单的c函数,实现了ls,
    那么挂载文件系统的时候,是不是同样的也是类似使用FILE这些结构体,实现ls这些命令,来访问ext4?
    问题2:如果这个前提成立,那么ext4是否可以直接挂载到rtems上,只是内容读不出来?需要做的只是实现这些类bash的接口?
    问题3:所有的功能实现都是基于现有的API,rtems文件系统开发手册上给出了很多API,都是用上面的吗,那这样的话,了解一个文件系统的开发好像也是很有必要的,比如,一个现有的文件系统里面给了什么, 需要实现的仅仅是文件的组织形式。
    问题4:rtems里面有FILE,DIR吗