简体   繁体   中英

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 . But when i type something like that ls -l /tmp or executing a "c" code located in the path everything is fine. 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. However, your particular question is in how you're calling execvp() .

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

This will always pass two arguments to ls . Even if pr1 and pr2 are empty, ls will still act like it was passed arguments. ls will determine how many arguments it has by looking for a NULL entry.

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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