Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a variable to popen command

I need to pass a string variable to a popen command that I made for decripting a piece of encrypted data. The code segment I need to use is:

char a[]="Encrypted data";
popen("openssl aes-256-cbc -d -a -salt <a-which is the data i have to pass here>","r");

What should I do to pass this variable into the command. I tried with:

popen("openssl aes-256-cbc -d -a -salt %s",a,"r");

But on compiling showed error that popen is passed too many arguments. Please help. Thanks in advance. Operating Platform: Linux

like image 873
James Joy Avatar asked Sep 03 '25 14:09

James Joy


1 Answers

Use snprintf to construct the command string passed to popen.

FILE * proc;
char command[70];
char a[]="Encrypted data";
int len;
len = snprintf(command, sizeof(command), "openssl aes-256-cbc -d -a -salt %s",a);
if (if len <= sizeof(command))
{
    proc = popen(command, "r");
}
else
{
    // command buffer too short
}
like image 83
MByD Avatar answered Sep 05 '25 02:09

MByD