简体   繁体   English

ls:无法访问:没有这样的文件或目录

[英]ls: cannot access : No such file or directory

When i type ls i get this message twice : ls: cannot access : No such file or directory . 当我键入ls我两次收到此消息: ls: cannot access : No such file or directory But when i type something like that ls -l /tmp or executing a "c" code located in the path everything is fine. 但是,当我键入ls -l /tmp类的东西或执行位于路径中的“ c”代码时,一切都很好。 Any ideas what is going wrong? 任何想法出了什么问题? My code: 我的代码:

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

int main()
{
    for (;;) {
        char *cmd,*splitcmd,*pr0,*pr1,*pr2;
        int i, j, nargc = 0, characters;
        char **cmdArray;
        size_t bufsize = 1024;
        pid_t pid, wpid;
        int status = 0;

        printf("Type a command : \n");

        cmd = (char *) malloc(bufsize * sizeof(char));
        characters = getline(&cmd, &bufsize, stdin);
        // printf("cmd===> %s  characters===>  %d \n",cmd,characters);
        if (cmd[characters-1] == '\n')
        {
            cmd[characters-1] = '\0';
            characters--;
        }
        // printf("cmd===> %s  characters===>  %d \n",cmd,characters);

        cmdArray = (char**) malloc(bufsize * sizeof(char *));
        for (i = 0 ; i < bufsize ; i++)
        {
            cmdArray[i] = (char*) malloc(bufsize*sizeof(char));
        }
        splitcmd = strtok(cmd," ");
        //   printf(" cmd====  %s\n",cmd);
        while ((splitcmd))
        {
            cmdArray[nargc] = splitcmd;
            if (cmdArray[nargc][(strlen(cmdArray[nargc])) - 1] == ' ')
                cmdArray[nargc][(strlen(cmdArray[nargc]))-1] == '\0';
            // printf(" nargc====%d  cmdArray===[  %s  ]                       \n",nargc,cmdArray[nargc]);
            nargc++;
            pr0 = cmdArray[0];
            pr1 = cmdArray[1];
            pr2 = cmdArray[2];

            splitcmd = strtok(NULL," ");
            //printf(" pr0  %s   \n",pr0);
            //printf(" pr1  %s   \n",pr1);
            //printf(" pr2  %s   \n",pr2);
        }

        if ((pid = fork()) == 0)
        {
            char *argv[] = {pr0, pr1, pr2, NULL};
            execvp(argv[0],argv);
            for (int i = 0; i < 100; i++) {
                free(cmdArray[i]);
            }
            free(cmdArray);
        }
        wait(&status);
    }
}

Your code has a number of problems, many of which are identified by turning on warnings. 您的代码有很多问题,其中许多是通过打开警告来识别的。 Use -Weverything if your compiler supports it, or at least -Wall if it does not. 如果编译器支持,则使用-Weverything ,否则不使用-Wall However, your particular question is in how you're calling execvp() . 但是,您的特定问题在于如何调用execvp()

char *argv[] = {pr0, pr1, pr2, NULL};
execvp(argv[0],argv);

This will always pass two arguments to ls . 这将始终将两个参数传递给ls Even if pr1 and pr2 are empty, ls will still act like it was passed arguments. 即使pr1pr2为空, ls仍然会像传递参数一样工作。 ls will determine how many arguments it has by looking for a NULL entry. ls将通过查找NULL条目来确定它具有多少个参数。

Your code has a flaw in that it is trying to hard code the number of arguments by splitting up the cmdArray into individual variables. 您的代码有一个缺陷,那就是它试图通过将cmdArray为单个变量来硬编码参数的数量。 This isn't going to work. 这行不通。 For starters, commands take more than two arguments. 对于初学者,命令采用两个以上的参数。 You should instead leave cmdArray together, properly NULL terminate it, and pass that into execvp . 您应该将cmdArray放在一起,将其正确地NULL终止,然后将其传递到execvp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM