Okay, I'm back. I tried something different. But it's not performing properly...
Code:
//Uses two pipes to establish 2-way communication
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#define MAXLINE 4096
char command[MAXLINE];
char choice1[] = "display";
char choice2[] = "chars";
char choice3[] = "lines";
char choice4[] = "words";
char choice5[] = "find";
char choice6[] = "exit";
char filename[50] = "";
char line[255];
char x;
int ch, c;
int count = 0;
int words = 0;
int l = 0;
FILE *fp;
FILE *in_file;
FILE *pFile;
FILE *wordcount;
int main(void){
int pid; //allows parent and child to identify themselves
int p[2]; //Pipe 'p'
int q[2]; //Pipe 'q'
//create 2 pipes
if (pipe(p) < 0 || pipe(q) < 0){
puts("Cannot create pipe");
exit(0);
} // end if pipe fails
pid = fork();
if (pid > 0){ //in parent process
close (p[0]);
close (q[1]);
menu(q[0], p[1]);
close (p[1]);
close(q[0]);
exit(0);
} //end if in parent
else{ //in child
close (p[1]);
close (q[0]);
execution(p[0], q[1]);
close (p[0]);
close (q[1]);
exit(0);
} //end if in child
} // end main program
int menu (int argc, char *argv[]){
printf("------------------------------------------\n");
printf("Please choose from the following commands:\n\n");
printf("display\n");
printf("chars\n");
printf("lines\n");
printf("words\n");
printf("find\n");
printf("exit\n\n");
printf("------------------------------------------\n");
printf("Please enter a command: ");
scanf("%s" "%s", command, filename);
printf("\n\nThe command is: %s %s\n\n", command, filename);
return(0);
}
int execution (int argc, char *argv[]){
if(strcmp(choice1, command) == 0){
printf("Executing command: %s %s\n\n", command, filename);
printf("=================\n");
printf("\nFile contents are: \n\n %s\n", filename);
fp = fopen(filename, "r");
if((fp == NULL))
{
printf("No file was selected!\n");
exit(1);
}
while(!feof(fp))
{
if(fgets(line, 255, fp))
printf("%s", line);
}
printf("\n\nEnd of file: \n\n");
printf("================\n\n");
printf("The command was executed\n\n");
printf("The file was displayed\n\n");
fclose(fp);
}
else if(strcmp(choice2, command) == 0){
printf("Executing command: %s %s\n\n", command, filename);
in_file = fopen(filename, "r");
if(in_file == NULL){
printf("Cannot open %s\n", filename);
exit(8);
}
while(1){
ch = fgetc(in_file);
if(ch == EOF)
break;
++count;
}
printf("\nNumber of characters in %s is %d\n\n\n", filename, count);
fclose(in_file);
printf("The command was executed\n\n");
printf("The file was displayed\n\n");
}
else if(strcmp(choice3, command) == 0){
printf("Executing command: %s \n\n", command);
pFile = fopen(filename, "r");
if(pFile == NULL)perror("Error openning file");
else{
do{
c = fgetc(pFile);
if(c == '\n') l++;
}
while(c != EOF);
printf("File contains %d lines\n\n", l);
fclose(pFile);
}
}
return 0;
}
It compiles okay in Dev-C++. I upload to my UNIX server and compile it there using gcc. It compiles with no errors. When I run it, the menu comes up and I can type in a command from the list. It displays the name of the command and the file and then nothing else. It does not go to the other function called execution and do the work.
What am I doing wrong?
Thanks!