For some reason pipe() is returning an error when I try to pipe. Any idea why? Here's the code:
heres the program outputCode:// Program asks user for input and tries to execute linux commands based on that input // User can use multiple input piped commands ie ls | grep main | grep o. #define _GNU_SOURCE 1 #include <cstdio> #include <cstdlib> #include <cstring> #include <unistd.h> #include <getopt.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <errno.h> #include <fcntl.h> #include "parsedTokens.h" #include <string> #include <iostream> #include <deque> using namespace std; struct filedesc { int desc[2]; }; int main() { //char *argv[] = {"ls", "ls", 0}; //char *left_arg[] = {"ls", 0}; //char *right_arg[] = {"grep", "main", 0}; //int p[2]; int pid; int len; ParsedTokens left_comm_toks; ParsedTokens right_comm_toks; ParsedTokens command_toks; vector<string> commands; // = {"ls", "grep main", "grep cpp"}; commands.push_back("ls"); commands.push_back("grep main"); commands.push_back("grep cpp"); vector<filedesc> p; //const int size = commands.size(); //int p[size][2]; // int pipe(int filedes[2]); // filedes[0] is for reading, filedes[1] is for writing for(int i = 0; i < commands.size() - 1; i++) { cout << "Pipe" << i << endl; if(pipe(p[i].desc) < 0) { cerr << strerror(errno) << endl; exit(EXIT_FAILURE); } cout << "got completed" << endl; } for(int x = 0; x < commands.size(); x++) { pid = fork(); if(pid < 0) { cerr << strerror(errno) << endl; exit(EXIT_FAILURE); } if(pid == 0) { cout << "exec left command" << endl; char * tempstring; tempstring = new char[commands[x].length() + 1]; strcpy(tempstring, commands[x].c_str()); command_toks.ParseAndAddTokens(tempstring); len = command_toks[0].length(); const char * arg0 = command_toks[0].c_str(); // converts left_command into left_argv[] which execvp uses /*len = left_command.length(); char * const myArr = new char[len + 1]; for(int x = 0; x < len; x++) myArr[x] = left_command[x]; myArr[len] = '\0'; char * const left_argv[] = {myArr,0};*/ delete [] tempstring; vector<string> args; for(int j = 0; j < command_toks.GetNumTokens(); j++) { args.push_back(left_comm_toks[j]); } vector<char *> argv; for(int k = 0; k < args.size(); k++) argv.push_back(const_cast<char *>(args[k].c_str())); argv.push_back('\0'); if(x == 0) { close(p[x].desc[0]); dup2(p[x].desc[1], 1); close(p[x].desc[1]); } else if(x==commands.size()-1) { close(p[x-1].desc[1]); dup2(p[x-1].desc[0],0); close(p[x-1].desc[0]); } else { for(int y = 0; y < commands.size() - 1; y++) { if(y == x-1) close(p[y].desc[1]); else if(y == x) close(p[y].desc[0]); else { close(p[y].desc[0]); close(p[y].desc[1]); } } dup2(p[x-1].desc[0],0); close(p[x-1].desc[0]); dup2(p[x].desc[1],1); close(p[x].desc[1]); } execvp(arg0, &argv[0]); cerr << "Error on exec of " << arg0 << ": " << strerror(errno) << endl; _exit(errno == ENOENT ? 127 : 126); } } for(int z = 0; z < commands.size() - 1; z++) { close(p[z].desc[0]); close(p[z].desc[1]); } for(int zz = 0; zz < commands.size(); zz++) wait(0); }
Code:dell1(22)>./mshell Pipe0 Bad address



LinkBack URL
About LinkBacks


