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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
$NetBSD: patch-ar,v 1.1 2005/06/14 18:10:37 jlam Exp $
--- /dev/null 2005-06-14 01:17:00.000000000 -0400
+++ lib/s_popen.c 2005-06-14 00:03:23.000000000 -0400
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2004 The X.Org fundation
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is fur-
+ * nished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+ * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the name of the X.Org fundation
+ * shall not be used in advertising or otherwise to promote the sale,
+ * use or other dealings in this Software without prior written
+ * authorization from the X.Org fundation.
+ */
+
+/*
+** This is a secure but NOT 100% compatible replacement for popen()
+** Note: - don't use pclose() use fclose() for closing the returned
+** filedesc.!!!
+**
+** Known Bugs: - unable to use i/o-redirection like > or <
+** Author: - Thomas Biege <thomas@suse.de>
+** Credits: - Andreas Pfaller <a.pfaller@pop.gun.de> for fixing a SEGV when
+** calling strtok()
+*/
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#define __SEC_POPEN_TOKEN " "
+
+FILE *s_popen(char *cmd, const char *type)
+{
+ pid_t pid;
+ int pfd[2];
+ int rpipe = 0, wpipe = 0, i;
+ char **argv;
+ char *ptr;
+ char *cmdcpy;
+
+
+ if(cmd == NULL || cmd == "")
+ return(NULL);
+
+ if(type[0] != 'r' && type[0] != 'w')
+ return(NULL);
+
+ if ((cmdcpy = strdup(cmd)) == NULL)
+ return(NULL);
+
+ argv = NULL;
+ if( (ptr = strtok(cmdcpy, __SEC_POPEN_TOKEN)) == NULL)
+ {
+ free(cmdcpy);
+ return(NULL);
+ }
+
+ for(i = 0;; i++)
+ {
+ if( ( argv = (char **) realloc(argv, (i+1) * sizeof(char *)) ) == NULL)
+ {
+ free(cmdcpy);
+ return(NULL);
+ }
+
+ if( (*(argv+i) = (char *) malloc((strlen(ptr)+1) * sizeof(char))) == NULL)
+ {
+ free(cmdcpy);
+ return(NULL);
+ }
+
+ strcpy(argv[i], ptr);
+
+ if( (ptr = strtok(NULL, __SEC_POPEN_TOKEN)) == NULL)
+ {
+ if( ( argv = (char **) realloc(argv, (i+2) * sizeof(char *))) == NULL)
+ {
+ free(cmdcpy);
+ return(NULL);
+ }
+ argv[i+1] = NULL;
+ break;
+ }
+ }
+
+
+ if(type[0] == 'r')
+ rpipe = 1;
+ else
+ wpipe = 1;
+
+ if (pipe(pfd) < 0)
+ {
+ free(cmdcpy);
+ return(NULL);
+ }
+
+ if((pid = fork()) < 0)
+ {
+ close(pfd[0]);
+ close(pfd[1]);
+ free(cmdcpy);
+ return(NULL);
+ }
+
+ if(pid == 0) /* child */
+ {
+ if((pid = fork()) < 0)
+ {
+ close(pfd[0]);
+ close(pfd[1]);
+ free(cmdcpy);
+ return(NULL);
+ }
+ if(pid > 0)
+ {
+ exit(0); /* child nr. 1 exits */
+ }
+
+ /* child nr. 2 */
+ if(rpipe)
+ {
+ close(pfd[0]); /* close reading end, we don't need it */
+ dup2(STDOUT_FILENO, STDERR_FILENO);
+ if (pfd[1] != STDOUT_FILENO)
+ dup2(pfd[1], STDOUT_FILENO); /* redirect stdout to writing end of pipe */
+ }
+ else
+ {
+ close(pfd[1]); /* close writing end, we don't need it */
+ if (pfd[0] != STDIN_FILENO)
+ dup2(pfd[0], STDIN_FILENO); /* redirect stdin to reading end of pipe */
+ }
+
+ if(strchr(argv[0], '/') == NULL)
+ execvp(argv[0], argv); /* search in $PATH */
+ else
+ execv(argv[0], argv);
+
+ close(pfd[0]);
+ close(pfd[1]);
+ free(cmdcpy);
+ return(NULL); /* exec failed.. ooops! */
+ }
+ else /* parent */
+ {
+ waitpid(pid, NULL, 0); /* wait for child nr. 1 */
+
+ if(rpipe)
+ {
+ close(pfd[1]);
+ free(cmdcpy);
+ return(fdopen(pfd[0], "r"));
+ }
+ else
+ {
+ close(pfd[0]);
+ free(cmdcpy);
+ return(fdopen(pfd[1], "w"));
+ }
+
+ }
+}
+
|