Skip to content

Commit 46d96e0

Browse files
committed
no leaks
1 parent 8c28064 commit 46d96e0

14 files changed

+129
-52
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ CFLAGS = -Wall -Wextra -Werror -MD -g
44

55
NAME = Minishell
66
SRC_DIR = src
7-
SRC = main.c utils.c utils2.c init.c prints.c builtin.c builtin2.c builtin3.c builtin_utils.c quote.c signal.c ft_parser.c quote_state.c parser_utils.c calls.c ft_do_commands_utils.c ft_do_commands_utils2.c ft_do_commands.c ft_do_commands2.c ft_do_commands_first_last.c executer_utils.c executer_utils2.c executer.c ft_split_quote.c remove_quotes.c executer_utils3.c remove_quotes_utils.c ft_do_commands_first_last2.c ft_do_commands3.c
7+
SRC = main.c utils.c utils2.c init.c prints.c builtin.c builtin2.c builtin3.c builtin_utils.c quote.c signal.c ft_parser.c quote_state.c parser_utils.c calls.c ft_do_commands_utils.c \
8+
ft_do_commands_utils2.c ft_do_commands.c ft_do_commands2.c ft_do_commands_first_last.c executer_utils.c executer_utils2.c executer.c ft_split_quote.c remove_quotes.c executer_utils3.c \
9+
remove_quotes_utils.c ft_do_commands_first_last2.c ft_do_commands3.c conditions.c
10+
811
BUILD_DIR = build
912
INCLUDE_DIR = includes
1013
INCLUDES = -I ./$(INCLUDE_DIR) -I ./$(LIB_DIR)/libft/includes -I ~/.brew/opt/readline/include/

file

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

file.txt

Whitespace-only changes.

hola

Whitespace-only changes.

includes/minishell.h

