summaryrefslogtreecommitdiff
path: root/netware/pipe.c
blob: c302fdf3e0a9c899429a49483c5000819865d5d6 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * FILENAME		:	pipe.c
 * DESCRIPTION	:	Functions to implement pipes on NetWare.
 * Author		:	Anantha Kesari H Y, Venkat Raghavan S, Srivathsa M
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "netware/pipe.h"
#include "netware/mktemp.h"


/* Following definitions unavailable in LibC, hence borrowed from CLib */
#define P_WAIT      0
#define P_NOWAIT    1

#define WHITESPACE  " \t"
#define MAX_ARGS    10


FILE* popen(const char* commandline, const char* mode)
{
	int               err, count;
	char              pszPipestr[32] = {'\0'};
	char *command = NULL, *argv[MAX_ARGS] = {'\0'};
	int fd = -1;
	fd_set myfds;
	wiring_t wiring;
	pid_t pid=0;
	FILE *fp=NULL;
	char *ptr = NULL;
	int ptrLen = 0, argc = 0, i = 0;


	/* Get a temporary name */
	(void) tmpnam(strecpy(pszPipestr,  "PHP/php$pipe/"));

	wiring.infd=FD_UNUSED;
	wiring.outfd=FD_UNUSED;
	wiring.errfd=FD_UNUSED;

	/* Open a pipe */
	if ( *mode=='r') {
		fd = pipe_open (pszPipestr, O_RDONLY);
		if (fd == -1)
			return NULL;

		wiring.outfd=fd;
	} else if (*mode=='w') {
		fd = pipe_open (pszPipestr, O_WRONLY);
		if (fd == -1)
			return NULL;

		wiring.infd=fd;
	} else {
		consoleprintf ("Unsupported pipe open mode \n");
		return NULL;
	}

	/* Get the file pointer */
	fp = fdopen(fd, mode);
	if (fp == NULL) {
		consoleprintf ("Failure in fdopen \n");
		close (fd);
		return NULL;
	}

	/* Separate commandline string into words */
	ptr = strtok((char*)commandline, WHITESPACE);
	ptrLen = strlen(ptr);

	/* Get the command */
	command = (char*)malloc(ptrLen + 1);
	if(command == NULL) {
		consoleprintf ("Failure in memory allocation \n");
		close (fd);
		fclose (fp);
		return NULL;
	}
	strcpy (command, ptr);

	/* Command as the first argument into prcessve */
	argv[argc] = (char*)malloc(ptrLen + 1);
	if(argv[argc] == NULL) {
		consoleprintf ("Failure in memory allocation \n");
		close (fd);
		fclose (fp);
		if(command) {
			free(command);
			command = NULL;
		}
		return NULL;
	}
	strcpy (argv[argc], ptr);
	argc++;

	/* Get more arguments if any to be passed to prcessve */
	ptr = strtok(NULL, WHITESPACE);
	while (ptr && (argc < MAX_ARGS))
	{
		ptrLen = strlen(ptr);

		argv[argc] = (char*)malloc(ptrLen + 1);
		if(argv[argc] == NULL) {
			consoleprintf ("Failure in memory allocation \n");
			close (fd);
			fclose (fp);
			if(command) {
				free(command);
				command = NULL;
			}
			return NULL;
		}
		strcpy (argv[argc], ptr);
		argc++;

		ptr = strtok(NULL, WHITESPACE);
	}
	argv[argc] = NULL;

	FD_ZERO(&myfds);
	FD_SET(fd, &myfds);

	pid = processve(command, PROC_CURRENT_SPACE, NULL, &wiring,
					&myfds, NULL, (const char **)argv );
	if (pid == -1) {
		consoleprintf ("Failure in processve call \n");
		close (fd);
		fclose(fp);
		if(command) {
			free(command);
			command = NULL;
		}
		for(i=0; i<argc; i++) {
			if(argv[i]) {
				free(argv[i]);
				argv[i] = NULL;
			}
		}
		return NULL;
	}

	close (fd);
	if(command) {
		free(command);
		command = NULL;
	}
	for(i=0; i<argc; i++) {
		if(argv[i]) {
			free(argv[i]);
			argv[i] = NULL;
		}
	}

	return fp;
}


int pclose(FILE* stream)
{
	fclose(stream);
	return 0;
}



#if 0

