# C: Shell tests work locally, but fail when pushed to remote

**URL:** <https://forum.codecrafters.io/t/c-shell-tests-work-locally-but-fail-when-pushed-to-remote/15506>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [November 8, 2025, 2:51am UTC](https://forum.codecrafters.io/t/c-shell-tests-work-locally-but-fail-when-pushed-to-remote/15506 "2025-11-08T02:51:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sriramr98](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/sriramr98/32/19656_2.png) [@sriramr98](https://forum.codecrafters.io/u/sriramr98)\
**Post date:** [November 8, 2025, 2:51am UTC](https://forum.codecrafters.io/t/c-shell-tests-work-locally-but-fail-when-pushed-to-remote/15506/1 "2025-11-08T02:51:00Z")

</div>

Hey Guys. I’m working on the shell challenge to learn C and I’m at the test where we implement for `echo`. When I run `./your-program.sh` directly and input `exit 0` , the shell promptly exits. However, when I run the test using `codecrafters test` , exit somehow fails to work properly.

CODECRAFTERS TEST OUTPUT

 ![Screenshot 2025-11-08 at 8.17.43 AM](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/8/4/8445b6fc5fc26805fcf24736b596b9d40a75480c.png)

Here’s the code`

`main.c`

```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "array_list.h"

char* EXIT_COMMAND = "exit";
char* ECHO_COMMAND = "echo";

typedef struct {
  char* command;
  ArrayList* inputs;
} Command;

void free_command(Command* cmd) {
  free(cmd->command);
  free_array_list(cmd->inputs);
  free(cmd);
}

Command* parse_command(char* command) {
  Command* cmd = malloc(sizeof(Command));
  cmd->inputs = new_array_list(10);

  char* token = strtok(command, " ");

  while (token != NULL) {
    int length = strlen(token);
    if (cmd->command == NULL) {
      cmd->command = malloc(sizeof(char) * length);
      strcpy(cmd->command, token);
    } else {
      add_array_list(cmd->inputs, token);
    }
    token = strtok(NULL, " ");
  }

  return cmd;
}

void perform_echo(Command* cmd) {
  if (cmd->inputs == NULL || cmd->inputs->size == 0) {
    return;
  }
  for (int i=0; i<cmd->inputs->size; i++) {
    printf("%s", get_element_from_list(cmd->inputs, i));
    if (i != cmd->inputs->size-1) {
      printf(" ");
    }
  }

  printf("\n");
}

// Captures the user's command in the "command" variable
char command[1024];

int main(int argc, char *argv[]) {
  // Flush after every printf
  setbuf(stdout, NULL);

  while (1)
  {
    printf("$ ");

    // Captures the user's command in the "command" variable
    char command[1024];
    fgets(command, sizeof(command), stdin);

    command[strcspn(command, "\n")] = '\0';

    Command* cmd = parse_command(command);
    if (cmd == NULL) {
      continue;
    }

    if (strcmp(cmd->command, EXIT_COMMAND) == 0) {
      return 0;
    }

    if (strcmp(cmd->command, ECHO_COMMAND) == 0) {
      perform_echo(cmd);
      continue;
    }

    printf("%s: command not found\n", command);

    free_command(cmd);
  }
  
  return 0;
}

```

`array_list.c`

```c
#include <stdlib.h>
#include <string.h>
#include "array_list.h"

ArrayList* new_array_list(int capacity) {
    if (capacity <= 0) {
        return NULL;
    }
    
    ArrayList* list = malloc(sizeof(ArrayList));
    if (list == NULL) {
        return NULL;
    }
    
    list->capacity = capacity;
    list->size = 0;
    list->inputs = malloc(capacity * sizeof(char*));
    
    if (list->inputs == NULL) {
        free(list);
        return NULL;
    }

    return list;
}

void add_array_list(ArrayList* list, char* element) {
    if (list == NULL || element == NULL) {
        return;
    }
    
    // Check if we need to resize
    if (list->size >= list->capacity) {
        // Double the capacity
        int new_capacity = list->capacity * 2;
        char** new_inputs = realloc(list->inputs, new_capacity * sizeof(char*));
        
        if (new_inputs == NULL) {
            return; // Failed to resize, could not add element
        }
        
        list->inputs = new_inputs;
        list->capacity = new_capacity;
    }
    
    // Allocate memory for the string copy
    list->inputs[list->size] = malloc((strlen(element) + 1) * sizeof(char));
    if (list->inputs[list->size] == NULL) {
        return;
    }
    
    // Copy the string
    strcpy(list->inputs[list->size], element);
    list->size++;
}

char* get_element_from_list(ArrayList* list, int index) {
    if (list == NULL || index < 0 || index >= list->size) {
        return NULL;
    }
    
    return list->inputs[index];
}

void free_array_list(ArrayList* list) {
    if (list == NULL) {
        return;
    }
    
    // Free all allocated strings
    for (int i = 0; i < list->size; i++) {
        if (list->inputs[i] != NULL) {
            free(list->inputs[i]);
        }
    }
    
    // Free the array of pointers
    free(list->inputs);
    
    // Free the list structure itself
    free(list);
}

int array_list_size(ArrayList* list) {
    if (list == NULL) {
        return -1;
    }
    return list->size;
}

int array_list_capacity(ArrayList* list) {
    if (list == NULL) {
        return -1;
    }
    return list->capacity;
}

```

---

<div class="post-metadata">

**Author:** ![sriramr98](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/sriramr98/32/19656_2.png) [@sriramr98](https://forum.codecrafters.io/u/sriramr98)\
**Post date:** [November 8, 2025, 2:52am UTC](https://forum.codecrafters.io/t/c-shell-tests-work-locally-but-fail-when-pushed-to-remote/15506/3 "2025-11-08T02:52:08Z")

</div>

I couldn’t attach the local execution output in the question, so here it is

 ![Screenshot 2025-11-08 at 8.21.48 AM](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/c/5/c5404e3715e218e79f5b669102834265e0ca36d4.png)

---

<div class="post-metadata">

**Author:** ![sriramr98](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/sriramr98/32/19656_2.png) [@sriramr98](https://forum.codecrafters.io/u/sriramr98)\
**Post date:** [November 9, 2025, 3:41am UTC](https://forum.codecrafters.io/t/c-shell-tests-work-locally-but-fail-when-pushed-to-remote/15506/5 "2025-11-09T03:41:12Z")

</div>

I found the issue.

In the `parse_command` function, I had to initialize `cmd→command = NULL;` after `malloc`. Since I didn’t initialize it to null, it sometimes pointed to garbage value and caused unintended consequence.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [November 14, 2025, 4:26am UTC](https://forum.codecrafters.io/t/c-shell-tests-work-locally-but-fail-when-pushed-to-remote/15506/7 "2025-11-14T04:26:57Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
