2014-11-05 93 views
0

我的教师给了我们一个基本的C语言shell来扩展,并且我正在努力让shell在用户输入' cd [目录]'进入命令行。我已经得到它来停止seg错误,但它不会改变目录。谁能告诉我为什么它不起作用?C-Shell程序和chdir()不能正常工作

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

#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; 
} 

回答

4

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

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

我想你会发现,argv[0]是你的程序的名字执行的代表, 不是您刚输入的命令,可能是args。或cmd。或者在某个地方。

即使您修复该问题,您也不应该在C中使用==进行字符串比较。其中一个strcmp系列是正确的方法。

+0

谢谢你的回应。我会接受你的建议,并让你知道它是如何发展的。 – GamerDJX 2014-11-05 03:13:58

+0

好的,我编辑了我的程序中的if语句。这更接近正确的答案吗? 'if(strcmp(cmd,“cd”)== 0){chdir(directory); }' – GamerDJX 2014-11-05 03:30:49