首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >execvp:地址错误

execvp:地址错误
EN

Stack Overflow用户
提问于 2013-12-08 10:32:15
回答 1查看 18.5K关注 0票数 5

我正在linux上创建一个带有各种命令的shell。我有不同的内置命令,其中之一是“历史”。我有一个reshist()函数来重置包含用户输入的数组。我还想使用execvp()和多管道操作来启用系统命令。

reshist()函数和多管道操作在它们不在一起时工作得很好,但是当我同时使用它们时,它会导致execvp()抛出“错误地址”错误。

我知道reshist()函数不能正确地将输入添加到列表中,但这不是什么大问题。问题是为什么我会得到这个错误。

可能的原因是什么?有没有更好的办法让他们一起工作?

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>

#define MAX_BUFFER 129                       // max line buffer
#define MAX_ARGS 32                            // max # args
#define SEPARATORS " \t\n"                     // token sparators
char *args[MAX_ARGS];

int print[16];
int get[16];
int fd[2];

char histarr[10][129];  // History array
char histel[129];

void reshist(void)
{
  //HISTORY RESORTING

  int counter = 0;
  while (counter < 10)
  {  //shifting all elements by one from the last element of the list
    if (histarr[counter] == NULL )
    {
      strcpy(histarr[counter], histel); //first element of the history will contain the last command
      break;
    }
    counter++;
  }
  if (counter == 10)
  {
    counter = 1;
    while (counter < 10)
    {
      strcpy(histarr[counter - 1], histarr[counter]);
      counter++;
    }
    strcpy(histarr[9], histel);
  }

  memset(histel, 0, 127);
  //HISTORY RESORT ENDS
}

void setup(void)
{

  char buf[MAX_BUFFER];                      // line buffer
  //char * args[MAX_ARGS];                     // pointers to arg strings
  char ** arg;                               // working pointer thru args
  char * prompt = "333.sh>";                    // shell prompt

  /* keep reading input until "quit" command or eof of redirected input */

  while (!feof(stdin))
  {

    /* get command line from input */
    fputs(prompt, stdout);                // write prompt
    if (fgets(buf, MAX_BUFFER, stdin))
    { // read a line

      /* tokenize the input into args array */

      arg = args;
      *arg++ = strtok(buf, SEPARATORS);   // tokenize input
      while ((*arg++ = strtok(NULL, SEPARATORS)))
        ;
      // last entry will be NULL 

      strcpy(histel, buf);
      reshist();

      pid_t pid;
      int print[16];
      int get[16];
      int fd[2];

      int count = 0;
      int i = 0;
      while (args[i] != NULL )
      {
        if (0 == strcmp(args[i], "|"))
        {
          count++;
        }
        i++;
      }

      char *arrays[count + 1][i - count]; // array lines bordered as arrays[numberOfPipes+1][numberofArguments-numberOfPipes]

      i = 0;
      int x = 0;
      int y = 0;
      while (args[i] != NULL )
      {
        if (strcmp(args[i], "|") != 0)
        {
          arrays[x][y] = args[i]; //builting arrays that is going to be sent to the each process, each row of the matrix is an array to be sent to another process
          y++;
        }
        else
        {
          x++;
          y = 0;
        }
        i++;
      }

      int h = 0;
      int a = 0;
      int k = 0;
      for (k = 0; k <= count; k++)
      {

        get[k] = -1;
        print[k] = -1;
      }
      //create required number of pipes
      for (a = 0; a < count; a++)
      {
        if (pipe(fd) == -1)
        {
          perror("Pipe failure");
          continue;
        }
        get[a + 1] = fd[0];
        print[a] = fd[1];
      }

      for (k = 0; k <= count; k++)
      {

        pid = fork();

        if (pid < 0)
        {
          printf("fork failed\n");
        }
        else if (pid == 0)
        {
          if (print[k] != -1)
          {

            if (dup2(print[k], 1) == -1)
            {
              perror("dup2 error");
              exit(1);
            }
          }

          if (get[k] != -1)
          {

            if (dup2(get[k], 0) == -1)
            {
              perror("dup2read error");
              exit(1);
            }
          }

          for (h = 0; h <= count; h++)
          {
            close(print[h]);
            close(get[h]);
          }

          if (execvp((const char*) arrays[k][0], arrays[k]) < 1)
          {
            perror("error");
            exit(1);
          }

          exit(0);
        }
        else
        {
          int stat;
          close(print[k]);
          close(get[k]);
          waitpid(pid, &stat, 0);

        }

      }

    } // system command else ends         

  }
}

int main(void)
{
  setup();
  /**
   * After reading user input, the steps are:
   * (1) fork a child process using fork()
   * (2) the child process will invoke execvp()
   * (3) if command included &, parent will invoke wait()
   */
  return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-08 16:54:22

对于NULL-terminate arrays[k],代码似乎遗漏了。使arrays[k]中的最后一个条目携带NULL

更新

代码语言:javascript
复制
  if (execvp((const char*) arrays[k][0], arrays[k]) < 1)

应该是

代码语言:javascript
复制
  if (execvp(arrays[k][0], arrays[k]) == -1)

更直截了当的是:

代码语言:javascript
复制
  execvp(arrays[k][0], arrays[k]);
  perror("execvp() failed");

因为exec*()-family of functions的成员只在出错时返回。

票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20449182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档