1p2open(3GEN) String Pattern-Matching Library Functions p2open(3GEN)
2
3
4
6 p2open, p2close - open, close pipes to and from a command
7
9 cc [ flag ... ] file ... -lgen [ library ... ]
10 #include <libgen.h>
11
12 int p2open(const char *cmd, FILE *fp[2]);
13
14
15 int p2close(FILE *fp[2]);
16
17
19 The p2open()gfunction forks and execs a shell running the command line
20 pointed to by cmd. On return, fp[0] points to a FILE pointer to write
21 the command's standard input and fp[1] points to a FILE pointer to read
22 from the command's standard output. In this way the program has con‐
23 trol over the input and output of the command.
24
25
26 The function returns 0 if successful; otherwise, it returns −1.
27
28
29 The p2close() function is used to close the file pointers that p2open()
30 opened. It waits for the process to terminate and returns the process
31 status. It returns 0 if successful; otherwise, it returns −1.
32
34 A common problem is having too few file descriptors. The p2close()
35 function returns −1 if the two file pointers are not from the same
36 p2open().
37
39 Example 1 Example of file descriptors.
40
41 #include <stdio.h>
42 #include <libgen.h>
43
44 main(argc,argv)
45 int argc;
46 char **argv;
47 {
48 FILE *fp[2];
49 pid_t pid;
50 char buf[16];
51
52 pid=p2open("/usr/bin/cat", fp);
53 if ( pid == −1 ) {
54 fprintf(stderr, "p2open failed\n");
55 exit(1);
56 }
57 write(fileno(fp[0]),"This is a test\n", 16);
58 if(read(fileno(fp[1]), buf, 16) <=0)
59 fprintf(stderr, "p2open failed\n");
60 else
61 write(1, buf, 16);
62 (void)p2close(fp);
63 }
64
65
67 See attributes(5) for descriptions of the following attributes:
68
69
70
71
72 ┌─────────────────────────────┬─────────────────────────────┐
73 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
74 ├─────────────────────────────┼─────────────────────────────┤
75 │MT-Level │Unsafe │
76 └─────────────────────────────┴─────────────────────────────┘
77
79 fclose(3C), popen(3C), setbuf(3C), attributes(5)
80
82 Buffered writes on fp[0] can make it appear that the command is not
83 listening. Judiciously placed fflush() calls or unbuffering fp[0] can
84 be a big help; see fclose(3C).
85
86
87 Many commands use buffered output when connected to a pipe. That, too,
88 can make it appear as if things are not working.
89
90
91 Usage is not the same as for popen(), although it is closely related.
92
93
94
95SunOS 5.11 29 Dec 1996 p2open(3GEN)