首页 > 解决方案 > 从标准输入创建一个以 null 结尾的字符串数组

问题描述

我正在尝试从标准输入中读取并使用execvp().

目前我收到许多警告,例如

警告:赋值从没有强制转换的指针生成整数 [-Wint-conversion] commands[i]=NULL;

警告:传递 'execvp' 的参数 1 使指针从整数而不进行强制转换 [-Wint-conversion] execvp(commands2[0],commands2);

# include <stdlib.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
# include <sys/wait.h>
# define BUF_SIZE 256
    

int main()
{
    char buffer[BUF_SIZE];
    char *commands;
    char *commands2;


    fgets(buffer, BUF_SIZE, stdin);
    commands = strtok(buffer, " ");

    printf("%d",commands[0]);
    while( commands != NULL ) 
    {
      printf( "%s", commands );
    
      commands = strtok(NULL, " ");
    }
    commands = realloc(commands,1);

    int i = strlen(commands);
    printf("%d",i);
    commands[i]=NULL;

    fgets(buffer, BUF_SIZE, stdin);
    commands2 = strtok(buffer, " ");
    
    while( commands2 != NULL ) 
    {
        commands2 = strtok(NULL, " ");
    }
    commands2 = realloc(commands2,1);
    
    int z = strlen(commands2);
    printf("%d",z);
    commands2[z]=NULL;

    int my_pipe[2];

    if (pipe(my_pipe) == -1)
    {
        perror("cannot create pipe\n");
    }

    pid_t my_pid;

    my_pid = fork();

    if (my_pid < 0)
    {
        perror("Failed fork\n");
    }

    if (my_pid > 0)
    {
        close(my_pipe[1]);
        dup2(my_pipe[0], 0);
        close(my_pipe[0]);

        wait(NULL);
        execvp(commands2[0],commands2); 
    }
    else
    {
        close(my_pipe[0]);   
        dup2(my_pipe[1], 1);
        close(my_pipe[1]);

        execvp(commands[0],commands);
    }
}

标签: cexecexecvp

解决方案


首先有一个关于使用指针的混淆。 char *commands是一个指针(指向一个字符)。 commands[i]是一个字符(不是指针)。 NULL是一个指针(类型void *和值0)所以,你不能写commands[i]=NULL. 但是你可以做到commands[i]='\0'(值 0 的字符)。

然后可能对预期的向量产生混淆execvp()。这个向量(execvp 的 v)是一个指针数组(一维)。这些指针中的每一个都是一个char *,一个字符串,一个单词。这个指针向量以指针 NULL 结束。

所以,我可以提出以下简单的代码(没有fork()和pipe()):

    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>

    #define BUF_SIZE 256
    #define MAX_ARGS 256
        
    int main() {
        char buffer[BUF_SIZE];
        char *argv[MAX_ARGS];
        int argc = 0;

        fgets(buffer, BUF_SIZE, stdin);
        
        for ( argv[argc] = strtok(buffer, " \t\n"); 
              argv[argc] != NULL; 
              argv[++argc] = strtok(NULL, " \t\n") ) {
          printf("argv[%d]=\"%s\"\n", argc, argv[argc]);
        }
        argv[argc] = NULL;

        execvp(argv[0], argv); 
        
        perror(argv[0]);
        exit(EXIT_FAILURE);
    }

推荐阅读