(C) NI6 - command not found cat

here’s my repo

currently im stuck on stage Single quotes #ni6
during execution, i got the error command not found

[tester::#NI6] Running tests for Stage #NI6 (Quoting - Single quotes)
[tester::#NI6] [setup] export PATH=/tmp/orange/raspberry/blueberry:$PATH
[tester::#NI6] Running ./your_program.sh
[tester::#NI6] [setup] echo -n "mango raspberry." > "/tmp/foo/f   91"
[tester::#NI6] [setup] echo -n "banana grape." > "/tmp/foo/f   94"
[tester::#NI6] [setup] echo -n "banana blueberry." > "/tmp/foo/f   10"
[your-program] $ echo 'world script'
[your-program] world script
[tester::#NI6] ✓ Received expected response
[your-program] $ echo script     test
[your-program] script test
[tester::#NI6] ✓ Received expected response
[your-program] $ echo 'shell     example' 'test''world'
[your-program] shell     example testworld
[tester::#NI6] ✓ Received expected response
[your-program] $ cat '/tmp/foo/f   91' '/tmp/foo/f   94' '/tmp/foo/f   10'
[your-program] cat '/tmp/foo/f   91' '/tmp/foo/f   94' '/tmp/foo/f   10': command not found
[tester::#NI6] Output does not match expected value.
[tester::#NI6] Expected: "mango raspberry.banana grape.banana blueberry."
[tester::#NI6] Received: "cat '/tmp/foo/f   91' '/tmp/foo/f   94' '/tmp/foo/f   10': command not found"
[your-program] $ 
[tester::#NI6] Assertion failed.
[tester::#NI6] Test failed

but weirdly when i tried to run locally, i got file not found which is expected

cat: "'/tmp/foo/f   65'": No such file or directory
cat: "'/tmp/foo/f   31'": No such file or directory
cat: "'/tmp/foo/f   37'": No such file or directory

ive tried to use my local debugger, and there seems to be nothing wrong with the argv content, am i missing something ?

I’ll take a look and get back to you by the end of the week.

1 Like

Hey,
My C knowledge is a bit rusty but I was able to recreate the issue you had. For me the issue occured only after the second time I tried to call a command in the shell. So the first thing I would change is clearing the argv between calls:

    char *argv[MAX_ARGC];
    memset(argv, 0, sizeof(argv));

and the second thing is you dynamically set strings in the split_string_with_quotes_and_space. Normally strings are automatically null terminated, but when creating them dynamically you should null terminate them. So you should add:

                    strncpy(result[*result_start_idx], start_pointer, curr_str_len);
                    result[*result_start_idx][curr_str_len] = '\0';

To all 3 places where you copy the string.
Let me know if this helps in fixing the issues you had.

2 Likes

yeah just tested your solution, and it seems the issue is because strncpy didn’t end the string with '\0' by default.

adding this line seems to result in another error which is execv: Bad address. the solution for which seems to be by adding

split_string_with_quotes_and_space(input, argv, &argc, &MAX_ARGC);
argv[argc] = 0;

to the execute_external_process func.

Thank you so much for the help.

1 Like

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