“The popen() function will carry out the command given by the command of the string. The function should return a pointer to a stream that is used to read or write to the pipe while also creating a pipe between the calling application and the executed command. The Popen function is available in the standard library of the I/O function and spawns a second process to run a terminal command. The open phase of the popen() is the same as the open phase in the fopen() function. The popen() function initiates a process by forking, constructing a pipe, and executing the shell. Since a pipe is unidirectional by default; as a result, the stream is either read-only or write-only. On the popen() function successful execution, an open stream is obtained that is used for the pipe to read and write.”
Example 1
With the following example program, we will read the files that exist in the current directory or folder using the popen system call. Firstly, we have input the stdio.h header file of the C standard library. Then, we have a program int main()function where we have deployed the popen function. Before this, we established the pointer variable “FileOpen” from the class “FILE”. The pointer variable of the file indicates the succeeding byte to read or write.
After that, we assigned the character’s limit value to be read. The variable “FileOpen” then invoked the “popen” function. The “popen” function takes the “ls -l” command of Linux, which will list all the files of the current directory. Moreover, we have input the “r” parameter in the popen function, which indicates the read mode.
Here, we have piped the files read process by utilizing the popen function. Next, we processed the created pipe with the while loop. The while loop uses the fgets methods, which takes the arguments “line”, “sizeof the line,” and “FileOpen”. The fgets read the piped process and stored it into the “%s” symbol of string. This particular symbol is called inside the printf method along with the “line” argument. Once we have created the pipe processed, with the pclosed function, the piped process can be closed as deployed at the end of the below program.
int main()
{
FILE *FileOpen;
char line[130];
FileOpen = popen("ls -l", "r");
while ( fgets( line, sizeof line, FileOpen))
{
printf("%s", line);
}
pclose(FileOpen);
}
The popen function of the C program forked the above process and then created the pipe. Now, we have executed the processed pipe of the stream in the shell with the C compilation command. The output has listed all the files in the “Home” directory as we have executed the program in that directory.
Example 2
In the previous popen program, we have a simple demonstration of the functionality of the popen program that is used for piping the streaming process of reading files. Now, we have taken another example of the popen function where we have piped the process with the write mode. Let’s consider the program with the main function. We have constructed the file pointer variable inside the main function as “file”. The file pointer is deployed with the popen function.
The popen function takes the “cat” command and the “w” for write mode. Here, each File argument is sequentially read and sent to standard output by the cat command. In the for loop body, we have utilized the fprintf function to print the numerical count values as we have specified the symbol “%d”. Then closed, the popen pipe process with the pclose system call.
int main(int argc, char **argv) {
FILE *file = popen("cat", "w");
int x = 0;
for(x=0;x<5;x++) {
fprintf(file, "My Count = %d\n", x);
}
pclose(file);
return 0;
}
When we executed the above-created process, it printed the count values in the following manner.
Example 3
Now, we have another program that transfers the data of one process to another process. We will do this with the pipe from the popen function. We have implemented the program by using the standard libraries of C. Then, we have an int main function for deploying a program. Here, we have specified the string in the sprintf function with the “buffer” argument. The sprintf() function keeps the result on the char buffer provided by sprintf rather than sending it to the prompt.
After that, we called the popen function inside the “read” variable. There, we have two processes inside the popen function. The “wc -c” is the first process which is used to count the provided characters, and the second process is the “w,” which indicates that the pipe is open in writing mode. After that, we have the “fwrite” function that uses the pipe to write data. The data will be received by the “wc”, then counted the character and displayed in the shell.
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main()
{
FILE *read;
char buffer[50];
sprintf(buffer,"Linux System call");
read=popen("wc -c","w");
fwrite(buffer,sizeof(char),strlen(buffer),read);
pclose(read);
}
The characters displayed in the prompt are “17” because the string we have specified above contains “17” characters. The “wc -c” process reads these characters and prints them as an output.
Example 4
The above example of popen sends the data from one process to another one. Here, we will receive the data from one process to the other process via a pipe. The main function of the program is to construct the buffer, which takes “50” values. Then, we created the variable “r,” where the popen function created the process. The “ls” process is used to list the files of the directory, and the “r” process for reading the data from the pipe. The “ls” process transmits the data to the “r” process to read. After this, we have the fread function, which reads the data and stores the data in the buffer. Then, the print statement will print the data stored in the buffer.
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main()
{
FILE *r;
char buffer[50];
r = popen("ls","r");
fread(buffer, 1, 25, r);
printf("%s\n", buffer);
pclose(r);
}
Here only “50” characters of the existing files are displayed from the working directory. Therefore, the output will only have 50 characters.
Conclusion
We have given a detailed demonstration of Linux popen system calls in C language. We have implemented four examples where we have deployed the popen function. The popen function returns the pipe stream according to the mode we have assigned. In the examples, we have used both the read and write mode with the file handling functions fread and fwrite. We have used the name of the program in the popen() function as its first argument. The second argument is a file “r” as read or “w” as write mode.