读取菜单选项的问题,用 C 代码编写

问题描述 投票:0回答:0

我正在编写一个代码,它应该与子进程一起使用,使用不同的方式发送参数。但是,问题与这部分无关。我有一个带有选项的菜单,它指定了用户可以处理子进程的方式。根据选择的选项,应该选择特定的案例来工作。它没有按应有的方式工作,这里是代码和输出,这将比我更好地解释问题。

附言抱歉语言不同和可能的错误,希望有人能帮助我。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <locale.h>
#include <string.h>
#include <math.h>

extern char **environ;

int main(int argc, const char *argv[], char *env[])
{
    char **name;
    int x = 0, num;
    pid_t pid;
    
    for(num=0; ; num++)    //counts the number of environment parameters
        if(env[num] == NULL)
            break;
    
    name = (char **) calloc(1, sizeof(char *));
    if(!name)
    {
        printf("\nMemory isn't allocated");
        return -1;
    }
    
    for(int j=0; j<num; j++)
    {
        name[j] = (char *) calloc(9, sizeof(char));
        if(!name[j])
        {
            printf("\nMemory isn't allocated");
            return -1;
        }
    }

    for(int i=0; i<num; i++)
        fprintf(stdout, "%s\n", args[i]);
    
    fprintf(stdout, "Parent process's started...\n");
    
    char *temp;
    temp = (char *) calloc(12, sizeof(char));
    if(!temp)
    {
        printf("\nMemory isn't allocated");
        return -1;
    }
    
    while(x < 100)
    {
        sprintf(temp, "%d", x);
        strcpy(name[0], "child_");
        strcat(name[0], temp);
        
        puts("path info: +-using getenv()");
        puts("                           *-scanning array of environment parameters");
        puts("                           &-using environ");
        puts("                           q-end");
        fflush(stdin);
        
        switch(getchar())
        {
            case '+':
                printf("PLUS:\n");
                
                pid = fork();
                
                if(pid == -1)
                {
                    fprintf(stdout, "Error, code - %d\n", errno);
                }
                
                else
                {
                    execve("./child", name, env);
                }
                
                break;
                
            case '*':
                printf("ASTERISK\n");
                
                pid = fork();
                
                if(pid == -1)
                {
                    fprintf(stdout, "Error, code - %d\n", errno);
                }
                
                else
                {
                    execve(findPath(env, num), name, env);
                }
                
                break;
                
            case '&':
                printf("AMPERSAND:\n");
                
                pid = fork();
                
                if(pid == -1)
                {
                    fprintf(stdout, "Error, code - %d\n", errno);
                }
                
                else
                {
                    execve(findPath(environ, num), name, env);
                }
                
                break;
                
            case 'q':
                return 0;
                
            default:
                printf("Wrong parameter\n");
                return -1;
        }
        
        x++;
    }

    exit(0);
}

输出如下:

Parent process's started...  
path info: +-using getenv()  
           *-scanning array of environment parameters  
           &-using environ  
           q-end  
+  
PLUS:  
path info: +-using getenv()  
           *-scanning array of environment parameters  
           &-using environ  
           q-end 
path info: +-using getenv()  
           *-scanning array of environment parameters  
           &-using environ  
           q-end  
q  
Program ended with exit code: 0  

同时,它应该是这样的:

Parent process's started...  
path info: +-using getenv()  
           *-scanning array of environment parameters  
           &-using environ  
           q-end  
+  
PLUS:  
path info: +-using getenv()  
           *-scanning array of environment parameters  
           &-using environ  
           q-end  
q  
Program ended with exit code: 0
c process menu switch-statement
© www.soinside.com 2019 - 2024. All rights reserved.