我的老师给了我们一个C语言的基本shell来扩展,我目前正在努力让shell在用户在命令行中输入“cd[目录]”时更改目录。我已经让它停止seg错误,但它不会改变目录。有人能告诉我为什么不起作用吗?
这是我目前的代码:

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

/* Array holds arguments: args[0] is the command. */
static char *args[512];
pid_t pid;
int command_pipe[2];

#define READ  0
#define WRITE 1


int chdir(const char* path);



static int
command (int input, int first, int last)
{
  int pipettes[2];

  /* Invoke pipe */
  pipe (pipettes);
  pid = fork ();

  if (pid == 0)
    {
      if (first == 1 && last == 0 && input == 0)
    {
      // 1st cmd
      dup2 (pipettes[WRITE], STDOUT_FILENO);
    }
      else if (first == 0 && last == 0 && input != 0)
    {
      // Mid cmd
      dup2 (input, STDIN_FILENO);
      dup2 (pipettes[WRITE], STDOUT_FILENO);
    }
      else
    {
      // Last cmd
      dup2 (input, STDIN_FILENO);
    }

      if (execvp (args[0], args) == -1)
    _exit (EXIT_FAILURE);   // If child fails
    }

  if (input != 0)
    close (input);

  close (pipettes[WRITE]);

  // If last command, nothing more needs to be read
  if (last == 1)
    close (pipettes[READ]);

  return pipettes[READ];
}

static void
cleanup (int n)
{
  int i;
  for (i = 0; i < n; ++i)
    wait (NULL);
}

static int go (char *cmd, int input, int first, int last);
static char line[1024];
static int n = 0;

int
main (int argc, char* argv[])
{
  while (1)
    {
      /* Initial Prompt */
      printf ("?> ");
      fflush (NULL);

      /* Read in command */
      if (!fgets (line, 1024, stdin))
    return 0;



      int input = 0;
      int first = 1;

      char *cmd = line;
      char *next = strchr (cmd, '|');   /* Find initial '|' */
      char *also = strchr (cmd, ';');   /* Find initial ';' */
      char *directory = argv[1];

      while (next != NULL)
    {
      /* 'next' points to '|' */
      *next = '\0';
      input = go (cmd, input, first, 0);

      cmd = next + 1;
      next = strchr (cmd, '|'); /* Find next '|' */
      first = 0;
    }

      if(argv[0] == "cd"){
        chdir(directory);
    }

      input = go (cmd, input, first, 1);
      cleanup (n);
      n = 0;
    }
  return 0;
}

static char *
skip_white_space (char *s)
{
  while (isspace (*s))
    ++s;
  return s;
}

static void
parse (char *cmd)
{
  cmd = skip_white_space (cmd);
  char *next = strchr (cmd, ' ');
  int i = 0;

  while (next != NULL)
    {
      next[0] = '\0';
      args[i] = cmd;
      ++i;
      cmd = skip_white_space (next + 1);
      next = strchr (cmd, ' ');
    }

  if (cmd[0] != '\0')
    {
      args[i] = cmd;
      next = strchr (cmd, '\n');
      next[0] = '\0';
      ++i;
    }

  args[i] = NULL;
}


static int
go (char *cmd, int input, int first, int last)
{
  parse (cmd);
  if (args[0] != NULL)
    {
      if (strcmp (args[0], "exit") == 0)
    exit (0);
      n += 1;
      return command (input, first, last);
    }
  return 0;
}

最佳答案

你眼前的问题似乎就在这里:

if(argv[0] == "cd"){
    chdir(directory);

我想您会发现argv[0]是程序名的实现表示,而不是您刚刚输入的命令,它可能在args中。或cmd。或者别的什么地方。
即使解决了这个问题,也不应该在C中使用==进行字符串比较。

关于c - 在C-Shell程序和chdir()上扩展无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26748914/

10-16 20:27