FILE* popen2(const char* commandline, const char* mode)
{
    int               err, count, var, replaced;
    char              **env, pszPipestr[32];
    NXVmId_t          newVM;
    NXExecEnvSpec_t   envSpec;
    char *command = NULL, *argv[MAX_ARGS] = {"\0"};
    NXHandle_t fd;
    
    fd = (NXHandle_t) -1;
    
    count = NXGetEnvCount() + 1;  // (add one for NULL)
    env   = (char **) NXMemAlloc(sizeof(char *) * count, 0);
    
    if (!env)
        return 0;
    
    err = NXCopyEnv(env, count);  // ensure NULL added at end
    if ( *mode='r'){
        (void) tmpnam(strecpy(pszPipestr,  "PHPPIPE/stdin/"));
        err = NXFifoOpen(0, pszPipestr, NX_O_RDONLY, 0, &fd);
        if (err)
            return 0;
        
        envSpec.esStdin.ssType     = NX_OBJ_CONSOLE;
        envSpec.esStdout.ssType    = NX_OBJ_FIFO;
        envSpec.esStdout.ssPath    = pszPipestr;
    }
    else if (*mode='w') then{
        (void) tmpnam(strecpy(pszPipestr, "PHPPIPE/stdout/"));
        err = NXFifoOpen(0, instr, NX_O_WRONLY, 0, &fd);
        if (err)
            return 0;
        envSpec.esStdin.ssType     = NX_OBJ_FIFO;
        envSpec.esStdout.ssType    = NX_OBJ_CONSOLE;
		envSpec.esStdin.ssPath     =  pszPipestr;
    }
    else
        consoleprintf ("Unsupported pipe open mode \n");
    
    
    /* Separate commandline string into words */
    
    ptr = strtok((char*)commandline, WHITESPACE);
    ptrLen = strlen(ptr);
    
    command = (char*)malloc(ptrLen + 1);
    strcpy (command, ptr);
    
    ptr = strtok(NULL, WHITESPACE);
    while (ptr && (argc < MAX_ARGS))
    {
        ptrLen = strlen(ptr);
        
        argv[argc] = (char*)malloc(ptrLen + 1);
        strcpy (argv[argc], ptr);
        
        argc++;
        
        ptr = strtok(NULL, WHITESPACE);
    }
/*    consoleprintf ("PHP | popen: command = %s\n", command); */
    
	//

    envSpec.esArgc             = argc;
    envSpec.esArgv             = argv;
    envSpec.esEnv              = env;
    
    envSpec.esStderr.ssType    = NX_OBJ_CONSOLE;
    
    envSpec.esStdin.ssHandle   =
        envSpec.esStdout.ssHandle  =
        envSpec.esStderr.ssHandle  = -1;
    
    envSpec.esStdin.ssPathCtx  =
        envSpec.esStdout.ssPathCtx =
        envSpec.esStderr.ssPathCtx = NULL;
    
    envSpec.esStderr.ssPath    = NULL;
    
    err = NXVmSpawn(NULL, command, &envSpec, NX_VM_CREATE_DETACHED, &newVM);
    return fd;
    
}

int pclose2(FILE* stream)
{
	NXClose(*(NXHandle_t *)stream);
    return 0;
}

FILE* popen1(const char* commandline, const char* mode)
{
    char *command = NULL, *argv[MAX_ARGS] = {"\0"};
	char *tempName = "phpXXXXXX.tmp";
    char *filePath = NULL;
    FILE *fp = NULL;
    char *ptr = NULL;
    int ptrLen = 0, argc = 0, i = 0;

    /* Check for validity of input parameters */
    if (!commandline || !mode)
        return NULL;

    /* Get temporary file name */
    filePath = mktemp(tempName);
	if (!filePath)
		return NULL;

    /* Separate commandline string into words */

    ptr = strtok((char*)commandline, WHITESPACE);
    ptrLen = strlen(ptr);

    command = (char*)malloc(ptrLen + 1);
    strcpy (command, ptr);

    ptr = strtok(NULL, WHITESPACE);
    while (ptr && (argc < MAX_ARGS))
    {
        ptrLen = strlen(ptr);

        argv[argc] = (char*)malloc(ptrLen + 1);
        strcpy (argv[argc], ptr);

        argc++;

        ptr = strtok(NULL, WHITESPACE);
    }
/*consoleprintf ("PHP | popen: command = %s\n", command);*/
    if(strchr(mode,'r') != 0)
    {
        /*spawnvp(P_WAIT, command, argv);*/

        fp = fopen(filePath, "r"); /* Get the file handle of the pipe */
    }
	else if(strchr(mode,'w') != 0)
        fp = fopen(filePath, "w"); /* Get the file handle of the pipe */

    /* Free resources */
    free (command);
    for (i = 0; i < argc; i++)
    {
/*consoleprintf ("%s\t", argv[i]);*/
        free (argv[i]);
    }
/*consoleprintf ("\n");*/

	return fp;  /* Return the file handle.*/
}

int pclose1(FILE* stream)
{
    return (fclose(stream));
}

# endif