Read User Input to Run Exec in C

The exec family has many functions in C. These C functions are basically used to run a system command in a separate process that the main program and print the output.

In this article, I am going talk about the exec family of functions and testify you how to use each 1 of these exec family function in C. So, permit's get started.

C Organisation Functions in Exec Family unit:

The exec office families are defined in the header unistd.h. Then, yous must utilize this header on the C programme where y'all want to use these functions.

The available exec functions forth with their function parameters are given below:

  • int execl(const char *path, const char *arg, …, NULL);
  • int execlp(const char *file, const char *arg, …, Zippo );
  • int execv(const char *path, char *const argv[]);
  • int execvp(const char *file, char *const argv[]);
  • int execle(const char *path, const char *arg, …, Goose egg, char * const envp[] );
  • int execve(const char *file, char *const argv[], char *const envp[]);

Permit's run into what each of these functions exercise and how to use them.

execl() System Function:

In execl() arrangement part takes the path of the executable binary file (i.eastward. /bin/ls) every bit the get-go and second argument. Then, the arguments (i.e. -lh, /home) that you want to pass to the executable followed by Nothing. Then execl() organisation function runs the command and prints the output. If whatsoever error occurs, then execl() returns -i. Otherwise, it returns nothing.

Syntax:

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

An example of the execl() organization function is given beneath:

#include <unistd.h>

int principal( void ) {
char *binaryPath = "/bin/ls" ;
char *arg1 = "-lh" ;
char *arg2 = "/dwelling house" ;

  execl(binaryPath, binaryPath, arg1, arg2, NULL) ;

return 0 ;
}

I ran the ls -lh /home command using execl() system office. As you lot tin see, the correct result is displayed.

execlp() System Function:

execl() does not use the PATH surround variable. So, the full path of the executable file is required to run it with execl(). execlp() uses the PATH environment variable. So, if an executable file or command is available in the PATH, then the command or the filename is enough to run it, the full path is non needed.

Syntax:

int execlp( const char *file, const char *arg,, Zippo ) ;

We tin rewrite the execl() example using the execlp() system function as follows:

#include <unistd.h>

int main( void ) {
char *programName = "ls" ;
char *arg1 = "-lh" ;
char *arg2 = "/domicile" ;

  execlp(programName, programName, arg1, arg2, Cipher) ;

return 0 ;
}

I only passed the command proper noun ls, not the total path /bin/ls. As yous tin can see, I got the same output every bit before.

execv() Organisation Role:

In execl() function, the parameters of the executable file is passed to the function as different arguments. With execv(), you tin laissez passer all the parameters in a Goose egg terminated array argv. The first element of the array should be the path of the executable file. Otherwise, execv() function works just equally execl() part.

Syntax:

int execv( const char *path, char * const argv[ ] ) ;

Nosotros tin rewrite the execl() example equally follows:

#include <unistd.h>

int primary( void ) {
char *binaryPath = "/bin/ls" ;
char *args[ ] = {binaryPath, "-lh" , "/home" , Cipher} ;

  execv(binaryPath, args) ;

return 0 ;
}

As yous tin can run across, I am getting the correct output.

execvp() System Role:

Works the aforementioned way as execv() system office. Simply, the PATH environment variable is used. So, the full path of the executable file is not required simply as in execlp().

Syntax:

int execvp( const char *file, char * const argv[ ] ) ;

We can rewrite the execv() example as follows:

#include <unistd.h>

int chief( void ) {
char *programName = "ls" ;
char *args[ ] = {programName, "-lh" , "/home" , NULL} ;

  execvp(programName, args) ;

render 0 ;
}

Every bit you tin see, the right output is displayed.

execle() System Function:

Works just like execl() but yous can provide your own environment variables forth with it. The environment variables are passed as an array envp. The last element of the envp array should exist Goose egg. All the other elements contain the key-value pairs as string.

Syntax:

int execle( const char *path, const char *arg, ..., NULL, char * const envp[ ] ) ;

An instance of the execle() system function is given beneath:

#include <unistd.h>

int primary( void ) {
char *binaryPath = "/bin/fustigate" ;
char *arg1 = "-c" ;
char *arg2 = "repeat "Visit $HOSTNAME:$PORT from your browser."" ;
char * const env[ ] = { "HOSTNAME=www.linuxhint.com" , "PORT=8080" , NULL} ;

  execle(binaryPath, binaryPath,arg1, arg2, NULL, env) ;

return 0 ;
}

I passed 2 environment variables HOSTNAME and PORT to the execle() function. As yous can see, I can access them from the executable /bin/bash.

execve() System Function:

Just like execle() you can provide your own surround variables along with execve(). You lot tin likewise pass arguments every bit arrays as y'all did in execv().

Syntax:

int execve( const char *file, char * const argv[ ] , char * const envp[ ] ) ;

The execle() example tin can be rewritten as follows:

#include <unistd.h>

int main( void ) {
char *binaryPath = "/bin/bash" ;
char * const args[ ] = {binaryPath, "-c" , "echo "Visit $HOSTNAME:$PORT
from your browser."" , NULL} ;
char * const env[ ] = { "HOSTNAME=world wide web.linuxhint.com" , "PORT=8080" , NULL} ;

  execve(binaryPath, args, env) ;

return 0 ;
}

Equally you can run across, nosotros get the same output as in the execle() example.

So, that's how yous use the exec office family unit in C for arrangement programming in Linux. Cheers for reading this commodity.

About the author

Freelancer & Linux Arrangement Administrator. Too loves Web API evolution with Node.js and JavaScript. I was born in People's republic of bangladesh. I am currently studying Electronics and Communication Applied science at Khulna Academy of Engineering & Applied science (KUET), one of the enervating public engineering universities of Bangladesh.

piersonlitsee1990.blogspot.com

Source: https://linuxhint.com/exec_linux_system_call_c/

0 Response to "Read User Input to Run Exec in C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel