简体   繁体   中英

'ls' Command Failed to Execute via C in Linux

This simple program supposed to run the command ls but the output is 1 and 2.Can someone explain to me why ls not working ?

#include <stdio.h>
main()
{
    int pid,stat;
    if((pid=fork())==0)
      {
        execl("/bin/","ls",NULL);
        printf("1");
    exit(1);
      }
    else
      {
         wait(&stat);
         printf("2");
      }
}

Try something like:

execl("/bin/ls", "ls", (char*)NULL);
           ^^^^

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