C – Command Execution


Command Execution in C



”;


Command Execution in C

Command execution in C is used to execute the system command using the C program. The system commands are executed by using the system() function which is a library function of stdlib.h header file.

By using the system() function, you can execute the Windows/Linux terminal commands inside a C program.


Syntax

The following is the syntax to execute system commands −


system(char *command);

Example of Command Execution

The following code shows the execution of ls command using system() function in C language.


#include <stdio.h>
#include<stdlib.h>
#include<string.h>

int main() {

   char cmd[10];
   strcpy(cmd,"dir C:\users\user\*.c");
   system(cmd);
   
   return 0;
}

Output

Run the code and check its output −


C:Usersuser>dir *.c
 Volume in drive C has no label.
 Volume Serial Number is 7EE4-E492

 Directory of C:Usersuser

04/01/2024  01:30 PM               104 add.c
04/02/2024  01:37 PM               159 add1.c
04/02/2024  01:37 PM               259 array.c
04/02/2024  01:37 PM               149 main.c
04/02/2024  01:37 PM               180 recursion.c
04/02/2024  01:37 PM               241 struct.c
04/02/2024  01:37 PM               172 voidptr.c
               7 File(s)           1,264 bytes
               0 Dir(s)  139,073,761,280 bytes 

exec Family of Functions in C

The exec family of functions have been introduced in the “unistd.h” header file. These functions are used to execute a file, and they replace the current process image with a new process image once they are called.

The following are the functions of exec family in C −

  1. execl() Function
  2. execlp() Function
  3. execle() Function
  4. execv() Function
  5. execvp() Function
  6. execve() Function

1. execl() Function

The execl() function”s first argument is the executable file as its first argument. The next arguments will be available to the file when it”s executed. The last argument has to be NULL.


int execl(const char *pathname, const char *arg, ..., NULL)

Example

Take a look at the following example −


#include <unistd.h>

int main(void) {

   char *file = "/usr/bin/echo";
   char *arg1 = "Hello world!";

   execl(file, file, arg1, NULL);

   return 0;
}

The echo command in Linux is being invoked through the C code.

Output

Save, compile, and execute the above program −


$ gcc hello.c -o hello
$ ./hello
Hello world!

2. execlp() Function

The execlp() function is similar to the execl() function. It uses the PATH environment variable to locate the file. Hence, the path to the executable file needn”t be given.


int execlp(const char *file, const char *arg, ..., NULL)

Example

Take a look at the following example −


#include <unistd.h>

int main(void) {

   char *file = "echo";
   char *arg1 = "Hello world!";

   execlp(file, file, arg1, NULL);
   
   return 0;
}

Output

Here, echo is already located in the PATH environment variable. Save, compile and run from the terminal.


$ gcc hello.c -o hello
$ ./hello
Hello world!

3. execle() Function

In the execle() function, we can pass environment variables to the function, and it”ll use them. Its prototype is like this −


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

Example

Take a look at the following example −


#include <unistd.h>

int main(void) {

   char *file = "/usr/bin/bash";
   char *arg1 = "-c";
   char *arg2 = "echo $ENV1 $ENV2!";
   char *const env[] = {"ENV1 = Hello", "ENV2 = World", NULL};

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

   return 0;
}

Output

Save, compile, and run from the terminal −


$ gcc hello.c -o hello
$ ./hello
Hello world!

4. execv() Function

The execv() function receives a vector of arguments that will be available to the executable file. In addition, the last element of the vector has to be NULL:


int execv(const char *pathname, char *const argv[])

Example

Take a look at the following example −


#include <unistd.h>

int main(void) {

   char *file = "/usr/bin/echo";
   char *const args[] = {"/usr/bin/echo", "Hello world!", NULL};

   execv(file, args);

   return 0;
}

Output

Save, compile, and execute the above program −


$ gcc hello.c -o hello
$ ./hello
Hello world!

5. execvp() Function

The execvp() has the following syntax −


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

Example

Take a look at the following example −


#include <unistd.h>

int main(void) {

   char *file = "echo";
   char *const args[] = {"/usr/bin/echo", "Hello world!", NULL};

   execvp(file, args);

   return 0;
}

Output

Save, compile, and execute the above program −


$ gcc hello.c -o hello
$ ./hello
Hello world!

6. execve() Function

In addition to environment variables, we can pass other arguments to execve() function as a NULL-terminated vector −


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

Example

Take a look at the following example −


#include <unistd.h>

int main(void) {

   char *file = "/usr/bin/bash";
   char *const args[] = {"/usr/bin/bash", "-c", "echo Hello $ENV!", NULL};
   char *const env[] = {"ENV=World", NULL};

   execve(file, args, env);

   return 0;
}

Output

Save, compile, and execute the above program −


$ gcc hello.c -o hello
$ ./hello
Hello world!

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *