MiniDevil As beautiful as a shell
path.c File Reference

Command path resolution: searches PATH & validates permissions. More...

#include "executor.h"
#include "env.h"
#include "libft.h"
+ Include dependency graph for path.c:

Detailed Description

Command path resolution: searches PATH & validates permissions.

Definition in file path.c.

Functions

static int is_direct_path (char *cmd)
 Check if a command contains a slash (= direct path) More...
 
static char * join_path (char *dir, char *cmd)
 Join a directory and command name with a / in between. More...
 
char * search_in_dir (char *dir, char *cmd)
 Search an executable command in a directory. More...
 
static char * search_in_path (char *cmd, char *path_var)
 Search for a command in all PATH directories. More...
 
char * find_cmd_path (char *cmd, t_env *env)
 Resolve a command name into its full executable path. More...
 

Function Documentation

◆ is_direct_path()

static int is_direct_path ( char *  cmd)
static

Check if a command contains a slash (= direct path)

Parameters
cmdCommand to check
Returns
1 if cmd contains / and 0 if not

Definition at line 23 of file path.c.

Referenced by find_cmd_path().

+ Here is the caller graph for this function:

◆ join_path()

static char* join_path ( char *  dir,
char *  cmd 
)
static

Join a directory and command name with a / in between.

Parameters
dirDirectory path
cmdCommand name
Returns
Newly allocated "dir/cmd" string or NULL

Definition at line 39 of file path.c.

Referenced by search_in_dir().

+ Here is the caller graph for this function:

◆ search_in_dir()

char* search_in_dir ( char *  dir,
char *  cmd 
)

Search an executable command in a directory.

Joins dir and cmd then checks if their result is executable via access(X_OK)

Parameters
dirDirectory to search in
cmdCommand name to look for
Returns
Full path if found AND executable or NULL

Definition at line 61 of file path.c.

References join_path().

Referenced by find_cmd_path(), and search_in_path().

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

◆ search_in_path()

static char* search_in_path ( char *  cmd,
char *  path_var 
)
static

Search for a command in all PATH directories.

Splits the PATH variable (by :) and tries each directory

Parameters
cmdCommand name to search for
path_varValue of the PATH environment variable
Returns
Full path to the executable or NULL if not found

Definition at line 85 of file path.c.

References search_in_dir().

Referenced by find_cmd_path().

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

◆ find_cmd_path()

char* find_cmd_path ( char *  cmd,
t_env env 
)

Resolve a command name into its full executable path.

If the command contains / it's treated as a direct path. Otherwise it looks up PATH in env and searches each directory until an executable match is found

Parameters
cmdCommand name or path to resolve
envEnvironment list
Returns
Full path, CWD match if PATH is unset or NULL if not found

Definition at line 115 of file path.c.

References get_env_value(), is_direct_path(), search_in_dir(), and search_in_path().

Referenced by prepare_exec().

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

Go to the source code of this file.