MiniDevil As beautiful as a shell
builtin_cd.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* builtin_cd.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/11 06:22:28 by baelgadi #+# #+# */
9 /* Updated: 2026/03/04 04:40:30 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include <stdio.h>
14 #include "builtins.h"
15 #include "libft.h"
16 #include "env.h"
17 
24 static char *get_home_path(t_env *env)
25 {
26  char *path;
27 
28  path = get_env_value(env, "HOME");
29  if (!path)
30  ft_putstr_fd("minishell: cd: HOME not set\n", STDERR_FILENO);
31  return (path);
32 }
33 
45 static char *get_cd_path(char **args, t_env *env, int *err)
46 {
47  char *path;
48 
49  *err = 0;
50  if (!args[1] || ft_strncmp(args[1], "--", 3) == 0)
51  {
52  if (args[1] && args[2] && args[3])
53  return (*err = 1, NULL);
54  if (args[1] && args[2])
55  return (args[2]);
56  return (get_home_path(env));
57  }
58  if (args[2])
59  return (*err = 1, NULL);
60  if (ft_strncmp(args[1], "-", 2) == 0)
61  {
62  path = get_env_value(env, "OLDPWD");
63  if (!path)
64  ft_putstr_fd("minishell: cd: OLDPWD not set\n", STDERR_FILENO);
65  else
66  ft_putendl_fd(path, STDOUT_FILENO);
67  return (path);
68  }
69  return (args[1]);
70 }
71 
78 static void update_pwd_vars(t_env **env, char *old_pwd)
79 {
80  char cwd[PATH_MAX];
81 
82  if (old_pwd && old_pwd[0])
83  set_env_value(env, "OLDPWD", old_pwd);
84  if (getcwd(cwd, PATH_MAX))
85  set_env_value(env, "PWD", cwd);
86 }
87 
101 int builtin_cd(char **args, t_env **env)
102 {
103  char *path;
104  char old_pwd[PATH_MAX];
105  int too_many;
106 
107  if (!getcwd(old_pwd, PATH_MAX))
108  old_pwd[0] = '\0';
109  path = get_cd_path(args, *env, &too_many);
110  if (too_many)
111  return (ft_putstr_fd("minishell: cd: too many arguments\n", 2), 1);
112  if (!path)
113  return (1);
114  if (chdir(path) == -1)
115  {
116  ft_putstr_fd("minishell: cd: ", STDERR_FILENO);
117  perror(path);
118  return (1);
119  }
120  update_pwd_vars(env, old_pwd);
121  return (0);
122 }
static void update_pwd_vars(t_env **env, char *old_pwd)
Update OLDPWD and PWD environment variables after cd.
Definition: builtin_cd.c:78
int builtin_cd(char **args, t_env **env)
Implement the cd command.
Definition: builtin_cd.c:101
static char * get_cd_path(char **args, t_env *env, int *err)
Get the target path for cd.
Definition: builtin_cd.c:45
static char * get_home_path(t_env *env)
Get the HOME value from environment.
Definition: builtin_cd.c:24
Shell builtin commands prototypes.
Environment variable management prototypes.
int set_env_value(t_env **env_list, char *key, char *value)
Set or create an environment variable.
char * get_env_value(t_env *env_list, char *key)
Look up an environment variable's value by its key.
Environment variable (doubly linked list)
Definition: structs.h:87