Skip to content

Instantly share code, notes, and snippets.

@riicchhaarrd
Created May 17, 2019 17:59
Show Gist options
  • Save riicchhaarrd/d334a2279b96053e2926d12d914b9fee to your computer and use it in GitHub Desktop.
Save riicchhaarrd/d334a2279b96053e2926d12d914b9fee to your computer and use it in GitHub Desktop.
pipe example for use with https://github.com/riicchhaarrd/cidscropt
#pragma comment(lib, "/lib/i386-linux-gnu/libc.so.6")
#define STDOUT_FILENO 1
typedef struct
{
int out;
int in;
} filedes_t;
main()
{
filedes = new filedes_t;
if (pipe(filedes) == -1) {
$perror("pipe");
$exit(1);
}
pid = fork();
if (pid == -1) {
$perror("fork");
$exit(1);
} else if(pid == 0)
{
printf("pid = %\n",pid);
dup2(filedes->in, STDOUT_FILENO);
printf("hello world\n");
$shutdown(filedes->out, 1);
close(filedes->in);
close(filedes->out);
$_exit(1); //exit child process
}
close(filedes->in);
buf = buffer(4096);
$memset(buf,0,buf.size);
brk = false;
while(!brk)
{
count = read(filedes->out, buf, buf.size);
if(count == 0)
{
brk = true;
} else
{
$printf("buf=%s,count=%d\n",buf,count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment