【问题标题】:How can I implement cd command in my own shell in C? [duplicate]如何在 C 中自己的 shell 中实现 cd 命令? [复制]
【发布时间】:2017-09-30 16:05:11
【问题描述】:

这是我的代码,我是一名西班牙学生,我无法正确解释,但是,我需要在我自己的 C 语言 shell 中实现 cd 命令。 是针对工人阶级的,如果有人可以帮助实现它。 当我将 if 条件放在进程上时,cd 不起作用,但是当我将代码放在外面时,我让 cd 工作,但是,当放回 cd 时,不会像 cd 命令通常那样返回到上一个目录。

如果有任何人可以纠正失败。非常感谢。

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h> 
#include <unistd.h> 
#include <libgen.h> 
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>

#define TAM 1000 

void  parseo(char *line, char **argv);

int main(void) {

    char  cad[TAM];             
    char  *argv[TAM]; 
    char  *gdir;
    char  *dir;
    char  *to;
    char buf[TAM];
    pid_t pid;
    int status;

    while (1) {                   

        printf("user@PC: ");    
        fgets(cad, 1000, stdin);

        // Si encontramos un salto de linea (se pulsa enter)
        if (cad[strlen(cad) - 1] == '\n')                                                           
            cad[strlen(cad) - 1] = '\0';    // lo marcamos como final de sentencia            

        parseo(cad, argv);


        // Exit para salir del shell
        if (!strcmp(argv[0], "exit")) exit(0);  

        if (!strcmp(argv[0], "cd")){

            gdir = getcwd(buf, sizeof(buf));
            dir = strcat(gdir, "/");
            to = strcat(dir, argv[1]);

            chdir(to);
            //printf("Acceso a la carpeta realizado\n");

        }            

        pid = fork();

        if (pid == 0) {   

            if (execvp(*argv, argv) < 0) {
                printf("%s: no se encontró la orden \n", argv[0]);
                exit(1);
            }
        }else {  

            waitpid(pid,&status,0);
        }   

    }
    return 0;
}

void  parseo(char *line, char **argv){

    while (*line != '\0') {       
        while (*line == ' ' || *line == '\t' || *line == '\n')

            *line++ = '\0';     
            *argv++ = line; 

        while (*line != '\0' && *line != ' ' && *line != '\t' && *line != '\n') 
            line++;             
    } 
    *argv = '\0';             // marca de final
}

【问题讨论】:

  • 一旦 fork() 语句,检查 fork() 是否失败是很重要的。插入语句: if(pid == -1){ printf("Can't fork a process.\n");返回 1; }
  • waitpid() 语句属于子代码块。还要检查 waitpid() 返回状态: if(waitpid( ... ) == -1 ){ printf("Error waiting for child process.\n");返回 1;通过这样做,您不需要 else 块代码。
  • 您对 strcat() 的使用可能会导致未定义的 be(havior。使用这种方法更安全: char gdir[100]; strcpy(gdir, "\"); strcat(gdir, argv[ 1]); chdir(gdir); strcat(dir, argv[1]) 可以改变 argv[1] 的值。阅读 strcat() 函数。

标签: c shell fork cd


【解决方案1】:

实际上,它工作正常!唯一的问题是,在调用chdir() 之后,您的代码继续将命令解析为外部可执行文件,但失败了。 exit 没有这个问题,因为它在执行任何其他操作之前就退出了 shell。 :)

将后面的代码移动到else 块中,或者在chdir() 之后添加continue,应该可以修复它。

(顺便说一句:chdir() 之前的getcwd() / strcat() 步骤实际上是不必要的。chdir() 适用于相对路径:例如,如果当前工作目录是 /usr,则可以调用 @ 987654331@进入/usr/bin。)

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2015-01-11
  • 1970-01-01
  • 2018-09-16
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 2010-12-02
  • 1970-01-01
相关资源
最近更新 更多