在我自己的 linux shell 上运行“ps”命令的问题

问题描述

作为我的学院项目的一部分,我正在构建我自己的 linux shell 一段时间。当我尝试在命令行“/usr/bin/wc text_file.txt”之后运行“ps”命令时遇到了一个问题。当我尝试这样做时 - 显示以下错误消息:

0: /usr/bin/wc shell.c
  751  3273 24140 shell.c
1: ps
error: garbage option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).

followig 代码负责在没有路径(又名“ps”)的情况下在我的 shell 中运行命令:

void cmd_commands_without_path(char** parsed,unused struct tokens *tokens) 
{
    pid_t pid = fork(); // Forking a child 
    char* s = getenv("PATH"); // Holds the full environment var path.
    int i = 0; // Counter.
    int symbol_counting = 0; // Holds the number of ":" in the full environment path.
    char *temp; // Helps with the initielizing of the new path.
    char *symbol = "/"; // Holds "/" for correcting the new path.


    /* Puts the user command into an array with NULL in the end. */
    while((tokens_get_token(tokens,i)) != NULL) // While we did not reached to the end of user input.
    {
        parsed[i] = tokens_get_token(tokens,i); // Initielize parsed to hold the user input (command).
        i++;
    }
    parsed[i+1] = NULL; // The end of the user input (command) will be NULL (for execv).

    /* Count the number of ":" in the path. */
    for(i = 0; i < strlen(s); i++)
    {
        if(s[i] == ':')
        {
            symbol_counting++; // increasing symbol counting.
        }
    }
    symbol_counting++; // Because have to be an address after the last ":"
    
    if (pid == -1) // If the creation of the child was unsuccessful.
    { 
        printf("\nFailed forking child.."); 
        return; 
    } 
    else if (pid == 0) // The child was returned.
    { 
     setpgrp(); // Changing the child group id.
     tcsetpgrp(0,getpgrp()); // Return the proccess id of the child.
     
     /* Signals for "ctrl" options. */
     signal(SIGINT,SIG_DFL);
     signal(SIGQUIT,SIG_DFL);
     signal(SIGKILL,SIG_DFL);
     signal(SIGTERM,SIG_DFL);
     signal(SIGTSTP,SIG_DFL);
     signal(SIGCONT,SIG_DFL);
     signal(SIGTTIN,SIG_DFL);
     signal(SIGTTOU,SIG_DFL);
     
        char* full_array[(symbol_counting+1)]; // Create new array at the size of the number of ":".
        i = 0; // Restarting the counter.
        
        /* Initielize array with the seperate environment variable paths. */
        full_array[i] = strtok(s,":");
        for(i = 1; i < symbol_counting; i++)
        {
            full_array[i] = strtok(NULL,":");
        }
        
        for(i = 0; i < symbol_counting; i++) // Runs as the number of phats.
        {
            temp = (char*)malloc(MAXLIST*sizeof(char)); // Allocate to temp size of 1024 chars.
            temp[0] = '\0'; // Erasing temp.
        
        if(temp[0] == '\0') // If the word was ended.
        {
            strcpy(temp,full_array[i]); // Adds to temp the path we currently gonna check.
            temp = strcat(temp,symbol); // Adds "/" to the new path.
            temp = strcat(temp,parsed[0]); // Adds to the new path the user command.
            
        
            if(access(temp,X_OK) == 0) // If the command was found in the path.
            {
                execv(temp,parsed); // Executing the command.
            }
            temp[0] = '\0'; // Erasing temp again.
        }
        }
        return; 
    } 
    else // pid > 0
    { 
        // waiting for child to terminate 
        wait(NULL);
        tcsetpgrp(0,getpgrp()); // Return the proccess id of the child.
        return; 
    } 
}

我还应该说,只有当我尝试在命令“/usr/bin/wc text.txt”之后运行“ps”命令时才会出现此错误。 谁能告诉我我做错了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)