[C][Stage #IP1] local testing works but codecrafters test fails?

I’m stuck on Stage #IP1.

I’ve tried running the program locally with the path to my local program to print the agruments and their count, it prints correctly, even in the code crafters cli i get the correct output for everything except for the line

Program was passed X args (including program name).

here i am getting X to be more than X but in logs i am getting upto exactly X-1(including 0).

Here are my logs:

[tester::#IP1] Running tests for Stage #IP1 (Run a program)

[tester::#IP1] [setup] export PATH=/tmp/baz:$PATH

[tester::#IP1] [setup] Available executables:

[tester::#IP1] [setup] - custom_exe_8647

[tester::#IP1] Running ./your_program.sh

[your-program] $ custom_exe_8647 Emily

[your-program] Program was passed 3 args (including program name).

[tester::#IP1] Output does not match expected value.

[tester::#IP1] Expected: "Program was passed 2 args (including program name)."

[tester::#IP1] Received: "Program was passed 3 args (including program name)."

[your-program] Arg #0 (program name): custom_exe_8647

[your-program] Arg #1: Emily

[your-program] Program Signature: 4964934947

[your-program] $

[tester::#IP1] Assertion failed.

[tester::#IP1] Test failed

And here’s a snippet of my code:

void executablesInPath(char* input) {
    char* duplicateInput = strdup(input);
    char* path = getenv("PATH");
    if(!duplicateInput) {
        perror("strdup in executablesInPath");
        return;
    }
    char* command = strtok(duplicateInput, " ");
    char* commandPath = findCommandInPath(command, path);
    if(commandPath != NULL) {
        if(!duplicateInput) {
            perror("strdup in executablesInPath");
            return;
        }
        char* argumentArray[100];
        int argumentCount = 0;
        char* argument = strtok(input, " ");
        while(argument != NULL) {
            if(!isspace(argument[0])){
                argumentArray[argumentCount] = argument;
                argumentCount++;
            }
            argument = strtok(NULL, " ");
        }
        pid_t pid = fork();
        if(pid == 0) {
            execv(commandPath, argumentArray);
            perror("execv in executablesInPath");
        } else if(pid < 0) {
            perror("fork in executablesInPath");
        } else {
            wait(NULL);
        }
        free(duplicateInput);
    }
    return;
}

@21mcme04, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

@andy1li GitHub - 21mcme04/codecrafters-shell-c here is the github repo link.

There was a small issue with the code, fixed it(added NULL termination for exec). Now it is working.

1 Like

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