//Smerge.c lab project 4 for CSC 340 //Spring 2009, for Dr. Pete. //Author: #include #include #include #include #include int getLine(int fd, char *line); void merge(int fd1, int fd2, int out); void sortAndPipe(char *file, int outPipe); void child(int *myPipe, int *otherPipe, char *fileName); void parent(int argc, char *argv[], int *pipe1, int *pipe2); int checkArgs(int argc, char *argv[]); //Read one line of text into char array, excluding newline character. //Array terminated with '\O'. //Return: 0 if end of file, 1 otherwise. //Parameter: fd is file descriptor from which to read, //Parameter: line is char array to contain the text being read. int getLine(int fd, char *line){ int bytes, j = 0; char buffer[2]; while( read(fd, buffer, 1) ){ line[j] = buffer[0]; if(buffer[0] == '\n' || buffer[0] == '\r'){ line[j] = '\0'; return 1; } j++; } return 0; } //Merge contents of two input streams and write results to output stream. //Parameter: fd1 is file descriptor for one input stream, //Parameter: fd2 is file descriptor for the other input stream, //Parameter: out is file descriptor for output stream. void merge(int fd1, int fd2, int out){ } //Perform a child's task: do pipe maintenance, call sortAndPipe(), //function. //Parameter: myPipe array containing my pipe's file descriptors. //Parameter: otherPipe array containing other pipe's file descriptors. //Parameter: fileName char array containing my input file name. void child(int *myPipe, int *otherPipe, char *fileName){ } //Perform the parent's task: create output file if necessary, //do pipe maintenance, call merge() function. //Parameter: argc argument count received from main(). //Parameter: argv argument list received from main(). //Parameter: pipe1 array containing first pipe's file descriptors. //Parameter: pipe2 array containing second pipe's file descriptors. void parent(int argc, char *argv[], int *pipe1, int *pipe2){ } //Sort contents of specified file and write to specified pipe. //Parameter: file is char array containing file name, //Parameter: outPipe is file descriptor for the write pipe. void sortAndPipe(char *file, int outPipe){ } //Check smerge command line for validity. //Return: 0 if command line valid else -1. //Parameter: argc argument count received from main() //Parameter: argv argument array received from main() int checkArgs(int argc, char *argv[]){ return 0; } //Perform main task: validate arguments, create two pipes, fork two // children, parent calls parent(), both children call child(). //Return: 0 if command worked else 1. //Parameter: argc number of command arguments, including command name. //Parameter: argv array containing command words int main(int argc, char *argv[]){ pid_t pid; // assign fork() return value to this variable. int pipe1[2], pipe2[2]; // use as arguments to the two pipe() calls // Check for valid arguments. If bad, terminate with return value 1. if (checkArgs(argc, argv) == -1) { return 1; } // YOU SUPPLY THE REST... return 0; }