Issues running commands

I’m stuck on Stage Run a program #ip1

I created a function that executes the commands based on the path and arguments passed in but i don’t quite understand why it isn’t printing out the program signature and args
although the program works normally locally to run normal commands.

Here are my logs:

[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.
[tester::#IP1] Running tests for Stage #IP1 (Run a program)
[tester::#IP1] [setup] export PATH=/tmp/foo:$PATH
[tester::#IP1] [setup] Available executables:
[tester::#IP1] [setup] - custom_exe_1177
[tester::#IP1] Running ./your_program.sh
[your-program] $ custom_exe_1177 David
[your-program] $ Program was passed 2 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 2 args (including program name)."
[your-program] Arg #0 (program name): custom_exe_1177
[your-program] Arg #1: David
[your-program] Program Signature: 2588132668
[tester::#IP1] Assertion failed.
[tester::#IP1] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)
include relevant logs here (please make sure to keep the backticks around this!)

And here’s a snippet of my code:

void execute_program(char *path,char **arguments){
  pid_t pid,pid2;//one for the fork process and another to return the process id
  pid=fork();
  pid2=getpid();
  if(pid==0){
    execv(path,arguments);
    for(int i=0;arguments[i]!=NULL;i++){
      printf("Arg #0: %s",arguments[0]);
      printf("Arg #1: %s",arguments[1]);
      printf("program signature: %d",pid2);}
    //execv doesn't return anything
    perror("no such directory");
    exit(1);
  }
else if(check_for_built_in(arguments)==0){
    char **args=split_line(input);
    /*printf("commands \n");
    for (int i = 0; args[i] != NULL; i++) {
      printf("%s\n", args[i]);}*/

    char *path=find_in_path(arguments);
    execute_program(path,args);
  }

include relevant code here (please make sure to keep the backticks around this!)

local output


$ ls -l
$ total 4
drwxr-xr-x 1 Olu-Ade Olu-Ade    0 Jan 20 21:43 build 
-rw-r--r-- 1 Olu-Ade Olu-Ade 3840 Jan 23 11:31 main.c

here is the link to the repo

Hi @Fruitpunch44, the task is to run an executable file such as “custom_exe_1234”, so there’s no need to implement it manually:

for(int i=0;arguments[i]!=NULL;i++){
      printf("Arg #0: %s",arguments[0]);
      printf("Arg #1: %s",arguments[1]);
      printf("program signature: %d",pid2);}

Another issue is that there’s an extra "$ " printed out:


I tried running your code against the previous stages, but it’s actually no longer passing the second stage #CZ2 (Handle invalid commands).

Suggestions:

  1. Use our CLI to test against previous stages by running:
codecrafters test --previous
  1. Focus on fixing the early stages first, as later stages depend on them.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

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