| 52 | } |
| 53 | |
| 54 | int split_command(char *input, char ***words, int *wordCount) |
| 55 | { |
| 56 | *words = NULL; |
| 57 | char *word = NULL; |
| 58 | int wordIndex = 0; |
| 59 | int inQuote = 0; |
| 60 | int inputLen = strlen(input); |
| 61 | |
| 62 | if (inputLen < 1) |
| 63 | { |
| 64 | fprintf(stderr, "Error: input had 0 length\n"); |
| 65 | cleanup_argarray(words, wordCount); |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | for (int i = 0; i < inputLen; i++) |
| 70 | { |
| 71 | char c = input[i]; |
| 72 | if (c == '\'' || c == '\"') |
| 73 | { |
| 74 | if (!inQuote && word != NULL) |
| 75 | { |
| 76 | process_word(words, wordCount, word, wordIndex); |
| 77 | word = NULL; |
| 78 | } |
| 79 | inQuote = !inQuote; |
| 80 | } |
| 81 | else if (c == ' ' && !inQuote) |
| 82 | { |
| 83 | if (word != NULL) |
| 84 | { |
| 85 | process_word(words, wordCount, word, wordIndex); |
| 86 | word = NULL; |
| 87 | } |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | if (word == NULL) |
| 92 | { |
| 93 | word = malloc(sizeof(char) * (inputLen + 1)); // Increased size by 1 to accommodate null character |
| 94 | if (word == NULL) |
| 95 | { |
| 96 | fprintf(stderr, "Error: memory allocation failed\n"); |
| 97 | cleanup_argarray(words, wordCount); |
| 98 | return 1; |
| 99 | } |
| 100 | wordIndex = 0; |
| 101 | } |
| 102 | word[wordIndex++] = c; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (word != NULL) |
| 107 | { |
| 108 | process_word(words, wordCount, word, wordIndex); |
| 109 | } |
| 110 | |
| 111 | *words = realloc(*words, sizeof(char *) * (*wordCount + 1)); |
no test coverage detected