Category : C Source Code
Archive   : EMXTEST8.ZIP
Filename : PIPE.C

 
Output of file : PIPE.C contained in archive : EMXTEST8.ZIP
/* pipe.c */

#include
#include
#include
#include
#include
#include
#include

static void my_dup2 (int from, int to)
{
int rc;

(void)close (to);
rc = dup (from);
if (rc == -1)
{
perror ("dup");
exit (2);
}
if (rc != to)
{
(void)fputs ("dup failed\n", stderr);
exit (2);
}
(void)close (from);
if (fcntl (to, F_SETFD, 0) != 0)
{
perror ("fcntl");
exit (2);
}
}


static void private (int handle)
{
if (fcntl (handle, F_SETFD, 1) != 0)
{
perror ("fcntl");
exit (2);
}
}


int main (int argc, char *argv[])
{
int ph[2];
int org_stdin, org_stdout, next_stdin, next_stdout;
int i, j;
char *nargv[1024], *p;

if (argc < 2)
{
(void)fputs ("Usage: pipe ...\n", stderr);
return (1);
}
org_stdin = dup (STDIN_FILENO);
if (org_stdin < 0)
{
perror ("dup stdin");
return (2);
}
org_stdout = dup (STDOUT_FILENO);
if (org_stdout < 0)
{
perror ("dup stdout");
return (2);
}
private (STDIN_FILENO);
private (STDOUT_FILENO);
ph[0] = org_stdin;
for (i = 1; i < argc; ++i)
{
next_stdin = ph[0];
if (i < argc-1)
{
if (pipe (ph) != 0)
{
perror ("pipe");
return (2);
}
private (ph[0]);
private (ph[1]);
next_stdout = ph[1];
}
else
next_stdout = org_stdout;
my_dup2 (next_stdin, STDIN_FILENO);
my_dup2 (next_stdout, STDOUT_FILENO);
p = argv[i]; j = 0;
while ((nargv[j++] = strtok (p, " \t")) != NULL)
p = NULL;
if (spawnvp (P_NOWAIT, nargv[0], (const char * const *)nargv) == -1)
{
perror ("spawnlp");
return (2);
}
}
for (i = 1; i < argc; ++i)
(void)wait (NULL);
return (0);
}


  3 Responses to “Category : C Source Code
Archive   : EMXTEST8.ZIP
Filename : PIPE.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/