Tak už :)
Zde lze prohlédnout všech příspěvky uživatele. Jsou zde vidět pouze příspěvky z oblastí, do kterých máte přístup.
Nabídka Zobrazit příspěvky#include <cstdio>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// http://snippets.dzone.com/posts/show/1134
pid_t mpopenv(const char *path, char *const argv[], int *infp, int *outfp) {
const int READ = 0;
const int WRITE = 1;
int p_stdin[2], p_stdout[2];
pid_t pid;
if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
return -1;
pid = fork();
if (pid < 0)
return pid;
else if (pid == 0)
{
close(p_stdin[WRITE]);
dup2(p_stdin[READ], READ);
close(p_stdout[READ]);
dup2(p_stdout[WRITE], WRITE);
execv(path, argv);
perror("execl");
exit(1);
}
if (infp == NULL)
close(p_stdin[WRITE]);
else
*infp = p_stdin[WRITE];
if (outfp == NULL)
close(p_stdout[READ]);
else
*outfp = p_stdout[READ];
return pid;
}
int main(int argc, char** argv) {
char *const child_argv[] = {"/bin/ls", "-la", NULL}; // TODO: vyresit: warning: deprecated conversion from string constant to 'char*'
int child_infd, child_outfd;
if (mpopenv(child_argv[0], child_argv, &child_infd, &child_outfd) <= 0) {
exit(1);
}
//write(child_infd, "a", 1);
close(child_infd);
char c;
while (read(child_outfd, &c, 1) == 1) {
fputc(c, stdout);
}
close(child_outfd);
printf("sem uz to nedojde\n"); // TODO: Proc se funkce read zasekne? Cekal bych, ze vrati nulu.
// TODO: waitpid: kam ho dat?
return 0;
}