File: stdinxout.c

package info (click to toggle)
qmtpssh 0.1
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 76 kB
  • ctags: 24
  • sloc: ansic: 96; makefile: 66; sh: 33
file content (121 lines) | stat: -rw-r--r-- 3,593 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 *   stdinxout - cross-connect two programs' stdin and stdout
 *   Copyright (C) 1999 Tommi Virtanen <tv@havoc.fi>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>

#define PROGNAME "stdinxout"
#define USAGE "usage: " PROGNAME " prog1 [args1..] -- prog2 [args2..]"
#define READ 0
#define WRITE 1

static void /*@exits@*/ fail(char *s) {
  (void) fprintf(stderr,"%s: failure: %s\n",PROGNAME,s);
  exit(100);
}

static void /*@exits@*/ defer(char *s) {
  (void) fprintf(stderr,"%s: deferral: %s\n",PROGNAME,s);
  exit(111);
}

static void /*@exits@*/ usage() {
  (void) fprintf(stderr, "%s\n", USAGE);
  exit(111);
}

int main(int argc, char **argv) {
  pid_t pid_prog1, pid_prog2, pid;
  int to1[2] = {-1, -1};
  int to2[2] = {-1, -1};
  int separator=1;
  if (argc<4)
    usage();
  while ((argv[separator][0]!='-' && argv[separator][0]!='\0')
        || (argv[separator][1]!='-' && argv[separator][0]!='\0')
        || argv[separator][2]!='\0') {
    separator++;
    if (separator>=argc) {
      fail(USAGE);
    }
  }
  if (separator<2) {
    fail(USAGE);
  }

  if (pipe(to1)==-1) { defer("pipe to prog1 failed."); }
  if (pipe(to2)==-1) { defer("pipe to prog2 failed."); }
  pid_prog1=fork();
  if (pid_prog1==-1) { defer("fork failed."); }
  if (pid_prog1==0) { /* child */
    (void) close(to1[WRITE]);
    (void) close(to2[READ]);
    if (dup2(to1[READ],0)==-1)
      defer("dup2 failed on stdin for prog1.");
    if (dup2(to2[WRITE],1)==-1)
      defer("dup2 failed on stdout for prog1.");
    argv[separator]=NULL;
    (void) execvp(argv[1], argv+1);
    defer("exec prog1 failed");
  }
  else { /* parent */
    pid_prog2=fork();
    if (pid_prog2==-1) { defer("fork failed."); }
    if (pid_prog2==0) { /* child */
      (void) close(to2[WRITE]);
      (void) close(to1[READ]);
      if (dup2(to2[READ],0)==-1)
        defer("dup2 failed on stdin for prog2.");
      if (dup2(to1[WRITE],1)==-1)
        defer("dup2 failed on stdout for prog2.");
      (void) execvp(argv[separator+1], argv+separator+1);
      defer("exec prog2 failed");
    }
    else { /* parent */
      pid=wait(NULL);
      if (pid==-1) {
        defer("fail in first wait");
      }
      else if (pid==pid_prog1) {
        if (close(to2[WRITE])!=0)
          defer("cannot close pipe to program 2");
      }
      else if (pid==pid_prog2) {
        if (close(to1[WRITE])!=0)
          defer("cannot close pipe to program 1");
      }
      pid=wait(NULL);
      if (pid==-1) {
        defer("fail in second wait");
      }
      else if (pid==pid_prog1) {
        if (close(to2[WRITE])!=0)
          defer("cannot close pipe to program 2");
      }
      else if (pid==pid_prog2) {
        if (close(to1[WRITE])!=0)
          defer("cannot close pipe to program 1");
      }
      exit(0);
    }
  }
  /*@notreached@*/ exit(1);
}