Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "assign2_funcs.h"
  6.  
  7. //get input and split up input array
  8. char **get_input(char *input)
  9. {
  10.   int i=0;
  11.   int buffer = COMMAND_BUFF;
  12.   char **command;
  13.   //printf("values assigned\n");
  14.  
  15.   if((command = malloc(COMMAND_BUFF * sizeof(char))) == NULL){  //this allocates memmory to the commands double pointer
  16.     write(STDOUT_FILENO, MALLOC_FAIL, sizeof(MALLOC_FAIL));
  17.     exit(EXIT_FAILURE); //if unsuccessful, prints error message and exits
  18.   }
  19.  
  20.     // Returns first token
  21.     char* token = strtok(input, " ");
  22.  
  23.    // printf("token strtoked\n") ;
  24.  
  25.     // Keep printing tokens while one of the
  26.     // delimiters present in str[].
  27.     while (token != NULL) {
  28.        // printf("%s\n", token);
  29.         command[i] = token; //assign string to first slot in command
  30.         //printf("i = %d\n", i);
  31.         i++;
  32.         if(i >= (sizeof(command))/(sizeof(command[0]))){
  33.  
  34.           if( (command = realloc(command, (sizeof(char*) * (buffer + COMMAND_BUFF)) )) == NULL){
  35.             write(STDOUT_FILENO, MALLOC_FAIL, sizeof(MALLOC_FAIL));
  36.             exit(EXIT_FAILURE); //if unsuccessful, prints error message and exits
  37.            
  38.           }
  39.        
  40.         }
  41.  
  42.         token = strtok(NULL, " ");
  43.     }
  44.  
  45.     //printf("after while\n");
  46.     command[i] = NULL;
  47.     return command;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement