/* David Rosas djr8@po.cwru.edu Reciation leader: Bill Coates Assignment #2 Program #2b, P1: use named pipes to pass variables between programs Compiler : gcc Compiled from command line on Sun ultra5 (gulam.cwru.edu) gcc -o p1.out p1.c -Wall */ /* Include files */ #include #include #include #include #include #include #include #include #include #include /* program must do the following things: --read in values from the pipe (PP0) created in session p0.c --use printf/fprintf/snprintf with each format string --provide a var of an appropriate type to output --output will be sent to PP1 */ int main() { int received; //holds number of bytes received from pipe const int BUFFERSIZE = 1024; //buffersize to use with pipes char buffer[BUFFERSIZE]; //actual buffer for using with pipes char readpipe[] = "PP0"; //two different pipe names char writepipe[] = "PP1"; int rpipedes, wpipedes; //pipe descriptors for referring to them FILE *pipestream; //for converting our pipe to a filestream struct stat pipestatus; //will receive pipe status information char reply[10]; //string used to get info from user int strnum; //holds which string number im' on double mydbl = 1.23456; //assignment says to make up some interesting values int myint = 101010; char mychar = 'a'; char mystring[] = "i'm interesting... really!"; //open up the pipe to read from it (should have been created earlier) if((rpipedes=open(readpipe,O_RDONLY)) == -1 ) { perror(" Error opening pipe for reading\n"); exit(-1); } //check to see if there's already a pipe there and if not, create one if(stat(writepipe ,&pipestatus) == -1) { if(errno != ENOENT) //we got a pretty big error here { perror("I can't stat the file and it appears to exist\n"); perror("Exiting... Fix this from the commandline\n"); exit(-1); } if(mknod(writepipe,S_IFIFO|0666,0) == -1) //okay, let's try to create the pipe, then { perror(" Error creating the pipe\n"); exit(-1); } } //maybe the pipe is already around in a different form else if(!S_ISFIFO(pipestatus.st_mode)) { printf("There is a file with the name of the pipe but it is not\n"); printf("a pipe. Should I remove the file and create the pipe (y/n)\n"); scanf("%s",reply); if((strcmp(reply,"y") == 0)||(strcmp(reply,"Y")== 0)) { printf(" Deleting the file and creating a pipe...\n\n"); if(unlink(writepipe) == -1) { perror("couldn't delete the file\n"); exit(-1); } if(mknod(writepipe,S_IFIFO|0666,0) == -1) { perror("error creating the pipe\n"); exit(-1); } } else { //user wanted to keep the file (whatever they say) perror(" OK, I'll leave the file. Exiting now...\n"); exit(1); } } else { printf(" Pipe appears to be there-- i'm gonna use it.\n"); fflush( stdout ); } // open the pipe if( ( wpipedes = open(writepipe, O_WRONLY) ) == -1 ) { perror("error opening pipe for writing\n"); exit(-1); } //create a stream to the pipe so we can use it like a file... if ((pipestream = fdopen(wpipedes, "w") ) == NULL ) { perror(" Unable to morph pipe into a stream.\n"); } //start looping and looking at our pipe strnum = 0; //don't forget to init which string i'm on for(;;) { // read some data if((received=read(rpipedes,buffer,BUFFERSIZE)) == -1) { perror(" Error reading from the pipe %s in P1\n\n"); exit(1); } else if (received == 0) break; //we've read all the data from the file, apparently //insert a newline after the last filled space buffer[received] = '\0'; //let's print it out to our stream and get it with cat //we'll look at which number we're on to decide what type //of value to pass it fprintf(pipestream,""); if (strnum < 6) fprintf(pipestream, buffer, mydbl); else if (strnum < 13) fprintf(pipestream, buffer, myint); else if (strnum < 15) fprintf(pipestream, buffer, mychar); else fprintf(pipestream, buffer, mystring); //insert a few blank lines for separation //so we can clearly see p0,p1...p0,p1... fprintf(pipestream, "\n\n"); //flush it through so it goes right now fflush( pipestream ); //increment our string counter strnum++; } fprintf(pipestream, "\n\n"); //make the file end with a few newlines //mommy always told me to clean up after myself close(rpipedes); //we no longer need that pipe to read from close(wpipedes); //we no longer need that pipe to write to fclose(pipestream); //we don't need to stream anymore either //let's close up shop and go home return 0; }