C 中的命令行解释器

标签 c shell warnings

我想创建一个命令行解释器,每行有多个命令。 我将从键盘输入命令,例如: pwd ; ls ;猫文件; ls -l

我写了这个,有人可以帮我解决警告吗?

警告是:

ex.c: In function ‘Execute’:

ex.c:33:18: warning: passing argument 1 of ‘execvp’ from incompatible pointer type [-Wincompatible-pointer-types]
        if(execvp(cmd, pin) == -1) {

In file included from ex.c:3:0:

/usr/include/unistd.h:581:12: note: expected ‘const char *’ but argument is of type ‘char **’
 extern int execvp (const char *__file, char *const __argv[])


ex.c: In function ‘main’:

ex.c:105:17: warning: passing argument 1 of ‘Execute’ from incompatible pointer type [-Wincompatible-pointer-types]

         Execute(pin[0],pin);


ex.c:21:6: note: expected ‘char **’ but argument is of type ‘char *’
 void Execute(char* cmd[],char* pin[]) {
      ^~~~~~~

抱歉英语不好。

#include <sys/wait.h>    
#include <sys/types.h>    
#include <unistd.h>    
#include <string.h>    
#include <stdlib.h>     
#include <stdio.h>

#define MAX_SIZE 512

//Clean function
void Clean(char* cmd[],char* pin[]) {          //Clean Array

    int a;
    for(a=0; a < 40; a++){
            cmd[a] = NULL;
            pin[a] = NULL;
    }        
}

//execute commands 

void Execute(char* cmd[],char* pin[]) { 

    pid_t pid;
    pid = fork();    

    switch(pid) {
        case -1:  
          printf("DEBUG:Fork Failure\n");
          exit(-1);
        case  0:
          execvp(*cmd, pin);

          if(execvp(cmd, pin) == -1) {
                printf("Command Not Found\n");
                exit(0);
          }

        default:  
        wait(NULL);
        printf("DEBUG:Child Finished\n");    
    }
}

int main() {
    char* cnd;
    char* cmd[40];
    char* pin[40];
    char* array;
    int how_much;

    char *input = malloc (MAX_SIZE);

    if (input == NULL) {
        printf ("No memory\n");
        return 1;
    }

    Clean(cmd,pin);

    printf("shell> ");

    fgets (input, MAX_SIZE, stdin);

    if ((strlen(input)>0) && (input[strlen (input) - 1] == '\n')) {
        input[strlen (input) - 1] = '\0';
    }

    printf("INPUT: %s\n", input);

//searching for ";"

    cnd = strtok(input, ";");

    int i = 0;

    while(cnd != NULL) {    
        cmd[i] = cnd;
        i++;
        cnd = strtok(NULL, ";");
    }

    cmd[i] = NULL;
    how_much = i;

// searching for " "    

    for(int j=0; j < how_much; j++) {            
        array = strtok(input, " ");

        int k=0;

        while(array != NULL) {  
            pin[k] = array;
            k++;
            array = strtok(NULL, " ");
        }

        pin[k] = NULL;

        Execute(pin[0],pin);
    }

    free (input);

   return 1;
}

最佳答案

执行函数中:

...
case  0:
  execvp(*cmd, pin);

  if (execvp(cmd, pin) == -1) {
  ...

您首先有execvp(*cmd, pin),然后是execvp(cmd, pin)。 这是否应该表明您出了什么问题。

这里:

Execute(pin[0],pin);

在第一个参数中,Execute 需要一个 char*数组,但您提供了一个 char*

可能还有更多问题。

关于C 中的命令行解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42511520/

相关文章:

c++ - 有没有办法找到导致 Qt 中出现警告的行?

c - 是什么导致我的数组充满了不需要的数字

unix - 为什么shell脚本比较经常使用x$VAR = xyes?

c - C 中结构体数组初始化为 0,警告 : missing braces around initializer

c# - AutoResetEvent - 两次快速调用不能保证线程释放 - 为什么?

linux - Linux 中的恢复脚本第 2 部分

c - 从列表中删除符合受另一个列表影响的条件的所有节点

c - ELF 二进制文件中默认信号处理程序的代码在哪里?

c++ - 为什么 float 可以比较?

linux - sed 行删除是原子的吗?