• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to use execl (example included)

by admin

execl is one of the families of exec calls that act as a front end to the execve. The following screenshot refers to man execl:

using execl

The arguments for these seven exec functions are difficult to remember. The letters in the function names help somewhat. The letter p means that the function takes a filename argument and uses the PATH environment variable to find the executable file. The letter l means that the function takes a list of arguments and is mutually exclusive with the letter v, which means that it takes an argv[] vector. Finally, the letter e means that the function takes an envp[] array instead of using the current environment.

Differences among the seven exec functions

In the post “Using execve” we saw how it can be used to launch a new process and also pass arguments to it. execl also launches a new process replacing the current one. The syntax of execl is:

int execl(const char *path, const char *arg, ...);

Arguments:
path: Path to the executable that needs to he executed by execl.
arg…: Series of pointers to the arguments that need be passed to executable.

In execve we had to pass an array of pointers as arguments, but in execl we can directly pass the pointers as arguments. These arguments should be NULL terminated.

Example

1. Let us write a simple program to print the arguments passed to it.

# vi hello.c
#include <stdio.h>
main(int argc,char *argv[],char *envp[]){

printf("Filename: %s\n",argv[0]);
printf("%s %s\n",argv[1],argv[2]);
}

2. By convention the first argument should always be the filename and we will be following the same. Let us compile this and name the executable “hello”

# cc hello.c -o hello

3. Now let us write a program to run the executable “hello” using execl.

# vi execl.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main() {
char *temp,*temp1,*temp2;
temp="hello";  //filename
temp1="Funny"; 
temp2="world";

execl("hello",temp,temp1,temp2,NULL);
printf("Error");
}

4. Compile the code and execute it:

# cc execl.c -o execl
./execl 

Output:

Filename: hello
Funny world

Thus the program could successfully run the executable “hello” and also pass the arguments to it. Also, note that execl did not return to the calling function, else it would have printed the “Error” statement after the call to execl.

Filed Under: C, Linux

Some more articles you might also be interested in …

  1. modprobe Command Examples in Linux
  2. How to allow or deny telnet login to specific users only in CentOS/RHEL
  3. Connection using SSH to a Host Not in DNS/hosts Stalls for Some Time at Connection Initiation
  4. mkfs.vfat Command Examples in Linux
  5. How to Create Disk Partitions using cfdisk
  6. rsync: command not found
  7. a2dissite Command Examples in Linux
  8. extrace: command not found
  9. CentOS / RHEL : How to Set up SFTP to Chroot Jail only for Specific Group
  10. TCP Wrapper (hosts.allow & hosts.deny) Command Options in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright