Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using execl to run a Linux command

Tags:

c

linux

unix

exec

I need to list all files in the current directory which have a permission of 644 by writing a C language program. I can not use system() and have to use execl() in order to use system calls.

This a line that I used in my code:

execl("/usr/bin/find", "find . -maxdepth 1 -perm 644", (char *)NULL);

The problem is that the code is searching the whole disk instead of the current directory. Would you help me to fix it please?


        ...

        case 4:
            int status;
            switch (fork()){
                case -1: quit ("fork",1);
                case 0:
                execl("/usr/bin/find","find","." ,"-maxdepth" ,"1","-perm", "644",(char *)NULL) ;
                exit (200);
                default:
                wait(&status);
                exit(0);
            }
        }
like image 815
femchi Avatar asked Jan 18 '26 12:01

femchi


1 Answers

Separate the arguments:

execl("/usr/bin/find", "find", ".", "-maxdepth", "1", "-perm", "644", (char *)NULL);

Your invocation was equivalent to invoking the find program with no arguments (and a very funny argv[0]).

like image 63
Jonathan Leffler Avatar answered Jan 21 '26 02:01

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!