I'm trying to understand the parts of the execl command. I'm new to programming in c and not very familiar with linux. I was told to execute a couple of statements using this command, and for the most part I think I understand it.
execl(location here, followed by arguments, terminated by a null pointer)
I was tasked with executing the following using this command:
A process status tree, ps --forest
execl("/bin/ps", "ps", "--forest", (char*) NULL);
This works
The Date and Time, date
execl("/bin/date", "date", 0, (char*) NULL);
This works.
The 'fortune' command
execl("/usr/bin/fortune", "fortune", (char*) NULL);
This doesn't work.
execl("/bin/fortune", "fortune", (char*) NULL);
This doesn't work.
The main issue with the fortune command is that I'm either missing an argument, or I have the wrong location. I'm assuming it's the location because I can execute it with just "fortune" in the command line. I've searched for the correct location, but couldn't find any mention of it.
Edit: Thanks to ojblass the location was verified to '/usr/bin/fortune', meaning my arguments must be incorrect.
long directory listing code
execl("/usr/bin/find", "find", "/", "-name", "date", "-maxdepth", "3", "2>", "/dev/null", (char *)NULL);
This doesn't work.
Edit: Changed to:
execl("/usr/bin/find", "find", "/", "-name", "date", "-maxdepth", "3", "2>/dev/null", (char *)NULL);
This is supposed to find files with the given name in them at a maximum depth of 3 from the root directory, then send all error messages to /dev/null. I'm pretty lost on this command.
For this one, I'm not sure how to approach, but I know I need to take the argument they give, then pass that into the handler I've created. So something like:
`execl("/bin/I don't know this part maybe 'signal'?",cmd[1], (char*) NULL);`
Then pass it into signal(cmd[1], sighandler);
After writing the post, I'd say the part I'm primarily struggling with would be understanding the commands rather than the use of execl().
If you could either correct any place where I messed up, or give an explanation of the command, that would be greatly appreciated!
Thanks!
Edit: The solutions have been posted in the comments.
Try using execlp instead, which searchs the executable in the directories specified by the PATH environment variable:
execlp("fortune","fortune",(char*)NULL);
The first argument of execlp is the name of the executable file. The rest are the arguments which are passed to char *argv[], terminated by a NULL pointer (The first of these is the name of the program).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With