// Main function determines whether one or many directory entries // will be recursively called. int main (int argc, char **argv) { execname = basename (argv[0]); set_execname (execname); set_options (argc, argv); if (flags.recursive == true) { if (flags.opct == 0) { recursive_list (WORKINGDIR); } else if (flags.opct == 1) { recursive_list (flags.pathname[0]); } else { for (int ind = 0; ind < flags.opct; ++ind) { recursive_list (flags.pathname[ind]); printf ("\n"); } } }else { if (flags.pathname == 0) { list (WORKINGDIR); } else if (flags.opct == 1) { list (flags.pathname[0]); } else { for (int ind = 0; ind < flags.opct; ++ind) { list (flags.pathname[ind]); printf ("\n"); } } } return (exit_status); }
//// // main // int main (int argc, char **argv) { // Creat a struct for the options. struct options options = {FALSE, FALSE, NULL, FALSE, NULL, FALSE, NULL}; set_execname (argv[0]); // Set the execution name. // Get the options from the command line. scan_opts (argc, argv, &options); // char buffer[256]; bzero (buffer, 256); int sockfd, n; int portno = 1337; // Default server port number 1337 struct sockaddr_in serv_addr; struct hostent *server; // SOCKET sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error ("opening socket"); // GETHOSTBYNAME server = gethostbyname (options.serverIP); if (server == NULL) error ("no such host"); bzero ((char *) &serv_addr, sizeof (serv_addr)); serv_addr.sin_family = AF_INET; bcopy ((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons (portno); // CONNECT if (connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) error ("connecting"); // vote protocol if (options.vote) { // convert candidate name to all lower case char *cand = strdup (options.candidateName); char *voter = strdup (options.voterID); int i; for (i = 0; cand[i]; ++i) cand[i] = tolower (cand[i]); vote_func (sockfd, cand, voter); free (cand); // strdup uses malloc free (voter); } // results protocol if (options.results) results_func (sockfd); // finish protocol n = write (sockfd, "done", 4); if (n < 0) error ("writing to socket"); bzero (buffer, 256); // verify finish n = read (sockfd, buffer, 255); if (n < 0) error ("reading socket"); if (strcmp (buffer, "done") != 0) error ("finishing"); // CLOSE close (sockfd); return get_exitstatus(); }
//// // main // // The main function to the shell program. // int main (int argc, char **argv) { (void) argc; // variables set_execname (argv[0]); // loop until user exits while (TRUE) { // repeat until user exits the shell char cmdline[1025]; // will only accept 1024 bzero (cmdline, 1025); char parsebuffer[2024]; bzero (parsebuffer, 2024); char *tokens[512]; int toknum = 0; // bool pipein = FALSE; // bool pipeout = FALSE; print_prompt(); // print the prompt // get up to 1024 chars from the user if (get_cmdline (cmdline) == FALSE) continue; // break the line into tokens if (parseline (cmdline, parsebuffer, tokens, &toknum)) { // parseline returned an error eprintf ("%s: %s\n", "Error", "parsing error caused by incorrect usage"); continue; } // Check if the user wishes to exit the shell if (check_exit (tokens, toknum)) exit (get_exitstatus()); // set the number of commands to be executed int numcmds = set_numofcmds (tokens, toknum); // the number of commands should not exceed 20 if (numcmds > 20) { eprintf ("%s: %s\n", "ERROR", "cannot handle more than 20 commands"); continue; } // initialize the current start index of the tokens int curstart; int nextstart = 0; int fd[2]; // int fdin[2]; // int fdout[2]; // loop fork-exec for each command int i; for (i = 0; i < numcmds && nextstart < toknum; ++i) { // setup current command, update current start, and determine pipe curstart = nextstart; char *command = set_command (tokens, toknum, &nextstart); // a null command will cause an error if (command == NULL || command[0] == '\0') { eprintf ("%s: %s\n", "FATAL ERROR", "encountered a null command"); break; } // make a new argv int newargc = get_newargc (tokens, curstart, nextstart); char *newargv[newargc + 1]; newargv[0] = command; newargv[newargc] = NULL; int j = 1; int k; for (k = curstart; k < nextstart && j < newargc; ++k) { if (tokens[k][0] == '1') { newargv[j] = &tokens[k][1]; ++j; } } /* // set pipe pipeout = checktopipe (tokens, curstart, nextstart); if (pipein) { fdin[0] = fdout[0]; fdin[1] = fdout[1]; } if (pipeout) { pipe (fdout); } */ // set redirection char *input_redirect = get_redirectarg (tokens, curstart, nextstart, '<'); char *output_redirect = get_redirectarg (tokens, curstart, nextstart, '>'); if (input_redirect != NULL) { // open file and set file descriptor fd[0] = open (input_redirect, O_RDONLY, 0755); } if (output_redirect != NULL) { // open file and set file descriptor fd[1] = open (output_redirect, O_CREAT | O_WRONLY | O_TRUNC, 0644); } // fork child process int status = 0; int pid = fork(); if (pid != 0) { /* this is the PARENT PROCESS */ /* // write to pipe from parent to child if (pipein) { close (fdin[0]); if (dup2 (fdin[1], STDOUT_FILENO) == -1) eprintf ("%s: %s\n", "ERROR", "could not open and write to pipe from parent to child"); close (fdin[1]); pipein = FALSE; } */ // wait for any child process to finish int waitstatus = waitpid (-1, &status, 0); /* // read pipe from child to parent if (pipeout) { close (fdout[1]); if (dup2 (fdout[0], STDIN_FILENO) == -1) eprintf ("%s: %s\n", "ERROR", "could not open and read pipe from child to parent"); pipein = TRUE; } */ // close the file descriptors caused by redirection // close stdin if (input_redirect != NULL) close (fd[0]); // close stdout if (output_redirect != NULL) close (fd[1]); // determine errors if (waitstatus == 0) { eprintf ("%s: %s\n", "ERROR", "error waiting on child process"); break; }else if (waitstatus == -1) { eprintf ("%s: %s\n", "ERROR", "error executing child process"); break; } if (status != 0) { eprintf ("%s: %s: %s %d\n", "ERROR", command, "exit status of", status); break; } }else { /* this is the CHILD PROCESS */ // set up pipe in from a previous command /* if (pipein) { close (fdin[1]); if (dup2 (fdin[0], STDIN_FILENO) == -1) eprintf ("%s: %s\n", "ERROR", "could not open and write to pipe from child to parent"); close (fdin[0]); } // set up pipe out to the next command if (pipeout) { close (fdout[0]); if (dup2 (fdout[1], STDOUT_FILENO) == -1) eprintf ("%s: %s\n", "ERROR", "could not open and write to pipe from child to parent"); } */ // set up redirection if (input_redirect != NULL) { // open file and set file descriptor // set file to stdin if (dup2 (fd[0], STDIN_FILENO) == -1) eprintf ("%s: %s \"%s\" %s\n", "ERROR", "could not open", input_redirect, "for input redirection"); } if (output_redirect != NULL) { // open file and set file descriptor // set stdout to file if (dup2 (fd[1], STDOUT_FILENO) == -1) eprintf ("%s: %s \"%s\" %s\n", "ERROR", "could not open", output_redirect, "for output redirection"); } // execute the command set_exitstatus (execvp (command, newargv)); // if there was an error executing the command eprintf ("%s: %s: %s\n", "ERROR", command, "could not execute"); return (get_exitstatus()); } } } return get_exitstatus(); }