Advertisement
shortspecialbus

main.c

May 19th, 2021
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. /*
  2.  * cs537 Project 1 - Shell
  3.  *
  4.  * Written by Stefan Strandberg (stefan@cs.wisc.edu)
  5.  * $Id: main.c,v 1.8 2004/09/23 22:00:27 stefan Exp $
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13. #include <sys/wait.h>
  14. #include "shell.h"
  15. #include "job.h"
  16.  
  17. /*
  18.  * TODO:
  19.  *
  20.  * Fix horrible memory leaks...they're better than they were, but they're still
  21.  * there.
  22.  *
  23.  * Figure out why the heck my waitpid keeps getting -1.  Maybe I'm reaping
  24.  * Wrong or something.  Meh.
  25.  *
  26.  */
  27.  
  28. int main(int argc, char *argv[]) {
  29.    
  30.    
  31.     int should_exit = 0;
  32.     int interactive = 1;
  33.     int jobnum = 1;
  34.     int c;
  35.     FILE *fp = NULL;
  36.     FILE *inbuffer = stdin;
  37.     action_t action;
  38.     int status;
  39.  
  40.     // turn off friggen buffering...it ruins our day!!
  41.     setbuf(stdout, NULL);
  42.     switch (argc) {
  43.         case 1:
  44.             break;
  45.         case 2:
  46.             fp = fopen(argv[1], "r");
  47.             if (fp == NULL) {
  48.                 fprintf(stderr, "Error: unable to open file %s\n", argv[1]);
  49.                 return 1;
  50.             }
  51.             interactive = 0;
  52.             inbuffer = fp;
  53.             break;
  54.         default:
  55.             fprintf(stderr, "Invalid number of command line arguments: %i\n",
  56.                     (argc - 1));
  57.             return 1;
  58.     }
  59.     shell_t *turtle_shell = create_shell();
  60.     job_t *jobs[MAX_JOBS]; // we aren't freeing these...memory leak!!
  61.  
  62.     while(!should_exit) {
  63.         jobs[jobnum - 1] = malloc(sizeof(job_t));
  64.         if((jobs[jobnum - 1]) == NULL) {
  65.             fprintf(stderr, "Unable to malloc() a job in main()!!\n");
  66.             exit(1);
  67.         }
  68.         action = run_shell(turtle_shell, interactive, &jobnum, jobs[jobnum - 1],
  69.                          inbuffer);
  70.         switch (action) {
  71.             case WAITERROR:
  72.                 fprintf(stderr, "Invalid jid\n");
  73.                 break;
  74.             case EXITVAL:
  75.                 should_exit = 1;
  76.                 break;
  77.             case JOBVAL:
  78.                 do_jobcmd(jobs, jobnum);
  79.                 break;
  80.             case NORMAL:
  81.                 break; // nothing, just run our loop again
  82.             case CHILDEXIT:
  83.                 exit(1);
  84.             default:
  85.                 if (action < (jobnum))  
  86.                     do_waitcmd(jobs[action - 1]);
  87.                
  88.                 else
  89.                     printf("Invalid jid %d\n", action);
  90.                
  91.         }
  92.  
  93.         // reap our children here i guess.  we're doing a lot of dumb
  94.         // algorithms in this program (with reaping and the "j" command and
  95.         // such, but I'm sleepy and I haven't gotten any sleep in forever.
  96.         // this bad algorithm reaps zombies every command
  97.         for (c = 0 ; c < jobnum - 1 ; c++)
  98.             waitpid(jobs[c]->pid, &status, WNOHANG);
  99.     }
  100.     if (argc > 1)
  101.         fclose(fp);
  102.     for (c = 0 ; c < jobnum - 1; c++) {
  103.         remove_job(jobs[c]);
  104.     }
  105.     return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement