Skip to content

Commit 915a88c

Browse files
committed
No Memory Leak Betty Doc And Style Fine System Command Not Allowed Getline Standard in Case of File
1 parent 7cc6af2 commit 915a88c

12 files changed

+341
-246
lines changed

bulltin.c

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
#include "shell.h"
22
/**
3-
* exit_bul - exit shell
4-
*@cmd:command
5-
* Return:int
3+
* exit_bul - Exit Statue Shell
4+
* @cmd: Parsed Command
5+
* @input: User Input
6+
* @env: Environment Variables
7+
* Return: Void (Exit Statue)
68
*/
7-
int exit_bul(char **cmd)
9+
void exit_bul(char **cmd, char *input, char **env)
810
{
911
int statue, i = 0;
1012

1113
if (cmd[1] == NULL)
1214
{
15+
free(input);
1316
free(cmd);
17+
free_env(env);
1418
exit(errno);
1519
}
1620
while (cmd[1][i])
1721
{
1822
if (_isalpha(cmd[1][i++]) < 0)
1923
{
2024
perror("illegal number");
21-
return (-1);
2225
}
2326
}
2427
statue = _atoi(cmd[1]);
28+
free(input);
29+
free(cmd);
30+
free_env(env);
2531
exit(statue);
2632
}
2733

2834

