Advertisement
shortspecialbus

job.c

May 19th, 2021
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. /*
  2.  * cs537 Project 1 - Shell
  3.  *
  4.  * Written by Stefan Strandberg (stefan@cs.wisc.edu)
  5.  * $Id: job.c,v 1.5 2004/09/23 21:06:15 stefan Exp $
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "job.h"
  13. #include "shell.h"
  14.  
  15.  
  16. ///////////////////
  17. //
  18. // create_job()
  19. //
  20. //   * creates a job, returns it
  21. //
  22. //////////////////
  23.  
  24. job_t *create_job(int jid, int pid, char* command, int argc, char* args[],
  25.                   int background, job_t *job) {
  26.     job->jid = jid;
  27.     job->pid = pid;
  28.     job->command = malloc(MAX_INPUT_SIZE);
  29.     if(job->command == NULL) {
  30.         fprintf(stderr, "Unable to malloc() command in create_job()!!\n");
  31.         exit(1);
  32.     }
  33.     strcpy(job->command, command);
  34.     job->argc = argc;
  35.     job->background = background;
  36.     int c;
  37.     for(c = 0; c < argc; c++) {
  38.         job->args[c] = malloc(MAX_INPUT_SIZE);
  39.         if(job->args[c] == NULL) {
  40.             fprintf(stderr, "Unable to malloc() a job in create_jobs()!!\n");
  41.             exit(1);
  42.         }
  43. //      job->args[c] = args[c];
  44.         strcpy(job->args[c], args[c]);
  45.     }
  46.    
  47.     return job;
  48. }
  49.  
  50.  
  51.  
  52. ////////////////////
  53. //
  54. // remove_job()
  55. //
  56. //   * removes a job, frees some of the memory
  57. //
  58. //  *FIXME: fix memory leak!!
  59. //
  60. ///////////////////
  61.  
  62. void remove_job(job_t *job) {
  63.     free(job->command);
  64.  
  65. // this free() code gives Segfaults and I don't care anymore, so it can go!
  66. //  int c = 0;
  67. //  //for(c = 0; c < job->argc ; c++) {
  68. //  while (job->args[c] != NULL) {
  69. //      printf("freeing job %d args at %d\n", job->jid, c);
  70. //      free(job->args[c]);
  71. //  }
  72.  
  73.     free(job);
  74. }
  75.  
  76.  
  77.  
  78. ///////////////////
  79. //
  80. // print_job()
  81. //
  82. //   * prints a job in the output needed by j
  83. //
  84. ///////////////////
  85.  
  86. void print_job(job_t *job) {
  87.     printf("%d : %s", job->jid, job->command);
  88.     int c;
  89.     for(c = 1; c < job->argc; c++)
  90.         printf(" %s", job->args[c]);
  91. //  printf(" pid: %d", job->pid);
  92.     printf("\n");
  93. }
  94.  
  95.  
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement