chdir
将更改您程序的当前目录,可能是您正在尝试实现的命令外壳。
绝对路径和相对路径,例如 /
、..
、subdir
都没有问题,可以直接传递给 chdir
系统调用。
-
是 cd
命令的一个特殊参数:cd -
表示返回上一个当前目录。要实现此功能,您需要跟踪 cd
更改到的最后一个目录。在调用 getcwd()
之前使用 chdir
函数,如果成功,则将之前的目录保留在全局数组中。
~
是另一件特别的事情:它应该在分派到命令处理程序之前扩展到主目录(HOME
环境变量的值),因此可以键入 cd ~
,或者只是 cd
更改到主目录,而 cd "~"
更改为名为 "~"
的目录。 ~
应扩展为 $(HOME)
,作为独立字符串或路径的初始部分:~/bin
。请注意,~name
应扩展到用户 name
的主目录。
这是一个部分和简单的版本:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char lastdir[MAX_PATH]; // initialized to zero
int exec_cd(char *arg) {
char curdir[MAX_PATH];
char path[MAX_PATH];
if (getcwd(curdir,sizeof curdir)) {
/* current directory might be unreachable: not an error */
*curdir = '\0';
}
if (arg == NULL) {
arg = getenv("HOME");
}
if (!strcmp(arg,"-")) {
if (*lastdir == '\0') {
fprintf(stderr,"no previous directory\n");
return 1;
}
arg = lastdir;
} else {
/* this should be done on all words during the parse phase */
if (*arg == '~') {
if (arg[1] == '/' || arg[1] == '\0') {
snprintf(path,sizeof path,"%s%s",getenv("HOME"),arg + 1);
arg = path;
} else {
/* ~name should expand to the home directory of user with login `name`
this can be implemented with getpwent() */
fprintf(stderr,"syntax not supported: %s\n",arg);
return 1;
}
}
}
if (chdir(arg)) {
fprintf(stderr,"chdir: %s: %s\n",strerror(errno),path);
return 1;
}
strcpy(lastdir,curdir);
return 0;
}
本文链接:https://www.f2er.com/525446.html