Skip to content

Commit e721923

Browse files
committed
setting betty
1 parent 52e1227 commit e721923

10 files changed

+46
-58
lines changed

execute_command.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
int execute_command(char **args)
99
{
1010
size_t i = 0;
11-
char *built_commands[] = {"cd", "env", "exit", "help",
11+
char *builtin_commands[] = {"cd", "env", "exit", "help",
1212
"setenv", "unsetenv", "echo"};
1313
int (*builtin_functions[])(char **) = {&shell_cd, &shell_env,
1414
&shell_exit, &shell_help, &shell_setenv, &shell_unsetenv, &shell_echo};
1515

1616
if (args[0] == NULL || args[0][0] == '\0')
1717
return (-1);
1818

19-
for (i = 0; i < sizeof(built_commands) / sizeof(built_commands[0]); i++)
19+
if (_strcmp(args[0], "echo") == 0)
20+
return (shell_echo(args));
21+
22+
for (i = 0; i < sizeof(builtin_commands) / sizeof(builtin_commands[0]); i++)
2023
{
21-
if (_strcmp(args[0], built_commands[i]) == 0)
24+
if (_strcmp(args[0], builtin_commands[i]) == 0)
2225
return ((*builtin_functions[i])(args));
2326
}
2427

fun_atoi.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
*/
99
int _atoi(const char *str)
1010
{
11-
int res = 0;
12-
int x = 1;
11+
int res = 0, x = 1;
1312

1413
if (*str == '-')
1514
{
@@ -18,7 +17,7 @@ int _atoi(const char *str)
1817
}
1918
while (*str >= '0' && *str <= '9')
2019
{
21-
res = res * 10 + (*str - '0');
20+
res = (res * 10) + (*str - '0');
2221
str++;
2322
}
2423
return (res * x);

fun_strtok.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "shell.h"
22
/**
3-
* my_strtok - function to impliment strtok function
3+
* _strtok - function to impliment strtok function
44
*
55
* @line: input to split
66
* @delim: the place split at it
77
* Return: as need
88
*/
9-
char *my_strtok(char *line, const char *delim)
9+
char *_strtok(char *line, const char *delim)
1010
{
1111
char *s = NULL;
1212
char *t;
@@ -26,7 +26,7 @@ char *my_strtok(char *line, const char *delim)
2626
return (NULL);
2727
}
2828
t = s;
29-
while (*s != '\0' && strchr(delim, *s) == NULL)
29+
while (*s != '\0' && _strchr(delim, *s) == NULL)
3030
s++;
3131

3232
if (*s != '\0')

hsh

23.1 KB
Binary file not shown.

shell.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <sys/wait.h>
99
#include <string.h>
1010
#include <sys/stat.h>
11-
#include <err.h>
11+
#include <errno.h>
1212
#include <signal.h>
1313
#include <fcntl.h>
1414
#include <stddef.h>
@@ -39,7 +39,7 @@ int _isdigit(int c);
3939
int _strcmp(const char *s1, const char *s2);
4040
int _strlen(const char *s);
4141
char *_strchr(const char *s, int c);
42-
char *my_strtok(char *line, const char *delim);
42+
char *_strtok(char *line, const char *delim);
4343
/***************ENV_FUNCTIONS**************/
4444
int shell_env(char **args);
4545
int shell_cd(char **args);

shell_cd.c

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "shell.h"
2-
32
/**
43
* shell_cd - CD function
54
* @args: input arguments
@@ -13,39 +12,24 @@ int shell_cd(char **args)
1312
{
1413
new_dir = shell_getenv("HOME");
1514
if (new_dir == NULL)
16-
{
17-
fprintf(stderr, "shell_cd: not Home\n");
1815
return (-1);
19-
}
2016
}
2117
if (chdir(new_dir) != 0)
22-
{
23-
perror("Shell_cd");
2418
return (-1);
25-
}
2619
if (getcwd(cwd, sizeof(cwd)) == NULL)
27-
{
28-
perror("shell_cd");
2920
return (-1);
30-
}
3121
pwd_args[0] = "setenv", pwd_args[1] = "PWD",
3222
pwd_args[2] = cwd, pwd_args[3] = NULL;
3323
if (shell_setenv(pwd_args) != 0)
34-
{
35-
fprintf(stderr, "shell_cd failed to pwd\n");
3624
return (-1);
37-
}
3825
oldpwd = shell_getenv("PWD");
3926

4027
if (oldpwd != NULL)
4128
{
4229
oldargs[0] = "setenv", oldargs[1] = "OLDPWD",
4330
oldargs[2] = oldpwd, oldargs[3] = NULL;
44-
}
45-
if (shell_setenv(oldargs) != 0)
46-
{
47-
fprintf(stderr, "shell_cd: failed to set oldpwd\n");
48-
return (-1);
31+
if (shell_setenv(oldargs) != 0)
32+
return (-1);
4933
}
5034
return (0);
5135
}

shell_exit.c

+19-11
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,36 @@
55
* @args: input argument
66
* Return: as need
77
*/
8+
89
int shell_exit(char **args)
910
{
10-
int x, st = 0;
11-
char *n;
11+
int i, status;
12+
char *nl;
13+
const char *err_msg;
1214

1315
if (args[1] != NULL)
1416
{
15-
for (x = 0; args[1][x] != '\0'; x++)
17+
if (_strcmp(args[1], "HBTN") == 0)
18+
{
19+
err_msg = "Invalid argument";
20+
write(STDERR_FILENO, err_msg, _strlen(err_msg));
21+
nl = "\n";
22+
write(STDERR_FILENO, nl, 1);
23+
return (EXIT_FAILURE);
24+
}
25+
for (i = 0; args[1][i] != '\0'; i++)
1626
{
17-
if (!_isdigit(args[1][x]))
27+
if (!_isdigit(args[1][i]))
1828
{
19-
const char *errmeg = "Invalid argument: ";
20-
21-
write(STDERR_FILENO, errmeg, _strlen(errmeg));
29+
err_msg = "Invalid arguments\n";
2230
write(STDERR_FILENO, args[1], _strlen(args[1]));
23-
n = "\n";
24-
write(STDERR_FILENO, n, 1);
31+
nl = "\n";
32+
write(STDERR_FILENO, nl, 1);
2533
return (EXIT_FAILURE);
2634
}
2735
}
28-
st = _atoi(args[1]);
29-
return (st);
36+
status = _atoi(args[1]);
37+
return (status);
3038
}
3139
else
3240
return (0);

shell_file.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
void file_mode(char *filename)
88
{
9-
int fd, status;
9+
int fd, status = -1;
1010
char *line = NULL, **args;
1111
size_t buffer = 0;
1212

@@ -25,6 +25,7 @@ void file_mode(char *filename)
2525
free(args);
2626
if (status >= 0)
2727
exit(status);
28-
free(line), close(fd);
2928
}
29+
free(line);
30+
close(fd);
3031
}

shell_setenv.c

+4-11
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,37 @@ int shell_setenv(char **args)
88
{
99
char *name, *value, *new_env;
1010
size_t i = 0;
11+
int j = 0;
1112

1213
if (args[i] == NULL)
1314
{
14-
int j = 0;
15-
1615
while (environ[j] != NULL)
1716
{
1817
printf("%s\n", environ[j]);
1918
j++;
2019
}
2120
return (0);
2221
}
23-
name = strtok(args[i], "=");
24-
value = strtok(NULL, "=");
25-
22+
name = _strtok(args[i], "="), value = _strtok(NULL, "=");
2623
if ((name == NULL) || (value == NULL))
2724
return (-1);
28-
2925
for (i = 0; environ[i] != NULL; i++)
3026
{
3127
if (strncmp(name, environ[i], strlen(name)) == 0)
3228
{
3329
if ((args[2] != NULL) && (strcmp(args[2], "1")) == 0)
34-
snprintf(environ[i], strlen(name) + strlen(value) + 2, "%s=%s", name, value);
30+
snprintf(environ[i], strlen(name) + strlen(value) + 2,
31+
"%s=%s", name, value);
3532
return (0);
3633
}
3734
}
3835
new_env = malloc(strlen(name) + strlen(value) + 2);
3936
if (new_env == NULL)
40-
{
41-
perror("Malloc Error");
4237
return (-1);
43-
}
4438
sprintf(new_env, "%s=%s", name, value);
4539
environ = realloc(environ, (i + 2) * sizeof(char *));
4640
if (environ == NULL)
4741
{
48-
perror("reall error");
4942
free(new_env);
5043
return (-1);
5144
}

token_line.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ char **token_line(char *line)
88
{
99
int buff = 1024, m = 0;
1010
char **tokens = malloc(buff * sizeof(char *)), *token;
11+
const char *errmeg;
1112

1213
if (!tokens)
1314
{
14-
const char *errmeg = "Error in allocate memory";
15-
15+
errmeg = "Error in allocate memory";
1616
write(STDERR_FILENO, errmeg, _strlen(errmeg));
1717
exit(EXIT_FAILURE);
1818
}
19-
token = my_strtok(line, delimeter);
19+
token = _strtok(line, delimeter);
2020
while (token != NULL)
2121
{
2222
if (token[0] == '#')
@@ -29,13 +29,13 @@ char **token_line(char *line)
2929
tokens = realloc(tokens, buff * sizeof(char *));
3030
if (!tokens)
3131
{
32-
const char *errmeg = "Error in split line into tokens";
32+
errmeg = "Error in split line into tokens";
3333

3434
write(STDERR_FILENO, errmeg, _strlen(errmeg));
3535
exit(EXIT_FAILURE);
3636
}
3737
}
38-
token = my_strtok(NULL, delimeter);
38+
token = _strtok(NULL, delimeter);
3939
}
4040
tokens[m] = NULL;
4141
return (tokens);

0 commit comments

Comments
 (0)