minishell
Minishell Project for École 42
Loading...
Searching...
No Matches
cd_builtin.c File Reference

Change directory builtin function. More...

#include "minishell.h"
Include dependency graph for cd_builtin.c:

Functions

void update_old (t_shell *shell, char *old)
 Update OLDPWD.
 
int env_var_update (t_shell *shell, char *old, char *new)
 Update PWD after directory change.
 
int cd_no_args (t_shell *shell)
 Handles the case when changing to a directory with no arguments.
 
int cd (t_shell *shell, char **args)
 Handles changing the current working directory with specified arguments.
 

Detailed Description

Change directory builtin function.

This file contains the implementation of the cd command The cd command is used to change the current working directory.

Function Documentation

◆ cd()

int cd ( t_shell shell,
char **  args 
)

Handles changing the current working directory with specified arguments.

This function is called when the cd command is executed with one argument, which is the target directory to change to. It checks the number of arguments, validates the input, and attempts to change the current working directory.

Parameters
[in]shellA pointer to the shell struct.
[in]argsAn array of strings containing command arguments.
Returns
An exit status (EXIT_SUCCESS on success, EXIT_FAILURE on failure).

◆ cd_no_args()

int cd_no_args ( t_shell shell)

Handles the case when changing to a directory with no arguments.

This function is called when the cd command is executed without any arguments. It attempts to change the current working directory to the user's home directory (retrieved from the environment variable HOME).

Parameters
[in]shellA pointer to the shell struct.
Returns
An exit status (EXIT_SUCCESS on success, EXIT_FAILURE on failure).

◆ env_var_update()

int env_var_update ( t_shell shell,
char *  old,
char *  new 
)

Update PWD after directory change.

Updates the environment variable PWD and passes the OLDPWD to the other update function.

Parameters
[in]shellA pointer to the shell struct.
[in]oldThe previous working directory.
[in]newThe new working directory.
Returns
An exit status (EXIT_SUCCESS on success, EXIT_FAILURE on failure).

◆ update_old()

void update_old ( t_shell shell,
char *  old 
)

Update OLDPWD.

Updates the OLDPWD environment variable with the value of the previous working directory before the change.

Parameters
[in]shellA pointer to the shell struct.
[in]oldThe previous working directory.