在C中将system的用法更改为Fork和exec
我有一个C程序,它接受一定数量的输入并作为系统命令运行。其余部分将传递给shell执行。然而,有人建议我应该尝试使用fork和exec来运行命令。不过,我对如何做到这一点感到困惑在C中将system的用法更改为Fork和exec,c,unix,system,exec,fork,C,Unix,System,Exec,Fork,我有一个C程序,它接受一定数量的输入并作为系统命令运行。其余部分将传递给shell执行。然而,有人建议我应该尝试使用fork和exec来运行命令。不过,我对如何做到这一点感到困惑 #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define MAX_BUFFER 1024 // max li
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_BUFFER 1024 // max line buffer
#define MAX_ARGS 64 // max # args
#define SEPARATORS " \t\n" // token sparators
extern char **environ;
/*******************************************************************/
int main (int argc, char ** argv)
{
char linebuf[MAX_BUFFER]; // line buffer
char cmndbuf[MAX_BUFFER]; // command buffer
char * args[MAX_ARGS]; // pointers to arg strings
char ** arg; // working pointer thru args
char * prompt = "==>" ; // shell prompt
// keep reading input until "quit" command or eof of redirected input
while (!feof(stdin)) {
// get command line from input
fputs (prompt, stdout); // write prompt
fflush(stdout);
if (fgets(linebuf, MAX_BUFFER, stdin )) { // read a line
// tokenize the input into args array
arg = args;
*arg++ = strtok(linebuf,SEPARATORS); // tokenize input
while ((*arg++ = strtok(NULL,SEPARATORS)));
// last entry will be NULL
if (args[0]) { // if there's anything there
cmndbuf[0] = 0; // set zero-length command string
// check for internal/external command
if (!strcmp(args[0],"clr")) { // "clr" command
strcpy(cmndbuf, "clear");
} else
if (!strcmp(args[0],"cd"))
{
int ret;
if (!args[1])
strcpy(cmndbuf, "pwd");
ret = chdir(args[1]);
strcpy(cmndbuf, "pwd");
}else
if (!strcmp(args[0],"dir")) { // "dir" command
strcpy(cmndbuf, "ls -al ");
if (!args[1])
args[1] = "."; // if no arg set current directory
strcat(cmndbuf, args[1]);
} else
if (!strcmp(args[0],"environ")) { // "environ" command
char ** envstr = environ;
while (*envstr) { // print out environment
printf("%s\n",*envstr);
envstr++;
} // (no entry in cmndbuf)
} else
if (!strcmp(args[0],"quit")) { // "quit" command
break;
} else { // pass command on to OS shell
int i = 1;
strcpy(cmndbuf, args[0]);
while (args[i]) {
strcat(cmndbuf, " ");
strcat(cmndbuf, args[i++]);
}
}
// pass any command onto OS
if (cmndbuf[0])
system(cmndbuf);
}
}
}
return 0;
}
#包括
#包括
#包括
#包括
#定义最大缓冲区1024//最大行缓冲区
#定义最大参数64//最大参数
#定义分隔符“\t\n”//token sparators
外部字符**环境;
/*******************************************************************/
int main(int argc,字符**argv)
{
char linebuf[MAX_BUFFER];//行缓冲区
char cmndbuf[MAX_BUFFER];//命令缓冲区
char*args[MAX_args];//指向arg字符串的指针
char**arg;//通过args的工作指针
char*prompt=“==>”;//shell提示符
//继续读取输入,直到“退出”命令或重定向输入的eof
而(!feof(stdin)){
//从输入获取命令行
fputs(提示符,stdout);//写提示符
fflush(stdout);
if(fgets(linebuf,MAX_BUFFER,stdin)){//读取一行
//将输入标记化为args数组
arg=args;
*arg++=strtok(linebuf,分隔符);//标记化输入
while((*arg++=strtok(NULL,分隔符));
//最后一个条目将为空
if(args[0]){//如果有什么
cmndbuf[0]=0;//设置零长度命令字符串
//检查内部/外部命令
如果(!strcmp(args[0],“clr”){/“clr”命令
strcpy(cmndbuf,“清晰”);
}否则
如果(!strcmp(args[0],“cd”))
{
int ret;
如果(!args[1])
strcpy(cmndbuf,“pwd”);
ret=chdir(args[1]);
strcpy(cmndbuf,“pwd”);
}否则
if(!strcmp(args[0],“dir”){/“dir”命令
strcpy(cmndbuf,“ls-al”);
如果(!args[1])
args[1]=“;//如果没有arg,则设置当前目录
strcat(cmndbuf,args[1]);
}否则
如果(!strcmp(args[0],“environ”){/“environ”命令
char**envstr=environ;
while(*envstr){//打印输出环境
printf(“%s\n”,*envstr);
envstr++;
}//(cmndbuf中无条目)
}否则
如果(!strcmp(args[0],“quit”){/“quit”命令
打破
}else{//将命令传递到OS shell
int i=1;
strcpy(cmndbuf,args[0]);
while(args[i]){
strcat(cmndbuf,“”);
strcat(cmndbuf,args[i++]);
}
}
//将任何命令传递到操作系统
if(cmndbuf[0])
系统(cmndbuf);
}
}
}
返回0;
}
我假设您有一个POSIX系统,例如GNU/Linux
这是一个非常常见的问题。第一个答案是研究免费C库中system
函数的实现,比如。许多关于Unix的好书也涉及到这个问题
作为线索,
system
使用fork
-ing,然后在shell的子进程execve
中/bin/sh
我假设您有一个POSIX系统,例如GNU/Linux
这是一个非常常见的问题。第一个答案是研究免费C库中system
函数的实现,比如。许多关于Unix的好书也涉及到这个问题
作为线索,system
使用fork
-ing,然后在shell的子进程execve
/bin/sh
中使用Brian Kernighan和Rob Pike的“system”函数实现示例
#include <signal.h>
system(s) /* выполнить командную строку s */
char *s;
{
int status, pid, w, tty;
int (*istat)(), (*qstat)();
extern char *progname;
fflush(stdout);
tty = open("/dev/tty", 2);
if (tty == 1) {
fprintf(stderr, "%s: can't open /dev/tty\n", progname);
return 1;
}
if ((pid = fork()) == 0) {
close(0); dup(tty);
close(1); dup(tty);
close(2); dup(tty);
close(tty);
execlp("sh", "sh", " c", s, (char *) 0);
exit(127);
}
close(tty);
istat = signal(SIGINT, SIG_IGN);
qstat = signal(SIGQUIT, SIG_IGN);
while ((w = wait(&status)) != pid && w != 1);
if (w == 1)
status = 1;
signal(SIGINT, istat);
signal(SIGQUIT, qstat);
return status;
}
#包括
系统/系统*/
char*s;
{
int状态,pid,w,tty;
int(*istat)(,(*qstat)();
外部字符*progname;
fflush(stdout);
tty=打开(“/dev/tty”,2);
如果(tty==1){
fprintf(stderr,“%s:无法打开/dev/tty\n”,progname);
返回1;
}
如果((pid=fork())==0){
关闭(0);dup(tty);
关闭(1);dup(tty);
关闭(2);dup(tty);
关闭(tty);
execlp(“sh”,“sh”,“c”,s,(char*)0);
出口(127);
}
关闭(tty);
istat=信号(信号输入,信号输出);
qstat=信号(SIGQUIT,SIG_IGN);
while((w=wait(&status))!=pid&&w!=1);
如果(w==1)
状态=1;
信号(SIGINT、istat);
信号(SIGQUIT、qstat);
返回状态;
}
请注意,本书是在C标准最终确定之前编写的,因此它没有使用原型。Brian Kernighan和Rob Pike的“系统”功能实现示例
#include <signal.h>
system(s) /* выполнить командную строку s */
char *s;
{
int status, pid, w, tty;
int (*istat)(), (*qstat)();
extern char *progname;
fflush(stdout);
tty = open("/dev/tty", 2);
if (tty == 1) {
fprintf(stderr, "%s: can't open /dev/tty\n", progname);
return 1;
}
if ((pid = fork()) == 0) {
close(0); dup(tty);
close(1); dup(tty);
close(2); dup(tty);
close(tty);
execlp("sh", "sh", " c", s, (char *) 0);
exit(127);
}
close(tty);
istat = signal(SIGINT, SIG_IGN);
qstat = signal(SIGQUIT, SIG_IGN);
while ((w = wait(&status)) != pid && w != 1);
if (w == 1)
status = 1;
signal(SIGINT, istat);
signal(SIGQUIT, qstat);
return status;
}
#包括
系统/系统*/
char*s;
{
int状态,pid,w,tty;
int(*istat)(,(*qstat)();
外部字符*progname;
fflush(stdout);
tty=打开(“/dev/tty”,2);
如果(tty==1){
fprintf(stderr,“%s:无法打开/dev/tty\n”,progname);
返回1;
}
如果((pid=fork())==0){
关闭(0);dup(tty);
关闭(1);dup(tty);
关闭(2);dup(tty);
关闭(tty);
execlp(“sh”,“sh”,“c”,s,(char*)0);
出口(127);
}
关闭(tty);
istat=信号(信号输入,信号输出);
qstat=信号(SIGQUIT,SIG_IGN);
while((w=wait(&status))!=pid&&w!=1);
如果(w==1)
状态=1;
信号(SIGINT、istat);
信号(SIGQUIT、qstat);
返回状态;
}
请注意,这本书是在C标准最终确定之前编写的,因此它没有使用原型。您有什么理由这样做吗