I’m stuck on #EI0.
My current approach is to use the __DIR__ magic PHP constant. When I run the program, pwd outputs /home/astlaw83/www/html/codecrafters-shell-php, which is correct. When I run codecrafters submit, it outputs /app/app.
I can’t really debug this as I don’t know how it is getting /app in the first place or how to replicate it.
Here are my logs:
[your-program] $ type pwd
[your-program] pwd is a shell builtin
[tester::#EI0] ✓ Received expected response
[your-program] $ pwd
[your-program] /app/app
[tester::#EI0] ^ Line does not match expected value.
[tester::#EI0] Expected: "/app"
[tester::#EI0] Received: "/app/app"
[your-program] $
[tester::#EI0] Test failed
Here are the main parts of the code:
$commands = ['exit', 'echo', 'type', 'pwd'];
// run the shell forever
while (true) {
fwrite(STDOUT, '$ ');
// get the user's command
$input = rtrim(fgets(STDIN), "\r\n");
// stop the shell if the command is 'exit'
if ($input === 'exit') break;
// find the command
$commandFound = false;
foreach ($commands as $command) {
$inputSplit = splitCommand($input);
// check if the command is built-in
if ($inputSplit[0] === $command) {
// call the command's function passing the input without the command
$functionName = "_$command";
$functionName(substr($input, strlen($command) + 1));
// exit loop
$commandFound = true;
break;
} else {
// check the command is an exec in PATH
$execPath = checkPath($inputSplit[0]);
// run exec if found
if ($execPath) {
system(implode(' ', $inputSplit));
// exit loop
$commandFound = true;
break;
}
}
}
// fallback if command not found
if (!$commandFound) fwrite(STDOUT, "$input: command not found" . PHP_EOL);
unset($commandFound);
}
// ...
function _pwd(string $input): void {
fwrite(STDOUT, __DIR__ . PHP_EOL);
}