之前做过bash的简单实现,github上有很多的代码
实现bash的简单功能,比如ls,使用的就是调用内核的接口,找出文件的fstat,然后将其列出打印,实现模拟ls的功能,但是不清楚系统的ls命令是否就是这样实现的
#include <stdio.h>
#include<stdlib.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<time.h>
#include<string.h>
#include<pwd.h>
#include<grp.h>
void dir_oper(char const*path);
int main(int argc, char const *argv[])
{
//FILE *file_p;
FILE *fp = fopen("inf.txt", "w+");
if(!fp)
{
printf("open filepath failed\n");
return 0;
}
char const* path = argv[1];
struct stat s_buf;
stat(path, &s_buf);
if(S_ISDIR(s_buf.st_mode))
{
dir_oper(path);
}
//若输入的路径就是文件
else if(S_ISREG(s_buf.st_mode))
{
printf("[%s] 是普通文件 %d \n",
path, (int)s_buf.st_size);
fprintf(fp, "[%s]是普通文件 %d \n",
path, (int)s_buf.st_size);
}
return 0;
}
void dir_oper(char const*path)
{
FILE *fp = fopen("inf.txt", "w");
if(!fp)
{
printf("filepath open failed\n");
exit(0);
}
printf("%s/ 是目录. \n", path);
fprintf(fp, "%s/ 是目录. \n", path);
struct dirent *filename;
struct stat s_buf;
DIR *dp = opendir(path);
//readdir()循环
while(filename = readdir(dp))
{
char file_path[200];
bzero(file_path,200);
strcat(file_path,path);
strcat(file_path,"/");
strcat(file_path,filename->d_name);
//排除目录下的隐藏文件. ..
if(strcmp(filename->d_name, ".") == 0 ||
strcmp(filename->d_name, "..") == 0)
{
continue;
}
stat(file_path,&s_buf);
if(S_ISDIR(s_buf.st_mode))
{
//printf("进来了, 是目录\n");
dir_oper(file_path);
printf("\n");
}
//char *name = ctime(&s_buf.st_mtim);
//char mtime[512] = {0};
//strncpy(mtime, time, strlen(time) - 1);
char *file_user = getpwuid(s_buf.st_uid)->pw_name;
char power[11] = {0};
// 文件所有者
power[0] = '-';
power[1] = (s_buf.st_mode & S_IRUSR) ? 'r' : '-';
power[2] = (s_buf.st_mode & S_IWUSR) ? 'w' : '-';
power[3] = (s_buf.st_mode & S_IXUSR) ? 'x' : '-';
// 所属组
power[4] = (s_buf.st_mode & S_IRGRP) ? 'r' : '-';
power[5] = (s_buf.st_mode & S_IWGRP) ? 'w' : '-';
power[6] = (s_buf.st_mode & S_IXGRP) ? 'x' : '-';
// 其他
power[7] = (s_buf.st_mode & S_IROTH) ? 'r' : '-';
power[8] = (s_buf.st_mode & S_IWOTH) ? 'w' : '-';
power[9] = (s_buf.st_mode & S_IXOTH) ? 'x' : '-';
if(S_ISREG(s_buf.st_mode))
{
printf("[%s]是普通文件 %s %d %s \n",
file_path, power, (int)s_buf.st_size, file_user);
fprintf(fp, "[%s] 是普通文件%s %d %s \n",
file_path, power, (int)s_buf.st_size, file_user);
}
}
}
问题1:这个代码中使用了FILE。DIR,dirent,stat等VFS提供的结构体,然后使用了一些简单的c函数,实现了ls,
那么挂载文件系统的时候,是不是同样的也是类似使用FILE这些结构体,实现ls这些命令,来访问ext4?
问题2:如果这个前提成立,那么ext4是否可以直接挂载到rtems上,只是内容读不出来?需要做的只是实现这些类bash的接口?
问题3:所有的功能实现都是基于现有的API,rtems文件系统开发手册上给出了很多API,都是用上面的吗,那这样的话,了解一个文件系统的开发好像也是很有必要的,比如,一个现有的文件系统里面给了什么, 需要实现的仅仅是文件的组织形式。
问题4:rtems里面有FILE,DIR吗