0

我正在 Unix 命令环境中编写 C shell。到目前为止,一切正常,但是以下命令将导致我的代码正确执行,但会以 seg 错误终止

1. ls | ls | ls
2. ls -al | ls -al > output.txt

底线:我相信分段错误来自“XXXX 从这里开始”和“XXXX 从这里结束”之间的行。我也相信这与第一个 while 循环中的 getchar 测试有关,尽管我可能弄错了。如果您编译并运行此代码,您将在输入“ls -al | ls -al > output.txt”时看到所有内容都将执行,然后打印 shhh> AAAAA,然后是 seg 错误。谁能证实或否认我最初的观察?

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>

main()
{
   char *path, *argv[20], buf[80], n, *p;
   int m, status, inword, continu;
   int inFlag, outFlag, bgFlag, k, count, j, pipes, pid, aCount, dirCount,
      hCount;
   int r_tube[2], l_tube[2];
   char *hist;
   char *h1[20] = { 0 };
   aCount = 0;
   dirCount = 0;

   while (1)
   {
      inword = 0;
      p = buf;
      m = 0;
      continu = 0;
      inFlag = outFlag = bgFlag = k = count = j = pipes = pid = 0;
      hCount = 0;
      int loc[20] = { 0 };
      char currD[50];

      printf("\nshhh> ");

      while ((n = getchar()) != '\n' || continu)   //XXXXXXXXX Starts here
      {
         if (n == ' ')
         {
            if (inword)
            {
               inword = 0;
               *p++ = 0;
            }
         }
         else if (n == '\n')
            continu = 0;
         else if (n == '\\' && !inword)
            continu = 1;
         else
         {
            if (!inword)
            {
               inword = 1;
               argv[m++] = p;
               *p++ = n;
            }
            else
               *p++ = n;
         }
      }                                    //XXXXXXXX Ends here

      *p++ = 0;
      argv[m] = 0;

      getcwd(currD, 50);

      while (argv[k] != 0)
      {                         //stores all entered commands into history array
         h1[aCount] = strdup(argv[k]);
         aCount++;
         k++;
      }
      k = 0;

      if ((strcmp(argv[0], "history") == 0) ||
         (strcmp(argv[0], "History") == 0))
      {                         //tests to see if history is asked for
         printf("History: \n");
         for (k = 0; k < aCount; k++)
            printf("%s\n", h1[k]);
      }


      if (strcmp(argv[0], "cd") == 0)
      {                         //tests to see if CD is needed
         if (strcmp(argv[1], "..") == 0)
            chdir(currD);
         else
         {
            while (argv[dirCount] != 0)
            {
               getcwd(currD, 50);
               chdir(argv[dirCount]);
               dirCount++;
            }
         }
      }

      char *outFile = NULL;
      char *inFile = NULL;
      loc[0] = 0;

      while (argv[count] != 0)
      {  //while loop sets the flags for input redirection, 
         // output redirection, background operator, and piping
         if (strcmp(argv[count], "<") == 0)
         {
            inFile = strdup(argv[count + 1]);
            argv[count] = argv[count + 1] = 0;
            inFlag = 1;
         }
         else if (strcmp(argv[count], ">") == 0)
         {
            outFile = strdup(argv[count + 1]);
            argv[count] = argv[count + 1] = 0;
            outFlag = 1;
         }
         else if (strcmp(argv[count], "&") == 0)
         {
            argv[count] = 0;
            bgFlag = 1;
         }
         else if (strcmp(argv[count], "|") == 0)
         {
            argv[count] = 0;
            loc[pipes + 1] = count + 1;
            pipes++;
            printf("LOC0: %d ", loc[0]);
            printf("LOC1: %d ", loc[1]);
            printf("LOC2: %d ", loc[2]);
            printf("DONE WITH IF\n");
         }
         else
            loc[count] = count;

         count++;
      }

      for (k = 0; k <= pipes; k++)
      {                         //actual execution of commands
         printf("IN FOR\n");
         if (k < pipes)
         {
            pipe(r_tube);
            j++;
         }

         pid = fork();          //fork child every time to exec

         if (pid > 0)
         {
            if (j > 0)
            {
               close(l_tube[0]);
               close(l_tube[1]);
            }
            l_tube[0] = r_tube[0];
            l_tube[1] = r_tube[1];
         }
         else if (pid == 0)
         {
            if ((k == 0) && (inFlag == 1))
            {
               int n = open(inFile, O_RDONLY | O_CREAT);
               if (n == -1)
               {
                  printf("Couldn't open inFile!\n");
                  exit(1);
               }
               close(0);
               dup(n);
               close(n);
            }
            else if ((k == pipes) && (outFlag == 1))
            {
               int out = open(outFile, O_WRONLY | O_CREAT, 0666);
               if (out < 0)
               {
                  printf("Could'nt open outFile!\n");
                  exit(1);
               }
               close(1);
               dup(out);
               close(out);
            }
            printf("K: %d ", k);
            printf("PIPES: %d ", pipes);
            printf("PID: %d\n", pid);
            execvp(argv[loc[k]], &argv[loc[k]]);
         }
         printf("ONE\n");
         if (bgFlag == 0)
            wait(NULL);
      }
      printf("TWO\n");
      if (strcmp(argv[0], "quit") == 0)
         exit(0);               //tests for exit/quit to end program
      if (strcmp(argv[0], "exit") == 0)
         exit(0);
      printf("THREE\n");
      for (k = 0; k < 20; k++)  //reset all of argv to NULL
         argv[k] = 0;
      printf("FOUR\n");

      wait(&status);
   }
}
4

1 回答 1

1

处理分段错误的最简单方法是分析保存到内核生成的 coredump 中的堆栈跟踪。大多数发行版都禁用了 coredump 保存,因此可以启用它

ulimit -c unlimited

Proc 文件系统有几个文件来配置 corudump 生产。最重要的是核心模式 /proc/sys/kernel/core_pattern,它指定核心转储名称和可选的后处理命令。

当进程接收到分段错误信号时,内核会将其状态保存在 coredump 文件中,以后可以使用dgb检查该文件:

gdb -c core ./binary_which_produced_the_core
bt

这将打印出程序在发生分段错误时所做的事情的回溯。

例如,对于您的程序

Program terminated with signal 11, Segmentation fault.
#0  main () at main.c:58
58                 *p++ = n;
(gdb) bt
#0  main () at main.c:58
(gdb) 

注意,我必须添加缺少的标题,所以行号是 +2。

请使用-Wall -Wextra编译器选项报告所有警告并始终修复它们。例如,您有未初始化的 l_tube 数组。

于 2012-10-27T06:21:49.027 回答