/* * FILENAME : pipe.c * DESCRIPTION : Functions to implement pipes on NetWare. * Author : Anantha Kesari H Y, Venkat Raghavan S, Srivathsa M * */ #include #include #include #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