Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
executor_path.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* executor_path.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hgenc <hgenc@student.42kocaeli.com.tr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/01/26 17:39:41 by hgenc #+# #+# */
9/* Updated: 2026/02/03 16:16:00 by hgenc ### ########.fr */
10/* */
11/* ************************************************************************** */
12
14#include <stdlib.h>
15#include <unistd.h>
16
17/**
18 * @brief Dosyanın var olup olmadığını ve çalıştırılabilir olup olmadığını
19 * kontrol et
20 * @param path Dosya yolu
21 * @return int 0: yok, 1: var ama çalıştırılamaz, 2: çalıştırılabilir
22 */
23int is_executable(char *path)
24{
25 if (!path)
26 return (0);
27 if (access(path, F_OK) != 0)
28 return (0);
29 if (access(path, X_OK) != 0)
30 return (1);
31 return (2);
32}
33
34/**
35 * @brief Dizin ve komut adını birleştir
36 * @param dir Dizin yolu
37 * @param cmd Komut adı
38 * @return char* "/usr/bin" + "/" + "ls" = "/usr/bin/ls"
39 */
40char *join_path(char *dir, char *cmd)
41{
42 char *path_slash;
43 char *result;
44
45 if (!dir || !cmd)
46 return (NULL);
47 path_slash = ft_strjoin(dir, "/");
48 if (!path_slash)
49 return (NULL);
50 result = ft_strjoin(path_slash, cmd);
51 free(path_slash);
52 return (result);
53}
54
55/**
56 * @brief PATH environment değişkenini parse et
57 * @param shell Shell structure
58 * @return char** split edilmiş dizinler
59 */
60char **get_path_dirs(t_shell *shell)
61{
62 char *path_value;
63
64 if (!shell)
65 return (NULL);
66 path_value = get_env_value(shell->env_list, "PATH");
67 if (!path_value)
68 return (NULL);
69 return (ft_split(path_value, ':'));
70}
71
72/**
73 * @brief PATH dizinlerinde komutu ara
74 * @param dirs PATH dizinleri
75 * @param cmd Komut adı
76 * @return char* bulunan tam yol veya NULL
77 */
78char *search_path(char **dirs, char *cmd)
79{
80 int i;
81 char *full_path;
82 int exec_status;
83
84 if (!dirs || !cmd)
85 return (NULL);
86 i = 0;
87 while (dirs[i])
88 {
89 full_path = join_path(dirs[i], cmd);
90 if (!full_path)
91 {
92 i++;
93 continue ;
94 }
95 exec_status = is_executable(full_path);
96 if (exec_status == 2)
97 return (full_path);
98 free(full_path);
99 i++;
100 }
101 return (NULL);
102}
103
104/**
105 * @brief Ana PATH arama fonksiyonu
106 * @param shell Shell structure
107 * @param cmd Komut adı
108 * @return char* tam yol veya NULL
109 */
110char *find_cmd_path(t_shell *shell, char *cmd)
111{
112 char **dirs;
113 char *result;
114 int i;
115
116 if (!cmd || !cmd[0])
117 return (NULL);
118 if (ft_strchr(cmd, '/'))
119 {
120 if (is_executable(cmd) >= 1)
121 return (ft_strdup(cmd));
122 return (NULL);
123 }
124 dirs = get_path_dirs(shell);
125 if (!dirs)
126 return (NULL);
127 result = search_path(dirs, cmd);
128 i = 0;
129 while (dirs[i])
130 free(dirs[i++]);
131 free(dirs);
132 return (result);
133}
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
char ** get_path_dirs(t_shell *shell)
PATH environment değişkenini parse et.
char * search_path(char **dirs, char *cmd)
PATH dizinlerinde komutu ara.
int is_executable(char *path)
Dosyanın var olup olmadığını ve çalıştırılabilir olup olmadığını kontrol et.
char * find_cmd_path(t_shell *shell, char *cmd)
Ana PATH arama fonksiyonu.
char * join_path(char *dir, char *cmd)
Dizin ve komut adını birleştir.
Minishell ana header dosyası
struct s_shell t_shell
t_list * env_list
Definition minishell.h:149