For a project in C, we need to build a shell over a Unix server.
对于C语言中的项目,我们需要在Unix服务器上构建一个shell。
It needs to be able to execute functionality native to Unix, and not bash (or any other Unix shell).
它需要能够执行Unix本机的功能,而不是bash(或任何其他Unix shell)。
I am writing a method in the hopes to generalize a call to a command. into a general function:
我正在编写一种方法,希望能够概括对命令的调用。进入一般功能:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <limits.h>
void execCmd(char* cmd,char* options)
{
char directory[MAX_CANON] = "/bin/"; //currently is just set to /bin
char* toExec = strcat(directory,cmd);
I would like to set the variable 'directory' to the proper directory for the Unix command I'm calling.
我想将变量'directory'设置为我正在调用的Unix命令的正确目录。
So, in essence, I would like to be able to use something like char directory[MAX_CANON] = which(cmd);
所以,从本质上讲,我希望能够使用类似char目录[MAX_CANON] = which(cmd);
so that the which function will return me the directory of the command I'm trying to call.
以便哪个函数将返回我正在尝试调用的命令的目录。
If you feel this is not a good solution, please recommend some other way.
如果您认为这不是一个好的解决方案,请推荐其他方式。
Thanks
EDIT: I guess at worst I could do a massive if-else statement, setting the 'directory' vbl based on the cmd parameter.
编辑:我想最糟糕的是我可以做一个巨大的if-else语句,根据cmd参数设置'目录'vbl。
3 个解决方案
#1
What you're asking for is the command 'which', ie
你要求的是命令'which',即
me@*:~$ which which
/usr/bin/which
On linux it's a shell script, so take a look a gander.
在linux上它是一个shell脚本,所以看一看。
However, I don't think this is required to make a UNIX shell. If you type
但是,我不认为这是制作UNIX shell所必需的。如果你输入
man 3 exec
you'll find that execlp() and execvp() take care of the details of searching PATH for the given command and executing it.
你会发现execlp()和execvp()负责处理搜索给定命令的PATH并执行它的细节。
#2
To implement this natively, I would:
为了实现这一点,我会:
- Use
getenv
to acquire thePATH
- parse the
PATH
into a list of candidate components (strtok
for all its faults is the classic tool), - For each
PATH
component: form/possible/path/to/cmd
and use astat
family function to test fif this file exists and the user has execute permission. Continue until you find it or run out ofPATH
...
使用getenv获取PATH
将PATH解析为候选组件列表(strtok的所有错误是经典工具),
对于每个PATH组件:form / possible / path / to / cmd并使用stat系列函数来测试该文件是否存在,并且用户具有执行权限。继续,直到找到它或用完PATH ...
Edit: You may want to follow symlinks, too.
编辑:您可能也想要遵循符号链接。
#3
Not that I'm aware off, but you can mimic the which functionality on a helper function. I'll need to search over all paths in the PATH environment variable for a file named as your command, and after that check if that file is executable, then you probably found your executable.
不是我知道,但你可以模仿辅助函数的功能。我需要在PATH环境变量中的所有路径上搜索一个名为命令的文件,然后检查该文件是否可执行,那么您可能找到了可执行文件。
To get the PATH variable you can use getenv(). You'll need to split it with strtok(). For searching a directory you can use opendir(), will be something like this:
要获取PATH变量,可以使用getenv()。你需要用strtok()拆分它。要搜索目录,您可以使用opendir(),将是这样的:
#include <sys/types.h>
#include <dirent.h>
...
DIR *dir;
struct dirent *dp;
...
if ((dir = opendir (".")) == NULL) {
perror ("Cannot open .");
exit (1);
}
while ((dp = readdir (dir)) != NULL) {
}
...
Check for dirent struct on the readdir() function man page.
检查readdir()函数手册页中的dirent结构。
#1
What you're asking for is the command 'which', ie
你要求的是命令'which',即
me@*:~$ which which
/usr/bin/which
On linux it's a shell script, so take a look a gander.
在linux上它是一个shell脚本,所以看一看。
However, I don't think this is required to make a UNIX shell. If you type
但是,我不认为这是制作UNIX shell所必需的。如果你输入
man 3 exec
you'll find that execlp() and execvp() take care of the details of searching PATH for the given command and executing it.
你会发现execlp()和execvp()负责处理搜索给定命令的PATH并执行它的细节。
#2
To implement this natively, I would:
为了实现这一点,我会:
- Use
getenv
to acquire thePATH
- parse the
PATH
into a list of candidate components (strtok
for all its faults is the classic tool), - For each
PATH
component: form/possible/path/to/cmd
and use astat
family function to test fif this file exists and the user has execute permission. Continue until you find it or run out ofPATH
...
使用getenv获取PATH
将PATH解析为候选组件列表(strtok的所有错误是经典工具),
对于每个PATH组件:form / possible / path / to / cmd并使用stat系列函数来测试该文件是否存在,并且用户具有执行权限。继续,直到找到它或用完PATH ...
Edit: You may want to follow symlinks, too.
编辑:您可能也想要遵循符号链接。
#3
Not that I'm aware off, but you can mimic the which functionality on a helper function. I'll need to search over all paths in the PATH environment variable for a file named as your command, and after that check if that file is executable, then you probably found your executable.
不是我知道,但你可以模仿辅助函数的功能。我需要在PATH环境变量中的所有路径上搜索一个名为命令的文件,然后检查该文件是否可执行,那么您可能找到了可执行文件。
To get the PATH variable you can use getenv(). You'll need to split it with strtok(). For searching a directory you can use opendir(), will be something like this:
要获取PATH变量,可以使用getenv()。你需要用strtok()拆分它。要搜索目录,您可以使用opendir(),将是这样的:
#include <sys/types.h>
#include <dirent.h>
...
DIR *dir;
struct dirent *dp;
...
if ((dir = opendir (".")) == NULL) {
perror ("Cannot open .");
exit (1);
}
while ((dp = readdir (dir)) != NULL) {
}
...
Check for dirent struct on the readdir() function man page.
检查readdir()函数手册页中的dirent结构。