c - 在 C.Fork() 中制作自定义 shell

标签 c linux shell input fork

因此,对于这个学校项目,我试图让我的 shell 执行基本的 fork() 命令,但是,我提供给我的代码的输入似乎不起作用。

当我在命令行中输入 fork() 时,程序会执行无效的输入部分。

我做错了什么?由于这是一个学校项目,我宁愿被指出正确的方向,而不是仅仅得到一个答案,如果那会很酷的话。 =)

#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM_ARGS 256
#define SIZE 256

void orders(char *command[SIZE]);

int main() {

    int pid = 0;
    int pid2 = 0;
    int childvalue =0;
    char buffer[SIZE]= "";
    //char input_args[MAX_NUM_ARGS];
    char **input_args = NULL;
    int i = 0;// counting variable
    printf("Welcome to my shell.\n");
    while(1){

    //initialize array of strings
    //first free any prevously allocated memory
    if (input_args != NULL)
    {   //memory has been allocated free it
        for (i = 0; i <MAX_NUM_ARGS; i++)
        {
            free(input_args[i]);
        }
    }   
    //free array of strings
    free(input_args);

    //new allocate memory
    input_args = (char**) malloc(sizeof(char*) * MAX_NUM_ARGS);
    //check return value for error
    if (input_args == NULL)
    {
        printf("We are out of memory. =(\n");
        continue;
        //print error: out of memory, exit with a error coce
    }
    //allocate memory for each string
    for (i = 0; i <MAX_NUM_ARGS; i++)
    { 
        input_args[i]= (char*)malloc(sizeof(char) * MAX_NUM_ARGS);
        if(input_args[i] == NULL)
            {//error

            printf("Error, the input is empty.");
            continue;
            }
    }


    printf("~$: "); //prompts the user for input
    fgets(buffer, sizeof(buffer), stdin);
    //if the user types in exit, quit
    if (strcmp(buffer, "exit\n") == 0){
        break;
    }
    //if user types in clear, wipe the screen and repeat the lop
    else if(strcmp(buffer, "clear\n")==0){

        system("clear");    
        continue;   

    }
    //should the user punch in nothing, repeat the loop
    else if (strcmp(buffer, "\n") == 0) {
        continue;
    }


    for (i = 0; i < SIZE; i++) {

        if(buffer[i] != '\n' || buffer[i] || ' ' || buffer[i] != '\t'){

        input_args[0][i] = buffer[i];

        }

    }

    //if the input doesn't fall under the conditionals above, execute orders.
    orders(input_args);


    } //end of while loop
     return 0;

    }//end of main function

    void orders(char *command[SIZE]){
    //handles the commands of the shell
        int pid =0;
    int pid2 = 0;
    int childValue = 0;
    if (strcmp(command, "fork()")== 0){


        pid = fork();//forks the process for the first time

        if (pid != 0){
            printf("I'm the parent, waiting on the child.\n");
            pid = waitpid(-1, &childValue,0);
            printf("Child %d returned a value of %x in hex.\n", pid,    childValue);
            return;//return backs to the main prompt
        }
        else{
            printf("I am the first child.\n");
            exit(2);//exits this process?
        }
    }
    //clears the screen
    else if(strcmp(command, "clear")==0){

        system("clear");        

        }

    else{//command outputted when you punch in a bad answer
        printf("Invalid command, %s.?\n", *command);
        }

    }   

最佳答案

导致您出现“无效命令”代码的原因有两个。

  1. 您不是在比较字符串。 command由于某种原因是一个指针数组,char **不是传递给 strcmp() 的合法第一个参数.

  2. 您在命令字符串中包含换行符。那会导致你所有的strcmp()函数调用失败,即使您修复了 #1。

您的程序中还有其他逻辑错误:

  1. 在这一行中:

    if(buffer[i] != '\n' || buffer[i] || ' ' || buffer[i] != '\t'){
    

    您可能想要第二个 ||成为== .

  2. 打印 stat_loc 时从 waitpid(2) 返回值,您应该使用手册页中列出的宏。在您的情况下,您似乎正在寻找 WEXITSTATUS(childValue) .

关于c - 在 C.Fork() 中制作自定义 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18991591/

相关文章:

c - 如何在C中打印数组中值的位置

linux - 在 Windows 上的 64 位 Ubuntu 上编译 32 位程序集

linux - 嵌套函数和 while 循环在 bash 中返回 `while: command not found`

php - 如何使用 PHP shell_exec 添加新用户到 Kamailio?

c++ - 是否有任何好的算法实现来检测相似图像?

c - 为什么这个程序不打印在我的文件上?

c - 将 *next 列表指针设置为 NULL

linux - Xcode 未在 Linux 上列为 CMake 生成器

html - 浏览器从标准输入读取 HTML

php - 使用PHP构建gradle Android项目