2935
/**
30-
* change_dir - change directory command
31-
* @cmd: command
32-
* Return: 1 in success
36+
* change_dir - Change Dirctorie
37+
* @cmd: Parsed Command
38+
* Return: 0 Succes 1 Failed (For Old Pwd Always 0 Case No Old PWD)
3339
*/
3440
int change_dir(char **cmd)
3541
{
@@ -40,7 +46,10 @@ int change_dir(char **cmd)
4046
if (cmd[1] == NULL || _strcmp(cmd[1], "~") == 0)
4147
value = chdir(getenv("HOME"));
4248
else if (_strcmp(cmd[1], "-") == 0)
43-
value = chdir(getenv("OLDPWD"));
49+
{
50+
chdir(getenv("OLDPWD"));
51+
value = 0;
52+
}
4453
else if (_strcmp(cmd[1], "..") == 0)
4554
value = chdir(cmd[1]);
4655
else if (_strcmp(cmd[1], ".") == 0)
@@ -56,7 +65,10 @@ int change_dir(char **cmd)
5665
value = chdir(cmd[1]);
5766

5867
if (value == -1)
68+
{
69+
perror("hsh");
5970
return (-1);
71+
}
6072
else if (value != -1)
6173
{
6274
getcwd(cwd, sizeof(cwd));
@@ -66,9 +78,9 @@ int change_dir(char **cmd)
6678
return (0);
6779
}
6880
/**
69-
* dis_env - display envirment variable bultin
70-
*@cmd:command
71-
* Return:int
81+
* dis_env - Display Enviroment Variable
82+
*@cmd:Parsed Command
83+
* Return:Always 0
7284
*/
7385
int dis_env(__attribute__((unused)) char **cmd)
7486
{
@@ -84,10 +96,9 @@ size_t i;
8496
return (0);
8597
}
8698
/**
87-
* display_help - builtin to display help
88-
*
89-
* @cmd:command to excute
90-
* Return: int
99+
* display_help - Displaying Help For Builtin
100+
* @cmd:Parsed Command
101+
* Return: 0 Succes -1 Fail
91102
*/
92103
int display_help(char **cmd)
93104
{
@@ -113,10 +124,10 @@ int display_help(char **cmd)
113124
return (0);
114125
}
115126
/**
116-
* echo_bul - echo bultin
127+
* echo_bul - Excute Echo Cases
117128
*
118-
* @cmd: commande line
119-
* Return: always 0
129+
* @cmd: Parsed Command
130+
* Return: Always 0 Or Excute Normal Echo
120131
*/
121132
int echo_bul(char **cmd)
122133
{

excute.c

+62-52
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,14 @@
11
#include "shell.h"
2-
/**
3-
* check_cmd - check for command and excute
4-
* @cmd:command to check and excute
5-
*@counter: nuber of exctution of shell
6-
*Return: 1 success -1 if failed
7-
*/
8-
int check_cmd(char **cmd, int counter)
9-
{
10-
pid_t pid;
11-
int status;
122

13-
if (cmd == NULL)
14-
{
15-
return (-1);
16-
}
17-
pid = fork();
18-
if (pid == 0)
19-
{
20-
if (execve(cmd[0], cmd, environ) == -1)
21-
{
22-
print_error(cmd[0], counter);
23-
return (-1);
24-
}
25-
return (0);
26-
}
27-
else if (pid < 0)
28-
{
29-
return (-1);
30-
}
31-
else
32-
{
33-
do {
34-
waitpid(pid, &status, WUNTRACED);
35-
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
36-
}
37-
return (0);
38-
}
393
/**
40-
* signal_to_handel - handle ^C
41-
*
42-
* @sig: int
43-
*/
44-
void signal_to_handel(int sig)
45-
{
46-
if (sig == SIGINT)
47-
{
48-
PRINTER("\n$ ");
49-
}
50-
}
51-
/**
52-
* handle_builtin - excute builtin if found or excute commandes
53-
* @cmd: command to check
54-
* Return: int
4+
* handle_builtin - Handle Builtin Command
5+
* @cmd: Parsed Command
6+
* Return: -1 Fail 0 Succes (Return :Excute Builtin)
557
*/
568

579
int handle_builtin(char **cmd)
5810
{
5911
bul_t bil[] = {
60-
{"exit", exit_bul},
6112
{"cd", change_dir},
6213
{"env", dis_env},
6314
{"help", display_help},
@@ -77,3 +28,62 @@ int handle_builtin(char **cmd)
7728
}
7829
return (-1);
7930
}
31+
/**
32+
* check_cmd - Excute Simple Shell Command (Fork,Wait,Excute)
33+
*
34+
* @cmd:Parsed Command
35+
* @input: User Input
36+
* @env: Envirment Variable
37+
* @c:Shell Excution Time Case of Command Not Found
38+
* Return: 1 Case Command Null -1 Wrong Command 0 Command Excuted
39+
*/
40+
int check_cmd(char **cmd, char *input, char **env, int c)
41+
{
42+
int status;
43+
pid_t pid;
44+
45+
if (*cmd == NULL)
46+
{
47+
return (1);
48+
}
49+
50+
pid = fork();
51+
if (pid == -1)
52+
{
53+
perror("Error");
54+
return (EXIT_FAILURE);
55+
}
56+
57+
if (pid == 0)
58+
{
59+
if (_strncmp(*cmd, "./", 2) != 0)
60+
{
61+
path_cmd(cmd);
62+
}
63+
64+
if (execve(*cmd, cmd, env) == -1)
65+
{
66+
print_error(cmd[0], c);
67+
free(input);
68+
free(cmd);
69+
free_env(env);
70+
exit(EXIT_FAILURE);
71+
}
72+
free_env(env);
73+
return (EXIT_SUCCESS);
74+
}
75+
wait(&status);
76+
return (1);
77+
}
78+
/**
79+
* signal_to_handel - Handle ^C
80+
* @sig:Captured Signal
81+
* Return: Void
82+
*/
83+
void signal_to_handel(int sig)
84+
{
85+
if (sig == SIGINT)
86+
{
87+
PRINTER("\n$ ");
88+
}
89+
}

excute_file.c

+87-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,100 @@
11
#include "shell.h"
22
/**
3-
* read_file - read command from a file
4-
* @filename:file name char
3+
* read_file - Read Command From File
4+
* @filename:Filename
55
* Return: -1 or 0
66
*/
77

8-
int read_file(char **filename)
8+
void read_file(char *filename)
9+
{
10+
FILE *fp;
11+
char *line = NULL;
12+
size_t len = 0;
13+
ssize_t read;
14+
char *envi[50];
15+
int counter = 0;
16+
17+
fp = fopen(filename, "r");
18+
if (fp == NULL)
19+
{
20+
exit(EXIT_FAILURE);
21+
}
22+
creat_envi(envi);
23+
while ((read = getline(&line, &len, fp)) != -1)
924
{
10-
int fd, rd;
11-
char *buff = malloc(sizeof(char) * BUFSIZ);
12-
char **line;
25+
counter++;
26+
treat_file(line, envi, counter, fp);
27+
28+
}
29+
if (line)
30+
free(line);
31+
free_env(envi);
32+
fclose(fp);
33+
exit(0);
34+
}
35+
/**
36+
* treat_file - PARSE Check Command Fork Wait Excute in Line of File
37+
* @line: Line From A File
38+
* @envi: Enviroment Variable
39+
* @counter:Error Counter
40+
* @fp:File Descriptor
41+
* Return : Excute A line void
42+
*/
43+
void treat_file(char *line, char **envi, int counter, FILE *fp)
44+
{
45+
char **cmd;
46+
47+
cmd = parse_cmd(line);
1348

14-
fd = open(filename[1], O_RDWR);
15-
if (fd == -1)
49+
if (_strcmp(cmd[0], "exit") == 0)
50+
{
51+
exit_bul_for_file(cmd, line, envi, fp);
52+
}
53+
else if ((check_builtin(cmd)) == 0)
1654
{
17-
perror("hsh ");
18-
exit(-1);
55+
handle_builtin(cmd);
56+
free(cmd);
1957
}
58+
else
59+
{
60+
check_cmd(cmd, line, envi, counter);
61+
free(cmd);
62+
}
63+
}
64+
/**
65+
* exit_bul_for_file - Exit Shell Case Of File
66+
* @line: Line From A File
67+
* @envi: Enviroment Variable
68+
* @cmd: Parsed Command
69+
* @fd:File Descriptor
70+
* Return : Case Of Exit in A File Line
71+
*/
72+
void exit_bul_for_file(char **cmd, char *line, char **envi, FILE *fd)
73+
{
74+
int statue, i = 0;
2075

21-
rd = read(fd, buff, BUFSIZE);
22-
if (rd == -1)
76+
if (cmd[1] == NULL)
77+
{
78+
free(line);
79+
free(cmd);
80+
free_env(envi);
81+
fclose(fd);
82+
exit(errno);
83+
}
84+
while (cmd[1][i])
85+
{
86+
if (_isalpha(cmd[1][i++]) < 0)
2387
{
24-
return (0);
88+
perror("illegal number");
2589
}
26-
line = parse_cmd(buff);
27-
path_cmd(line);
28-
handle_builtin(line);
29-
exit(0);
3090
}
91+
statue = _atoi(cmd[1]);
92+
free(line);
93+
free(cmd);
94+
free_env(envi);
95+
fclose(fd);
96+
exit(statue);
97+
98+
99+
100+
}

get_line.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#include "shell.h"
33

44
/**
5-
*_getline - read the input
6-
*Return: The line readed
5+
* _getline - Read The Input By User From Stdin
6+
* @envi: Enviroment Variable To Free Case Of ^D
7+
* Return: Input
78
*/
8-
char *_getline()
9+
char *_getline(char **envi)
910
{
1011
int i, buffsize = 1024, rd;
1112
char c = 0;
@@ -14,7 +15,6 @@ char *buff = malloc(buffsize);
1415
if (buff == NULL)
1516
{
1617
free(buff);
17-
buff = NULL;
1818
return (NULL);
1919
}
2020

@@ -25,6 +25,7 @@ char *buff = malloc(buffsize);
2525
if (rd == 0)
2626
{
2727
free(buff);
28+
free_env(envi);
2829
exit(EXIT_SUCCESS);
2930
}
3031
buff[i] = c;

0 commit comments

Comments
 (0)