将系统的使用更改为 C 中的 Fork 和 exec

标签 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 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; 
}

最佳答案

我假设您有一个 POSIX 系统,例如 GNU/Linux。

这是一个很常见的问题。第一个答案是研究免费 C 库(如 GNU Libc)中 system 函数的实现。 。许多关于 Unix 的好书也讨论了这个问题。

作为线索,systemfork-ing 一起使用,然后在 shell /bin/的子进程 execve 中工作嘘

关于将系统的使用更改为 C 中的 Fork 和 exec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7878219/

相关文章:

c++ - 从 c/c++ 代码更改 Linux 用户运行时

从内核模块创建 sysfs 条目

c - 通过管道向子进程写入和读取不起作用

python - subprocess.communicate() 仅在从脚本运行时神秘地挂起

r - 如何使用 R 针对不同参数值绘制同一变量的多条曲线?

eclipse 调试透视图中的 android 系统统计信息

c - C 程序的单元测试

c - 我可以用什么来等待创建所有线程?

c - 从另一个 exe 运行 exe

c - 如何存储fscanf以逆序打印?