Reputation: 889
I want to run execlp() from C file and write the result to some output file. I use the line:
buff = "./cgi-bin/smth";
execlp(buff, buff, "> /cgi-bin/tmp", NULL);
where smth is a compiled c script. But smth prints to stdout, and no file appears. What happens, and how to put script result to an output file?
Upvotes: 1
Views: 231
Reputation: 26647
You have to handle it yourself with dup2
if using execlp
. You can look at how I handle file out with execvp
in comparison. I pass a flag for out redirection and then I handle it:
if (structpipeline->option[0] == 1) { /* output redirection */
int length = structpipeline[i].size;
char *filename = structpipeline->data[length - 1];
for (int k = length - 2; k < length; k++)
structpipeline->data[k] = '\0';
fd[1] = open(filename, O_WRONLY | O_CREAT, 0666);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
} /* TODO: input redirection */
execvp(structpipeline[i].data[0], structpipeline[i].data);
See also this question Redirecting exec output to a buffer or file
Upvotes: 1