I'm building a string here based on 4 arguments and calling it with system(), but seems kinda messy the way i have done it. Is there a more correct way i should have done this and not use all those strcat and str1-4?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char str1[40] = "sed -n 's/.*\\(";
char str2[] = "\\)\\(.*\\)\\(";
char str3[] = "\\).*/\\2/p' ";
char str4[] = " > ";
if (argc != 5)
{
fprintf (stderr, "Usage %s <LogFile> <token1> <token2> <DumpFile>\n",
argv[0]);
exit(EXIT_FAILURE);
}
strcat(str1, argv[2]);
strcat(str1, str2);
strcat(str1, argv[3]);
strcat(str1, str3);
strcat(str1, argv[1]);
strcat(str1, str4);
strcat(str1, argv[4]);
system(str1);
return 0;
}
One problem with your code is that you're not checking the arguments fit in the 40 bytes.
I would probably use snprintf
:
snprintf(str, LENGTH, "sed -n 's/.*\\(%s...", argv[2]...);