+32-26
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* minishell.h :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: alvalope <alvalope@student.42.fr> +#+ +:+ +#+ */
6+
/* By: ysingh <ysingh@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/05/31 18:55:13 by ysingh #+# #+# */
9-
/* Updated: 2023/08/16 16:03:26 by alvalope ### ########.fr */
9+
/* Updated: 2023/08/16 21:23:28 by ysingh ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -90,6 +90,29 @@ typedef struct s_global
9090
char *dir;
9191
} t_global;
9292

93+
typedef struct pipex
94+
{
95+
char **archive_lines;
96+
int lines;
97+
char **paths;
98+
char ***args;
99+
int *n_args;
100+
char **infile;
101+
int *heredoc;
102+
char **outfl;
103+
int *outmode;
104+
int *command_not_found;
105+
int i;
106+
} t_pipex;
107+
108+
typedef struct s_aux
109+
{
110+
int i;
111+
int pipes;
112+
int cmd;
113+
int args;
114+
} t_aux;
115+
93116
t_global g_global;
94117

95118
int __quote_state(char c, int old_state);
@@ -139,30 +162,8 @@ int check_pwd(void);
139162
void do_cd(char *path, int flag);
140163
void update_pwd(void);
141164
void do_exit(int status);
142-
143-
typedef struct pipex
144-
{
145-
char **archive_lines;
146-
int lines;
147-
char **paths;
148-
char ***args;
149-
int *n_args;
150-
char **infile;
151-
int *heredoc;
152-
char **outfl;
153-
int *outmode;
154-
int *command_not_found;
155-
int i;
156-
} t_pipex;
157-
158-
typedef struct s_aux
159-
{
160-
int i;
161-
int pipes;
162-
int cmd;
163-
int args;
164-
} t_aux;
165-
165+
void if_no_builtin(t_pipex *p, t_aux *auxs, char **argv,
166+
char **paths);
166167
int ft_open_in_file(t_pipex *p, int fd[2]);
167168
void ft_open_out_file(t_pipex *p, int fd2[2]);
168169
int ft_open_first_file(t_pipex *p, int *file);
@@ -217,5 +218,10 @@ int ft_do_else(t_pipex *p, int fd[2]);
217218
int ft_do_commands2(t_pipex *p, int n_com);
218219
int ft_do_command(t_pipex *p, int fd[2], int fd2[2]);
219220
int ft_do_command2builtin(t_pipex *p, int fd[2], int fd2[]);
221+
int condition(t_master mas);
222+
int condition2(char *cmd, t_master mas);
223+
int condition3(t_master mas);
224+
void if_dollar_int(t_master *mas);
225+
void init_all(t_master *mas, char *cmd);
220226

221227
#endif

src/conditions.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* conditions.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ysingh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/08/16 19:06:26 by ysingh #+# #+# */
9+
/* Updated: 2023/08/16 19:07:29 by ysingh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "minishell.h"
14+
15+
int condition(t_master mas)
16+
{
17+
return (mas.strs.aux[1] == '\0' || mas.strs.aux[1] == '$'
18+
|| mas.strs.aux[1] == '=' || mas.strs.aux[1] == '\"'
19+
|| mas.strs.aux[1] == '\'' || mas.strs.aux[1] == '?'
20+
|| mas.strs.aux[1] == '|');
21+
}
22+
23+
int condition2(char *cmd, t_master mas)
24+
{
25+
return ((cmd[ft_strlen(cmd) - 1] == SQ) || !ft_strchr(mas.strs.cpy, '$'));
26+
}
27+
28+
int condition3(t_master mas)
29+
{
30+
return (mas.strs.cpy[mas.count.i] == '$' && mas.strs.cpy[mas.count.i
31+
+ 1] == '?');
32+
}
33+
34+
void if_dollar_int(t_master *mas)
35+
{
36+
char *temp;
37+
38+
temp = ft_itoa(g_global.exit_status);
39+
mas->strs.cpy = ft_replace(mas->strs.cpy, mas->strs.cpy + mas->count.i,
40+
mas->strs.cpy + mas->count.i + 2, temp);
41+
mas->count.i += (int)ft_strlen(temp);
42+
free(temp);
43+
}
44+
45+
void init_all(t_master *mas, char *cmd)
46+
{
47+
mas->count.i = 0;
48+
mas->strs.cpy = ft_strdup(cmd);
49+
}

src/executer.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: alvalope <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/06/20 12:57:41 by alvalope #+# #+# */
9-
/* Updated: 2023/08/16 16:56:55 by alvalope ### ########.fr */
9+
/* Updated: 2023/08/16 20:40:20 by ysingh ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -45,18 +45,14 @@ int ft_separate_1st_cmd2(t_pipex *p, t_aux *auxs, char **argv)
4545

4646
void ft_separate_1st_cmd(t_pipex *p, t_aux *auxs, char **argv, char **paths)
4747
{
48-
char *temp;
49-
5048
auxs->args = 0;
5149
while (argv[auxs->i] && ft_separate_1st_cmd2(p, auxs, argv))
5250
;
5351
if (argv[auxs->i] && strncmp(argv[auxs->i], "|\0", 2) != 0)
5452
{
5553
if (!get_builtin(argv[auxs->i]))
5654
{
57-
temp = ft_strdup(argv[auxs->i]);
58-
p->paths[auxs->cmd] = ft_check_comm(paths, temp, p, auxs->cmd);
59-
free(temp);
55+
if_no_builtin(p, auxs, argv, paths);
6056
}
6157
else
6258
p->paths[auxs->cmd] = ft_strdup(argv[auxs->i]);
@@ -94,9 +90,9 @@ void ft_main(char **argv, int argc, char **paths, t_aux auxs)
9490
ft_separate_cmds(&p, &auxs, argc, argv);
9591
auxs.i++;
9692
p.args[auxs.cmd][++auxs.args] = 0;
93+
p.args[auxs.cmd] = remove_quotes_in_array(p.args[auxs.cmd]);
9794
auxs.cmd++;
9895
}
99-
*p.args = remove_quotes_in_array(*p.args);
10096
if (p.paths[0])
10197
{
10298
if (!ft_do_commands(&p, auxs.pipes + 1))

src/executer_utils3.c

+21
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,24 @@ void do_call(t_pipex *p, t_aux auxs, char **argv, int argc)
6868
ft_reserve_mem(p, auxs.pipes + 1, ft_count_max_args(argv, argc, auxs.i));
6969
ft_count_args(argv, argc, p);
7070
}
71+
72+
void if_no_builtin(t_pipex *p, t_aux *auxs, char **argv, char **paths)
73+
{
74+
char *temp;
75+
char *temp2;
76+
77+
temp = ft_strdup(argv[auxs->i]);
78+
temp2 = NULL;
79+
if (temp[0] == '"' && temp[ft_strlen(temp) - 1] == '"')
80+
temp2 = ft_strtrim(temp, "\"");
81+
else if (temp[0] == '\'' && temp[ft_strlen(temp) - 1] == '\'')
82+
temp2 = ft_strtrim(temp, "'");
83+
if (temp2)
84+
{
85+
p->paths[auxs->cmd] = ft_check_comm(paths, temp2, p, auxs->cmd);
86+
free(temp2);
87+
}
88+
else
89+
p->paths[auxs->cmd] = ft_check_comm(paths, temp, p, auxs->cmd);
90+
free(temp);
91+
}

src/ft_do_commands.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int ft_do_command(t_pipex *p, int fd[2], int fd2[2])
8181
;
8282
close(fd2[1]);
8383
if (WIFEXITED(status))
84-
g_global.exit_status = WEXITSTATUS(status);
84+
g_global.exit_status = 127;
8585
}
8686
return (1);
8787
}
@@ -109,7 +109,7 @@ int ft_do_one_command(t_pipex *p)
109109
while (waitpid(pid, &status, 0) != pid)
110110
;
111111
if (WIFEXITED(status))
112-
g_global.exit_status = WEXITSTATUS(status);
112+
g_global.exit_status = 127;
113113
}
114114
return (1);
115115
}

src/ft_do_commands_first_last.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ int ft_do_last_comm(t_pipex *p, int fd[2], pid_t pid)
7171
;
7272
close(fd[1]);
7373
if (WIFEXITED(status))
74-
g_global.exit_status = WEXITSTATUS(status);
74+
g_global.exit_status = 127;
7575
}
7676
}
7777
if (!ft_do_else(p, fd))

src/ft_parser.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ char **ft_parser(char *prompt)
4545
{
4646
char **cmd;
4747

48-
prompt = ft_call_replace(prompt);
48+
//prompt = ft_call_replace(prompt);
4949
cmd = ft_cmdtrim(prompt);
50-
if (prompt[ft_strlen(prompt) - 1] == ' ')
50+
if (prompt[0] && prompt[ft_strlen(prompt) - 1] == ' ')
5151
cmd = ft_charpp_del_back(cmd);
5252
cmd = ft_expand_vars(cmd);
53-
free(prompt);
5453
return (cmd);
5554
}
5655

@@ -73,7 +72,7 @@ char **ft_cmdtrim(char *prompt)
7372
ret = NULL;
7473
while (i < (int)ft_strlen(prompt))
7574
{
76-
while (prompt[i] == ' ')
75+
while (prompt[0] && prompt[i] == ' ')
7776
i++;
7877
if (prompt[i] == DQ)
7978
aux = ft_call_dq(prompt, &i);

src/main.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ysingh <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/05/31 18:59:03 by ysingh #+# #+# */
9-
/* Updated: 2023/08/14 17:48:27 by ysingh ### ########.fr */
9+
/* Updated: 2023/08/16 21:25:00 by ysingh ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -25,12 +25,12 @@ int main(int argc, char **argv __attribute__((unused)), char **envp)
2525
while (1)
2626
{
2727
line = ft_readline();
28-
add_history(line);
2928
if (ft_check_cmd(line))
3029
{
3130
cmds = ft_parser(line);
3231
ft_executer(cmds, g_global.path);
3332
ft_charppfree(cmds);
33+
free(line);
3434
}
3535
else
3636
{
@@ -77,22 +77,22 @@ char *ft_get_var(char *cmd)
7777
{
7878
t_master mas;
7979

80-
mas.count.i = 0;
81-
mas.strs.cpy = ft_strdup(cmd);
82-
if ((cmd[0] == SQ && cmd[ft_strlen(cmd) - 1] == SQ)
83-
|| !ft_strchr(mas.strs.cpy, '$'))
80+
init_all(&mas, cmd);
81+
if (cmd[0] == SQ && condition2(cmd, mas))
8482
return (mas.strs.cpy);
8583
while (mas.strs.cpy[mas.count.i])
8684
{
85+
if (condition3(mas))
86+
{
87+
if_dollar_int(&mas);
88+
continue ;
89+
}
8790
mas.strs.aux = ft_strchr(mas.strs.cpy + mas.count.i, '$');
8891
if (!mas.strs.aux)
8992
break ;
9093
mas.count.j = ft_get_index(mas.strs.cpy,
9194
ft_strchr(mas.strs.cpy + mas.count.i, '$'));
92-
if (ft_isspace(mas.strs.aux[1]) || mas.strs.aux[1] == '\0'
93-
|| mas.strs.aux[1] == '$' || mas.strs.aux[1] == '='
94-
|| mas.strs.aux[1] == '\"' || mas.strs.aux[1] == '\''
95-
|| mas.strs.aux[1] == '?' || mas.strs.aux[1] == '|')
95+
if (ft_isspace(mas.strs.aux[1]) || condition(mas))
9696
{
9797
mas.count.i++;
9898
continue ;

src/prints.c

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ char *ft_readline(void)
3737
}
3838
if (!line)
3939
handle_eof(line);
40+
else if (line[0])
41+
add_history(line);
4042
return (line);
4143
}
4244

src/utils.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int ft_vr(char *str, int *i)
2323
}
2424
else if (str[*i] == '>')
2525
{
26-
if (str[*i + 1] == '>' || str[*i + 1] == '|')
26+
if (str[*i + 1] == '>')
2727
*i += 2;
2828
else
2929
*i += 1;

0 commit comments

Comments
 (0)