Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include "assign2_funcs.h"
- //get input and split up input array
- char **get_input(char *input)
- {
- int i=0;
- int buffer = COMMAND_BUFF;
- char **command;
- //printf("values assigned\n");
- if((command = malloc(COMMAND_BUFF * sizeof(char))) == NULL){ //this allocates memmory to the commands double pointer
- write(STDOUT_FILENO, MALLOC_FAIL, sizeof(MALLOC_FAIL));
- exit(EXIT_FAILURE); //if unsuccessful, prints error message and exits
- }
- // Returns first token
- char* token = strtok(input, " ");
- // printf("token strtoked\n") ;
- // Keep printing tokens while one of the
- // delimiters present in str[].
- while (token != NULL) {
- // printf("%s\n", token);
- command[i] = token; //assign string to first slot in command
- //printf("i = %d\n", i);
- i++;
- if(i >= (sizeof(command))/(sizeof(command[0]))){
- if( (command = realloc(command, (sizeof(char*) * (buffer + COMMAND_BUFF)) )) == NULL){
- write(STDOUT_FILENO, MALLOC_FAIL, sizeof(MALLOC_FAIL));
- exit(EXIT_FAILURE); //if unsuccessful, prints error message and exits
- }
- }
- token = strtok(NULL, " ");
- }
- //printf("after while\n");
- command[i] = NULL;
- return command;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement