Skip to content

Commit 5a37bd3

Browse files
committed
simple shell files
1 parent 306c57e commit 5a37bd3

File tree

9 files changed

+17
-69
lines changed

9 files changed

+17
-69
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ DL_getline.c
22
DL_strtok.c
33
.DL_getline.c.swp
44
/test
5+
test
6+
./sample/DL_locator.c
7+
sample/hsh
8+
sample

_execute.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* execute_command - function to handle command execution based on user input
55
* @get_address: input line from user
6-
* @env: environment variable
6+
*
77
* Return: output executed command else -1
88
*/
99
int execute_command(char *get_address)
@@ -17,14 +17,14 @@ int execute_command(char *get_address)
1717
{
1818
perror("Error ");
1919
_free_memo(tokens);
20-
return (ex);
20+
return (-1);
2121
}
2222
if (_strcmp(tokens[0], "exit") == 0)
2323
{
2424
if (tokens[1] != NULL)
2525
status = _atoi(tokens[1]);
2626
_free_memo(tokens);
27-
free(command_path);
27+
free(get_address);
2828
exit(status);
2929
}
3030
if (_strcmp(tokens[0], "env") == 0)
@@ -39,7 +39,6 @@ int execute_command(char *get_address)
3939
{
4040
perror("command not found");
4141
_free_memo(tokens);
42-
free(command_path);
4342
ex = 127;
4443
return (ex);
4544
}

_getenv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ char *_getenv(const char *str)
2828
value = _strdup(environ[l] + len + 1);
2929
if (value == NULL)
3030
{
31-
free(value);
31+
/*free(value);*/
3232
perror("Error: ");
3333
return (NULL);
3434
}

_locator.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@
77
*/
88
char *locate_path(char *com_val)
99
{
10-
char *read_path = NULL,/*copy_of_path,*/ *file = NULL, *token_to_path;
10+
char *read_path = NULL, *file = NULL, *token_to_path;
1111
size_t length, dir_length;
1212
struct stat buff;
1313

1414
if (stat(com_val, &buff) == 0)
1515
{
16-
/*free(read_path);*/
17-
return (com_val);
16+
file = _strdup(com_val);
17+
return (file);
1818
}
1919
read_path = _getenv("PATH");
2020
if (!read_path)
2121
return (NULL);
22-
/*copy_of_path = _strdup( read_path);
23-
free(read_path);*/
2422
token_to_path = strtok(read_path, ":");
2523

2624
while (token_to_path != NULL)
@@ -45,6 +43,5 @@ char *locate_path(char *com_val)
4543
token_to_path = strtok(NULL, ":");
4644
}
4745
free(read_path);
48-
/*free(file);*/
4946
return (NULL);
5047
}

_pid.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*
99
* Return: 0 when succeful else 1
1010
*/
11-
1211
int execute_and_wait(char *command_path, char **tokens, int *e_status)
1312
{
1413
pid_t pid;

hsh

8 Bytes
Binary file not shown.

my_main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* whenever the function is called upon.
66
* @argc: Number of commandline arguments.
77
* @argv: Pointer array of strings containing commandline arguments.
8-
* @env: An array of strings containing environment variables.
98
*
109
* Return: 0 when successfull.
1110
*/
@@ -31,7 +30,10 @@ int main(int argc, char __attribute__((__unused__)) *argv[])
3130
{
3231
string_line = execute_command(get_address);
3332
if (string_line != 0)
33+
{
34+
free(get_address);
3435
return (string_line);
36+
}
3537
}
3638
}
3739
free(get_address);

sample/DL_locator.c

Lines changed: 0 additions & 54 deletions
This file was deleted.

tokenize.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ char **tokenize(char *buffer, const char *delim)
2727
while (token != NULL && token_count < MAX_TOKEN)
2828
{
2929
m_token[token_count] = _strdup(token);
30-
/*if (m_token[token_count] == NULL)*/
31-
if (!m_token[token_count])
30+
if (m_token[token_count] == NULL)
31+
/*if (!m_token[token_count])*/
3232
{
3333
perror("Memory allocation failed");
3434
for (i = 0; i < token_count; i++)
3535
{
3636
free(m_token[i]);
3737
}
3838
free(m_token);
39+
m_token = NULL;
3940
return (NULL);
4041
}
4142
token_count++;

0 commit comments

Comments
 (0)