File: test1.c

package info (click to toggle)
trueprint 5.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 1,744 kB
  • ctags: 728
  • sloc: ansic: 8,296; sh: 1,225; makefile: 138
file content (117 lines) | stat: -rw-r--r-- 2,452 bytes parent folder | download | duplicates (33)
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
/*
 * Source file:
 *	openpipe.c
 *
 * Contains openpipe - a replacement for the non-POSIX function popen.
 * This returns an int rather than a FILE * to make it general purpose -
 * fdopen can be used outside to convert this to a FILE *
 */

#define _POSIX_SOURCE

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

#ifdef _ultrix
extern int close(int fildes);
extern int dup(int fildes);
extern pid_t fork(void);
extern int pipe(int fildes[2]);
#else
#include <unistd.h>
#endif

#include "trueprint.h"
#include "main.h"
#include "openpipe.h"

int
openpipe(const char *command, char *mode)

{
  int fildes[2];
  int parent_pipe_end;
  int child_pipe_end;
  char *child_pipe_mode;

  if (strcmp(mode, "w") == 0) {
    parent_pipe_end = 1; child_pipe_end = 0;
    child_pipe_mode = "read";
  } else if (strcmp(mode, "r") == 0) {
    parent_pipe_end = 0; child_pipe_end = 1;
    child_pipe_mode = "write";
  } else {
    (void)fprintf(stderr, "%s: Internal error: popen() called with bad mode %s", cmd_name, mode);
    exit(2);
  }

  if (pipe(fildes) != 0) {
    (void)fprintf(stderr, "%s: Cannot create pipe, %s\n", cmd_name, strerror(errno));
    exit(2);
  }

  switch (fork()) {
  case -1:
    /* Error */
    (void)fprintf(stderr, "%s: Cannot fork, %s\n", cmd_name, strerror(errno));
    exit(2);
    /*NOTREACHED*/

  case 0:
    /* Child process */
    if (!((close(fildes[parent_pipe_end]) == 0) &&
	 (close(child_pipe_end) == 0) &&
	 (dup(fildes[child_pipe_end]) == child_pipe_end))) {
      (void)fprintf(stderr,"%s: Cannot redirect %s for child, %s\n", cmd_name, child_pipe_mode, strerror(errno));
      exit(2);
    }

    if (system(command) < 0) {
      (void)fprintf(stderr,"%s: Cannot start pipe, %s\n",cmd_name, strerror(errno));
      exit(2);
    } else exit(0);
    /*NOTREACHED*/

  default:
    /* Parent process */

    if (close(fildes[child_pipe_end]) != 0) {
      (void)fprintf(stderr,"%s: Cannot close %s end of pipe for parent, %s\n",cmd_name, child_pipe_mode, strerror(errno));
      exit(2);
    }

    return fildes[parent_pipe_end];
  }
}

FILE *
fopenpipe(const char *command, char *mode)

{
  int handle;

  handle = openpipe(command, mode);

  return fdopen(handle, mode);
}

void
closepipe(int handle)

{
  int statloc;

  (void)close(handle);
  (void)waitpid(-1, &statloc, WNOHANG);
}

void
fclosepipe(FILE *fp)

{
  closepipe(fileno(fp));
}