I’m stuck on Stage #IP1 .
I’ve tried to update my code using stderror, also put some printf to debug but nothing is working.
Here are my logs:
remote: [compile] Moved ./.codecrafters/run.sh → ./your_program.sh
remote: [compile] Compilation successful.
remote:
remote: Debug = true
remote:
remote: [tester::#IP1] Running tests for Stage #IP1 (Run a program)
remote: [tester::#IP1] [setup] export PATH=/tmp/bar:$PATH
remote: [tester::#IP1] [setup] Available executables:
remote: [tester::#IP1] [setup] - custom_exe_1618
remote: [tester::#IP1] Running ./your_program.sh
remote: [your-program] $ custom_exe_1618 David
remote: [your-program] Program was passed 2 args (including program name).
remote: [your-program] Arg #0 (program name): custom_exe_1618
remote: [your-program] Arg #1: David
remote: [tester::#IP1] Output does not match expected value.
remote: [tester::#IP1] Expected: "Program Signature: 7597376296"
remote: [tester::#IP1] Received: ""
remote: [your-program] Program Signature: 7597376296
remote: [tester::#IP1] Assertion failed.
remote: [tester::#IP1] Test failed
And here’s a snippet of my code:
int status;
int pid = fork();
if(pid == 0){
execvp(program[0],program);
perror("execvp failed");
exit(EXIT_FAILURE);
}
else if (pid > 0){
waitpid(pid, &status, 0);
}
else{
perror("fork failed");
}
Here is reporsitory for my code
Contribute to ThosarSheth/Shell development by creating an account on GitHub.
Seems like the issue is that your shell is too slow in executing the provided binary.
In this stage, the binary provided to you actually logs 4 lines to the STDOUT:
Program was passed <COUNT_ARGS> args (including program name)
Arg #0 (program name): <ARG[0]>
Arg #1: <ARG[1]>
Program Signature: <RANDOM_CODE>
From the logs, it seems your shell begins the execution of the binary, and the binary correctly logs 3 lines too, but then (due to being too slow most probably) the tester logs the following failure:
remote: [tester::#IP1] Expected: "Program Signature: 7597376296"
remote: [tester::#IP1] Received: ""
showing that it expected the fourth line in the output, but got none (""
).
After that, your shell actually manages to complete the execution of the provided binary, and logs the correct output:
remote: [your-program] Program Signature: 7597376296
but by that time the tester has already marked the run as a failure.
1 Like
Thanks for letting know the root cause, and will you help me to tackle this situation ?
Hey @ThosarSheth , I can confirm that the current code does output an extra blank line:
Not entirely sure why it’s happening, but if I start from scratch, the issue doesn’t occur:
#include <stdio.h> // for printf, fflush, stdout, fgets
#include <stdlib.h> // for exit
#include <unistd.h> // for fork, execv
#include <sys/wait.h> // for wait
#include <sys/types.h> // for pid_t
#include <string.h> // for strcspn
#define MAX_INPUT 1024 // Define a reasonable buffer size
int main(int argc, char *argv[], char *envp[]) {
while (1) {
printf("$ ");
fflush(stdout);
char input[MAX_INPUT];
// Read input
if (fgets(input, MAX_INPUT, stdin) == NULL) {
// Handle EOF (Ctrl+D)
printf("\n");
}
// Remove newline if present
input[strcspn(input, "\n")] = 0;
// Split input into command and arguments
char *args[MAX_INPUT];
char *token = strtok(input, " ");
int i = 0;
while (token != NULL && i < MAX_INPUT - 1) {
args[i++] = token;
token = strtok(NULL, " ");
}
args[i] = NULL; // Null terminate the argument list
pid_t pid = fork();
if (pid == 0) {
// Child process
execvp(args[0], args); // Use execvp to search PATH
perror("execvp failed");
exit(1);
} else if (pid > 0) {
// Parent process
int status;
wait(&status);
} else {
perror("fork failed");
}
}
return 0;
}
Okay, looks like the newline (\x0a
) wasn’t stripped:
Thank you @andy1li it helps me a lot
Quick update: We’ve improved the error messages in this PR .
system
Closed
June 22, 2025, 5:19am
14
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.