MiniDevil As beautiful as a shell
builtin_cd.c File Reference

Implements cd: changes directory and updates OLDPWD/PWD. More...

#include <stdio.h>
#include "builtins.h"
#include "libft.h"
#include "env.h"
+ Include dependency graph for builtin_cd.c:

Detailed Description

Implements cd: changes directory and updates OLDPWD/PWD.

Definition in file builtin_cd.c.

Functions

static char * get_home_path (t_env *env)
 Get the HOME value from environment. More...
 
static char * get_cd_path (char **args, t_env *env, int *err)
 Get the target path for cd. More...
 
static void update_pwd_vars (t_env **env, char *old_pwd)
 Update OLDPWD and PWD environment variables after cd. More...
 
int builtin_cd (char **args, t_env **env)
 Implement the cd command. More...
 

Function Documentation

◆ get_home_path()

static char* get_home_path ( t_env env)
static

Get the HOME value from environment.

Parameters
envEnvironment list
Returns
HOME value or NULL with error message

Definition at line 24 of file builtin_cd.c.

References get_env_value().

Referenced by get_cd_path().

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

◆ get_cd_path()

static char* get_cd_path ( char **  args,
t_env env,
int *  err 
)
static

Get the target path for cd.

Handles no argument or "--" (HOME), "-" (OLDPWD and printed), "-- path" (path) and too many arguments (sets err = 1)

Parameters
argsArguments array
envEnvironment list for HOME and OLDPWD lookup
errSet to 1 if too many arguments and 0 otherwise
Returns
Target path or NULL on error

Definition at line 45 of file builtin_cd.c.

References get_env_value(), and get_home_path().

Referenced by builtin_cd().

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

◆ update_pwd_vars()

static void update_pwd_vars ( t_env **  env,
char *  old_pwd 
)
static

Update OLDPWD and PWD environment variables after cd.

Parameters
envPointer to env list head
old_pwdPrevious working directory (OLDPWD)

Definition at line 78 of file builtin_cd.c.

References set_env_value().

Referenced by builtin_cd().

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

◆ builtin_cd()

int builtin_cd ( char **  args,
t_env **  env 
)

Implement the cd command.

Changes the working directory and supports:

  • no args (HOME)
  • "-" (OLDPWD)
  • "--" separator
  • Error on too many arguments
Parameters
argsNULL terminated argument array with arg[0] = "cd"
envPointer to env list head
Returns
0 on success, 1 on error

Definition at line 101 of file builtin_cd.c.

References get_cd_path(), and update_pwd_vars().

Referenced by exec_builtin().

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

Go to the source code of this file.