cd命令是很多操作系统的内部命令,但在我们的教程中仍然把它作为一个普通程序来实现。cd的功能为将当前工作目录切换至目标目录。实现如下:
session = malloc(sizeof(s_session));
session->current_path = malloc(SHELL_CMD_LEN);
int params[2];
params[0] = 7;
params[1] = (int) session;
__asm__ volatile("int $0x80" :: "a"(params));
char *path = malloc(SHELL_CMD_LEN);
str_copy(args[1], path);
int len = str_len(path);
if (path[len - 1] != '/')
{
path[len] = '/';
path[len + 1] = '\0';
}
char *full_path = malloc(SHELL_CMD_LEN);
full_path[0] = '\0';
if (path[0] == '/')
{
repath(path, NULL, full_path);
}
//不是以'/'开头: 相对路径
else
{
repath(path, session->current_path, full_path);
}
s_fs *fs_work = malloc(sizeof(s_fs));
u32 dev_id = 0;
int status = fs_find_path_by_user(full_path, session->uid, session->gid, &dev_id, fs_work);
if (status == FS_STATUS_NO_FILE_DIR)
{
printf("-bash: \"%s\": no such file or directory.\n", path);
}
else if (status == FS_STATUS_NO_PERMISSION)
{
printf("-bash: \"%s\": Permission denied.\n", path);
}
else
{
str_copy(full_path, session->current_path);
}
params[0] = 8;
params[1] = (int) session->current_path;
__asm__ volatile("int $0x80" :: "a"(params));
free(fs_work);
free(full_path);
free(session->current_path);
free(session);
运行结果:
源代码的下载地址为:
https https://github.com/magicworldos/lidqos.git
git git@github.com:magicworldos/lidqos.git
subverion https://github.com/magicworldos/lidqos
branch v0.30
Copyright © 2015-2023 问渠网 辽ICP备15013245号