Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
builtin_cd.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* builtin_cd.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hgenc <hgenc@student.42kocaeli.com.tr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/01/17 18:47:00 by hgenc #+# #+# */
9/* Updated: 2026/03/09 18:10:00 by hgenc ### ########.fr */
10/* */
11/* ************************************************************************** */
12
14#include <stdio.h>
15#include <stdlib.h>
16#include <unistd.h>
17
18static int cd_home(t_shell *shell)
19{
20 char *home;
21
22 home = get_env_value(shell->env_list, "HOME");
23 if (!home)
24 {
25 shell_error("cd", NULL, "HOME not set");
26 return (1);
27 }
28 if (chdir(home) == -1)
29 {
30 perror("minishell: cd");
31 return (1);
32 }
33 return (change_dir(shell, ft_strdup(home)));
34}
35
36static int cd_oldpwd(t_shell *shell)
37{
38 char *oldpwd;
39
40 oldpwd = get_env_value(shell->env_list, "OLDPWD");
41 if (!oldpwd)
42 {
43 shell_error("cd", NULL, "OLDPWD not set");
44 return (1);
45 }
46 if (chdir(oldpwd) == -1)
47 {
48 shell_error("cd", oldpwd, "No such file or directory");
49 return (1);
50 }
51 ft_putendl_fd(oldpwd, STDOUT_FILENO);
52 return (change_dir(shell, ft_strdup(oldpwd)));
53}
54
55static int cd_path(t_shell *shell, char *arg)
56{
57 char *new_pwd;
58
59 new_pwd = compute_logical_pwd(shell, arg);
60 if (!new_pwd)
61 {
62 if (chdir(arg) == -1)
63 {
64 shell_error("cd", arg, "No such file or directory");
65 return (1);
66 }
67 return (change_dir(shell, ft_strdup(arg)));
68 }
69 if (chdir(new_pwd) == -1)
70 {
71 shell_error("cd", arg, "No such file or directory");
72 free(new_pwd);
73 return (1);
74 }
75 return (change_dir(shell, new_pwd));
76}
77
78int builtin_cd(t_shell *shell, t_cmd *cmd)
79{
80 if (!cmd->args[1])
81 return (cd_home(shell));
82 if (!cmd->args[1][0])
83 {
84 shell_error("cd", "", "No such file or directory");
85 return (1);
86 }
87 if (cmd->args[2])
88 {
89 shell_error("cd", NULL, "too many arguments");
90 return (2);
91 }
92 if (ft_strcmp(cmd->args[1], "-") == 0)
93 return (cd_oldpwd(shell));
94 return (cd_path(shell, cmd->args[1]));
95}
static int cd_path(t_shell *shell, char *arg)
Definition builtin_cd.c:55
int builtin_cd(t_shell *shell, t_cmd *cmd)
Definition builtin_cd.c:78
static int cd_home(t_shell *shell)
Definition builtin_cd.c:18
static int cd_oldpwd(t_shell *shell)
Definition builtin_cd.c:36
int change_dir(t_shell *shell, char *new_pwd)
char * compute_logical_pwd(t_shell *shell, char *arg)
char * get_env_value(t_list *env_list, char *key)
Environment listesinde key'e göre değer döndürür.
Definition env_utils.c:73
void shell_error(char *cmd, char *arg, char *msg)
Minishell ana header dosyası
struct s_cmd t_cmd
struct s_shell t_shell
char ** args
Definition minishell.h:129
t_list * env_list
Definition minishell.h:149
int ft_strcmp(const char *s1, const char *s2)
Definition utils.c:48