gpt4 book ai didi

c - 扩展 C-Shell 程序和 chdir() 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:04 30 4
gpt4 key购买 nike

我的导师给了我们一个基本的 C shell 来扩展,我目前正在努力让 shell 在用户在命令行中输入“cd [directory]”时更改目录。我已经得到它来停止段错误,但它不会更改目录。谁能告诉我为什么它不起作用?

到目前为止,这是我的代码:

#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] 是你程序名的实现表示,不是你刚刚输入的命令,它可能在 参数。或者 cmd。或者某处。

即使您解决了这个问题,您也不应该在 C 语言中使用 == 进行字符串比较。strcmp 系列之一是执行此操作的正确方法。

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com