• C Programming Video Tutorials

Command Execution in C



Sometimes it may be required that you execute a few OS commands through a C program. To execute system commands, you can use the system() function from the "stdlib.h" header file.

With this system() function, you can invoke Linux/Windows terminal commands. The prototype to use this function is as follows −

system(char *command);

Example

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:\Users\user>dir *.c
 Volume in drive C has no label.
 Volume Serial Number is 7EE4-E492

 Directory of C:\Users\user

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 

The exec Family of Functions

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 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!

The 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!

The 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!

The 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!

The 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!

The 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