Question

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.

  • Code to install a signalhandler as code used to catch the signal.

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.

Was it helpful?

Solution

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

OTHER TIPS

To recap:

  • A process status tree

    execl("/bin/ps", "ps", "--forest", (char*) NULL);

  • The Date and Time

    execl("/bin/date", "date", 0, (char*) NULL);

  • The "fortune" command

    execl("/usr/bin/fortune", "fortune", (char*) NULL);

It turns out that I was parsing the case for 'F' incorrectly in my shell. This does indeed work.

  • Long directory listing code

    if (strlen(cmd)>2) { cmd[strlen(cmd)-1]='\0'; execl("/bin/ls","ls", "-l",(char *)&cmd[2],(char *) NULL); } else execl("/bin/ls","ls","-l",(char *) NULL);

  • Code to install a signal handler based on the int

    signum = atoi((char *)&cmd[1]);

    signal(signum,sighandler);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top