Test bug works fine localy

I’m stuck on Stage #MG5.

the code works fine locally but when submitting it fails

Here are my logs:

[tester::#MG5] Running tests for Stage #MG5 (The type builtin: executable files)
# command-line-arguments
loadinternal: cannot find runtime/cgo
[tester::#MG5] Running ./your_shell.sh
[your-program] $
[tester::#MG5] > type cat
[your-program] cat is /tmp/blueberry/blueberry/strawberry/cat
[your-program] $
[tester::#MG5] Expected first line of output to match "cat is /bin/cat", got "cat is /tmp/blueberry/blueberry/strawberry/cat"
[tester::#MG5] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

And this is the code to get the file path if the file is an executable

char *getfilepath(char *file) {
  char *PATH = getenv("PATH");
  assert(PATH);
  char *path = strtok(PATH, ":");
  do {
    if (access(path, X_OK) == 0) {
      char *str = malloc(strlen(path) + 1 + strlen(file) + 1);
      sprintf(str, "%s/%s", path, file);
      return str;
    }
  } while (path = strtok(NULL, ":"));
  return NULL;
}

You are not checking if the file exists in the paths you are iterating.

You just check if the path exist and if it does you append the filename to that path and return it, which is incorrect.

1 Like

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