MiniDevil As beautiful as a shell
exec_cmd.c File Reference

Command execution: forks and execs external commands. More...

#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include "executor.h"
#include "libft.h"
#include "env.h"
#include "ast.h"
#include "signals.h"
#include "get_next_line.h"
+ Include dependency graph for exec_cmd.c:

Detailed Description

Command execution: forks and execs external commands.

Definition in file exec_cmd.c.

Functions

static void handle_exec_error (char *path)
 Print an error for when execve fails. More...
 
static void child_execute (char *path, char **args, char **envp, t_shell *shell)
 Replace current process with the external command (execve) More...
 
static int wait_for_child (pid_t pid)
 Wait for a child process and get the correct exit status. More...
 
static int prepare_exec (char **args, t_env *env, char **path, char ***envp)
 Resolve the command's path and build envp array before forking. More...
 
int exec_external (char **args, t_shell *shell)
 Fork and execute an external command. More...
 

Function Documentation

◆ handle_exec_error()

static void handle_exec_error ( char *  path)
static

Print an error for when execve fails.

Fallback is EACCES (Permission denied) because it's the most likely otherwise

Parameters
pathPath that failed to execute

Definition at line 32 of file exec_cmd.c.

Referenced by child_execute().

+ Here is the caller graph for this function:

◆ child_execute()

static void child_execute ( char *  path,
char **  args,
char **  envp,
t_shell shell 
)
static

Replace current process with the external command (execve)

  • Resets signals to default and calls execve
  • On failure, prints an error, frees resources and exits with code 126
Parameters
pathResolved path (free on failure)
argsArgument array for execve
envpEnvironment array for execve (free on failure)
shellShell context (free on failure)

Definition at line 61 of file exec_cmd.c.

References t_shell::current_ast, t_shell::current_input, t_shell::env, free_ast(), free_env_list(), handle_exec_error(), and reset_child_signals().

Referenced by exec_external().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ wait_for_child()

static int wait_for_child ( pid_t  pid)
static

Wait for a child process and get the correct exit status.

Handles normal exits (WEXITSTATUS) and signal deaths (128 + signal)

Parameters
pidPID of the child to wait for
Returns
Correct exit code (0-255)

Definition at line 85 of file exec_cmd.c.

Referenced by exec_external().

+ Here is the caller graph for this function:

◆ prepare_exec()

static int prepare_exec ( char **  args,
t_env env,
char **  path,
char ***  envp 
)
static

Resolve the command's path and build envp array before forking.

Handles special cases: . and ..

Parameters
argsArgument array with arg[0] being the command name
envEnvironment list (for PATH)
pathresolved path
envpenvp array
Returns
0 on success or an error exit code (1, 2 or 127)

Definition at line 116 of file exec_cmd.c.

References env_to_array(), exec_cmd_not_found(), and find_cmd_path().

Referenced by exec_external().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exec_external()

int exec_external ( char **  args,
t_shell shell 
)

Fork and execute an external command.

Resolves the path, builds envp and forks a child that calls execve

  • If already in a child process, executes directly without forking
  • The parent waits for the child and returns its exit status code
Parameters
argsArgument array with arg[0] being the command name
shellShell context
Returns
exit status (0 success, 126 exec error or 127 not found)

Definition at line 150 of file exec_cmd.c.

References child_execute(), t_shell::env, t_shell::is_child, prepare_exec(), and wait_for_child().

Referenced by exec_simple_command().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Go to the source code of this file.