Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Get-system-command-output/C/get-system-command-output.c
Normal file
36
Task/Get-system-command-output/C/get-system-command-output.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) return 1;
|
||||
|
||||
FILE *fd;
|
||||
fd = popen(argv[1], "r");
|
||||
if (!fd) return 1;
|
||||
|
||||
char buffer[256];
|
||||
size_t chread;
|
||||
/* String to store entire command contents in */
|
||||
size_t comalloc = 256;
|
||||
size_t comlen = 0;
|
||||
char *comout = malloc(comalloc);
|
||||
|
||||
/* Use fread so binary data is dealt with correctly */
|
||||
while ((chread = fread(buffer, 1, sizeof(buffer), fd)) != 0) {
|
||||
if (comlen + chread >= comalloc) {
|
||||
comalloc *= 2;
|
||||
comout = realloc(comout, comalloc);
|
||||
}
|
||||
memmove(comout + comlen, buffer, chread);
|
||||
comlen += chread;
|
||||
}
|
||||
|
||||
/* We can now work with the output as we please. Just print
|
||||
* out to confirm output is as expected */
|
||||
fwrite(comout, 1, comlen, stdout);
|
||||
free(comout);
|
||||
pclose(fd);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue