Skip to content

Commit 0fd6c40

Browse files
author
Michail Chatzipanagiotou
committed
norminetted.
1 parent 65364c9 commit 0fd6c40

12 files changed

+132
-84
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# By: mchatzip <[email protected]> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2021/06/18 18:00:18 by mchatzip #+# #+# #
9-
# Updated: 2022/02/01 18:10:06 by mchatzip ### ########.fr #
9+
# Updated: 2022/02/07 13:09:52 by mchatzip ### ########.fr #
1010
# #
1111
# **************************************************************************** #
1212

@@ -17,7 +17,8 @@ LIBFT_PATH = libft/
1717
LIBFT_LIB = libft.a
1818

1919
SRCS_FILES = minishell.c commands.c commands2.c history.c utils.c utils2.c utils3.c utils4.c utils5.c echoutils.c \
20-
utils6.c exportutils.c exportutils2.c exportutils3.c pipenredirutils.c utils7.c utils8.c pipeutils.c
20+
utils6.c exportutils.c exportutils2.c exportutils3.c pipenredirutils.c utils7.c utils8.c pipeutils.c \
21+
utils9.c utils10.c
2122

2223
SRCS_OBJS = ${SRCS_FILES:.c=.o}
2324

commands.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9-
/* Updated: 2022/02/05 21:19:11 by mchatzip ### ########.fr */
9+
/* Updated: 2022/02/07 13:05:35 by mchatzip ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,43 +16,28 @@ void execexit(char *b, char *buff, char *name, t_nums *n)
1616
{
1717
char **args;
1818
bool num;
19-
size_t i;
2019

2120
args = ft_split(buff, ' ');
22-
i = 0;
23-
num = 0;
2421
if (!args[1])
25-
{
26-
free(args[0]);
27-
free(args);
22+
freedstring(args);
23+
if (!args[1])
2824
exit(0);
29-
}
3025
if (args[2])
3126
{
3227
write(2, "exit: too many arguments\n", 25);
33-
while (args[i])
34-
free(args[i++]);
35-
free(args);
28+
freedstring(args);
3629
return ;
3730
}
38-
while (args[1][i])
39-
if (!ft_isdigit(args[1][i++]))
40-
num = 1;
31+
num = checkifnumeric(args[1]);
4132
if (num == 1 || ft_atoi(args[1]) < 0 || ft_atoi(args[1]) > 255)
4233
{
4334
printf
4435
("exit: argument: '%s' is not numeric or exceeds scope\n", args[1]);
4536
exitall(b, buff, name, n);
46-
free(args[0]);
47-
i = 0;
48-
while (args[i])
49-
free(args[i++]);
50-
free(args);
37+
freedstring(args);
5138
exit(255);
5239
}
53-
free(args[0]);
54-
free(args[1]);
55-
free(args);
40+
freedstring(args);
5641
exit (ft_atoi(args[1]));
5742
}
5843

commands2.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9-
/* Updated: 2022/02/05 19:54:38 by mchatzip ### ########.fr */
9+
/* Updated: 2022/02/07 12:54:51 by mchatzip ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -22,29 +22,18 @@ void execexport(char *b)
2222
b = xportnmskip(b);
2323
i = 0;
2424
c = -1;
25-
dest = malloc(1000);
25+
dest = ft_calloc(1000, 1000);
2626
if (echoerrcheck(b))
2727
return ;
2828
s = processinput(b);
2929
while (s[++c])
3030
{
3131
i = incrementi(i, s[c]);
3232
if (ft_strchr(s[c], '='))
33-
{
34-
if (g_env[i])
35-
free(g_env[i]);
36-
evalquotes(exportout2(dest, s[c]));
37-
ft_bzero(dest, 1000);
38-
g_env[i] = ft_strdup(exportout2(dest, s[c]));
39-
}
33+
xportloop(i, c, s, dest);
4034
i = 0;
4135
}
42-
i = -1;
43-
while (s[++i])
44-
free(s[i]);
45-
free(s);
46-
free(dest);
47-
cleartrash(g_env);
36+
cleanhouse(s, dest);
4837
}
4938

5039
void execunset(char *b)

exportutils.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9-
/* Updated: 2022/02/05 21:01:59 by mchatzip ### ########.fr */
9+
/* Updated: 2022/02/07 12:40:07 by mchatzip ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -63,7 +63,7 @@ static char *handlespacexp(char *b, char *ret)
6363
return (b);
6464
}
6565

66-
static char *handlescharsxp(char *b, char *ret)
66+
char *handlescharsxp(char *b, char *ret)
6767
{
6868
if (*b == ' ')
6969
b = handlespacexp(b, ret);
@@ -96,14 +96,3 @@ char *exportout(char *b)
9696
b = handlescharsxp(b, ret);
9797
return (ret);
9898
}
99-
100-
char *exportout2(char *dest, char *b)
101-
{
102-
if (echoerrcheck(b))
103-
return ("");
104-
while (*b == ' ')
105-
b++;
106-
while (*b)
107-
b = handlescharsxp(b, dest);
108-
return (dest);
109-
}

exportutils3.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9-
/* Updated: 2022/02/05 21:01:36 by mchatzip ### ########.fr */
9+
/* Updated: 2022/02/07 12:53:25 by mchatzip ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,7 +16,7 @@ int incrementi(int i, char *s)
1616
{
1717
char *dest;
1818

19-
dest = malloc(1000);
19+
dest = ft_calloc(1000, 1000);
2020
while (g_env[i]
2121
&& (!namecmp(g_env[i], exportout2(dest, s)) || !g_env[i][0]))
2222
i++;

minishell.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2021/11/16 11:15:48 by mchatzip #+# #+# */
9-
/* Updated: 2022/02/05 21:25:18 by mchatzip ### ########.fr */
9+
/* Updated: 2022/02/07 13:13:01 by mchatzip ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -106,5 +106,11 @@ int handleleftredir(char *b, int i);
106106
void exitall(char *b, char *buff, char *name, t_nums *n);
107107
void freeenvnstruct(t_nums *n);
108108
char *exportout2(char *dest, char *b);
109+
void cleanhouse(char **s, char *dest);
110+
void xportloop(int i, int c, char **s, char *dest);
111+
char *handlescharsxp(char *b, char *ret);
112+
void freedstring(char **s);
113+
bool checkifnumeric(char *s);
114+
void initparseret(char **pr, char *name);
109115

110116
#endif

pipenredirutils.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9-
/* Updated: 2022/02/05 21:25:54 by mchatzip ### ########.fr */
9+
/* Updated: 2022/02/07 13:24:50 by mchatzip ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -63,40 +63,28 @@ void initparse(char *b, t_nums *n)
6363
{
6464
char *buff;
6565
char *name;
66-
int i;
6766
char **pr;
6867

69-
i = -1;
68+
buff = NULL;
7069
if (echoerrcheck(b))
7170
return ;
7271
name = parsecmdname(b);
7372
pr = checkpipesnredirs(b);
7473
if (!*pr)
75-
{
76-
while (++i < 1000)
77-
free(pr[i]);
78-
free(pr);
79-
free(name);
80-
return ;
81-
}
74+
return (initparseret(pr, name));
8275
if (!pr[1])
8376
{
8477
if (checkstrayops(b))
85-
{
86-
while (++i < 1000)
87-
free(pr[i]);
88-
free(pr);
89-
free(name);
90-
return ;
91-
}
78+
return (initparseret(pr, name));
9279
buff = exportout(b);
9380
parseargs(b, buff, name, n);
9481
n->hyphen = 1;
9582
}
96-
//else to be added for pipes/redirections handling
97-
while (pr[++i])
98-
free(pr[i]);
99-
free3(pr, buff, name);
83+
//else
84+
//to be added for pipes/redirections handling
85+
freedstring(pr);
86+
free(buff);
87+
free(name);
10088
}
10189

10290
// for (int k = 0; ret[k]; k++)

utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9-
/* Updated: 2022/02/05 18:36:41 by mchatzip ### ########.fr */
9+
/* Updated: 2022/02/07 12:38:46 by mchatzip ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -32,7 +32,7 @@ void dhandler(int signum)
3232
{
3333
write(1, "\n", 1);
3434
rl_on_new_line();
35-
rl_replace_line("", 0);
35+
//rl_replace_line("", 0);
3636
rl_redisplay();
3737
}
3838
else if (signum == SIGQUIT)

utils10.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* utils10.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9+
/* Updated: 2022/02/07 13:12:45 by mchatzip ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "minishell.h"
14+
15+
void initparseret(char **pr, char *name)
16+
{
17+
freedstring(pr);
18+
free(name);
19+
}

utils3.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9-
/* Updated: 2022/01/20 15:43:09 by mchatzip ### ########.fr */
9+
/* Updated: 2022/02/07 13:21:51 by mchatzip ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -69,7 +69,10 @@ void restoreenv(char *s)
6969

7070
void free3(void *s, void *sp, void *sav)
7171
{
72-
free(s);
73-
free(sp);
74-
free(sav);
72+
if (s)
73+
free(s);
74+
if (sp)
75+
free(sp);
76+
if (sav)
77+
free(sav);
7578
}

utils6.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9-
/* Updated: 2022/01/21 14:55:02 by mchatzip ### ########.fr */
9+
/* Updated: 2022/02/07 13:19:44 by mchatzip ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -88,6 +88,6 @@ char *handlenflag(char *b)
8888
free(buff);
8989
j = 0;
9090
}
91-
free(s);
91+
freedstring(s);
9292
return (b);
9393
}

utils9.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* utils9.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mchatzip <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2021/11/16 11:19:39 by mchatzip #+# #+# */
9+
/* Updated: 2022/02/07 13:04:11 by mchatzip ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "minishell.h"
14+
15+
char *exportout2(char *dest, char *b)
16+
{
17+
if (echoerrcheck(b))
18+
return ("");
19+
while (*b == ' ')
20+
b++;
21+
while (*b)
22+
b = handlescharsxp(b, dest);
23+
return (dest);
24+
}
25+
26+
void cleanhouse(char **s, char *dest)
27+
{
28+
int i;
29+
30+
i = -1;
31+
while (s[++i])
32+
free(s[i]);
33+
free(s);
34+
free(dest);
35+
cleartrash(g_env);
36+
}
37+
38+
void xportloop(int i, int c, char **s, char *dest)
39+
{
40+
if (g_env[i])
41+
free(g_env[i]);
42+
evalquotes(exportout2(dest, s[c]));
43+
ft_bzero(dest, 1000);
44+
g_env[i] = ft_strdup(exportout2(dest, s[c]));
45+
}
46+
47+
void freedstring(char **s)
48+
{
49+
int i;
50+
51+
i = -1;
52+
while (s[i])
53+
free(s[i++]);
54+
free(s);
55+
}
56+
57+
bool checkifnumeric(char *s)
58+
{
59+
size_t i;
60+
bool num;
61+
62+
i = 0;
63+
num = 0;
64+
while (s[i])
65+
if (!ft_isdigit(s[i++]))
66+
num = 1;
67+
return (num);
68+
}

0 commit comments

Comments
 (0)