From 050b2fff6af14ed0a4188229c1e1d7502cd928fd Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Thu, 19 Oct 2017 19:02:48 -0400 Subject: [PATCH] Clean up makeargv file --- makeargv.c | 123 +++++++++++++++++++++++++---------------------------- 1 file changed, 59 insertions(+), 64 deletions(-) diff --git a/makeargv.c b/makeargv.c index 00a446a..4e4a4a9 100644 --- a/makeargv.c +++ b/makeargv.c @@ -1,79 +1,74 @@ -// makeargv.c -// takes a null terminated string s, and a string of delimiters and returns -// an array of char * pointers, leaving argvp pointing to that array. -// each of the array locations points to a null terminated "token" found in s -// defined by the delimiter array. The number of tokens found is returned by makeargv #include <stdio.h> #include <string.h> #include <stdlib.h> -/* - * Make argv array (*argvp) for tokens in s which are separated by - * delimiters. Return -1 on error or the number of tokens otherwise. + + +/** + * Creates an equivalent of argv, based off of provided command line. + * The last arg in the array is always null. + * + * Command: Null terminated command line to parse. + * Delimiters: Single character delimiters to help parse command string. + * Argvp: The pointer to an array of strings for holding the parsed command. + * + * Return: Negative for failure. Otherwise, the number of tokens parsed into array. */ -int makeargv(char * s, char * delimiters, char *** argvp) -{ - char * t; - char * snew; +int makeargv(char* command, char* delimiters, char*** argvp) { + char* tempString; + char* strippedString; int numtokens; - int i; - - /* snew is real start of string after skipping leading delimiters */ - snew = s + strspn(s, delimiters); //very clever - //printf("s = %s\n", s); - //printf("delimiters = %s\n", delimiters); - //printf("strspn = %d\n", (int) strspn(s, delimiters)); - //printf("snew = %s\n", snew); - - /* create space for a copy of snew in t */ - if ((t = calloc(strlen(snew) + 1, sizeof(char))) == NULL) //only true if no - //more memory - { + int index; + + // Strippedstring is real start of string after removing leading delimiters. + strippedString = command + strspn(command, delimiters); + // printf("Command: %s\n", command); + // printf("Delimiters: %s\n", delimiters); + // printf("Strspn: %d\n", (int) strspn(command, delimiters)); + // printf("StrippedString: %s\n", strippedString); + + // Create space for a copy of strippedString in tempString. + tempString = calloc((strlen(strippedString) + 1), sizeof(char*)); + if (tempString == NULL) { *argvp = NULL; - numtokens = -1; - } - else - { /* count the number of tokens in snew */ - strcpy(t, snew); - - if (strtok(t, delimiters) == NULL) + return -1; + } else { + strcpy(tempString, strippedString); + + // Check if values to parse. If none, string was empty. + if (strtok(tempString, delimiters) == NULL) { numtokens = 0; - else - for (numtokens = 1; strtok(NULL, delimiters) != NULL; numtokens++); // THE work is in the For Construct - //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - //start at 1 and move to the next, - // if its null stop - // else increment and move to the next - - /* create an argument array to contain ptrs to tokens */ - if ((*argvp = calloc(numtokens + 1, sizeof(char *))) == NULL) - //^Notice the dereferencing //Again only true if - //no more memory - { - free(t); - numtokens = -1; + } else { + // Abuse a for loop to count number of tokens. + // Start at 1 and loop until no more. + for (numtokens = 1; strtok(NULL, delimiters) != NULL; numtokens++); } - else - { /* insert pointers to tokens into the array */ - if (numtokens > 0) - { - strcpy(t, snew); - **argvp = strtok(t, delimiters); //set the first token - // printf("1 = %s\n", **argvp); - - for (i = 1; i < numtokens + 1; i++) - { - *((*argvp) + i) = strtok(NULL, delimiters); - //^^^^^^^^^^^^^^^^ set the other tokens - // printf("%d = %s\n", i, *((*argvp) + i)); + free(tempString); + + // Create an argument array to contain pointers to tokens. + *argvp = calloc(numtokens + 1, sizeof(char*)); + if (*argvp == NULL) { + return -1; + } else { + // Insert pointers for tokens into the array. + if (numtokens > 0) { + tempString = calloc((strlen(strippedString) + 1), sizeof(char*)); + strcpy(tempString, strippedString); + + // Set first token. + **argvp = strtok(tempString, delimiters); + // printf("Token 1: %s\n", **argvp); + + // Set other tokens. + for (index = 1; index < numtokens + 1; index++) { + *(*argvp + index) = strtok(NULL, delimiters); + // printf("Token %d = %s\n", index, *((*argvp) + index)); } - } - else - { + } else { **argvp = NULL; - free(t); } } } - + + // printf("Tokens: %d\n", numtokens); return numtokens; } -- GitLab