-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmore_builtins.c
131 lines (123 loc) · 2.37 KB
/
more_builtins.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "shell.h"
/**
* show_history - Display history to the shell on the stdout
* @c:Parsed Command
* @s:Statue Of Last Excute
* Return: 0 Succes -1 Fail
*/
int show_history(__attribute__((unused))char **c, __attribute__((unused))int s)
{
char *filename = ".simple_shell_history";
FILE *fp;
char *line = NULL;
size_t len = 0;
int counter = 0;
char *er;
fp = fopen(filename, "r");
if (fp == NULL)
{
return (-1);
}
while ((getline(&line, &len, fp)) != -1)
{
counter++;
er = _itoa(counter);
PRINTER(er);
free(er);
PRINTER(" ");
PRINTER(line);
}
if (line)
free(line);
fclose(fp);
return (0);
}
/**
* execute_cmd - a command
* @cmd: Parsed Command
* Return: 0 Succes -1 Fail
*/
int execute_cmd(char **cmd)
{
int status, check;
pid_t pid;
char *buff = NULL;
if (*cmd == NULL)
{
return (-1);
}
pid = fork();
if (pid == -1)
{
perror("Error");
return (-1);
}
if (pid == 0)
{
if (_strncmp(*cmd, "./", 2) != 0 && _strncmp(*cmd, "/", 1) != 0)
{
buff = _getpath(cmd[0]);
if (buff != NULL)
cmd[0] = _strdup(buff);
}
check = execve(*cmd, cmd, environ);
if (check == -1)
{
free(cmd);
exit(EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
free(buff);
wait(&status);
if (WIFEXITED(status))
return (WEXITSTATUS(status));
return (-1);
}
/**
* _setenv - function sets or change variable without setenv
* @cmd: Parsed Command
* @er: Statue Of Last Excute
* Return: new value of the variable
*/
int _setenv(char **cmd, __attribute__((unused))int er)
{
char *new_name = cmd[1];
char *new_value = cmd[2];
char *env_string = build(new_name, new_value, "=");
if (_putenv(env_string) != 0)
{
free(env_string);
return (0);
}
return (-1);
}
/**
* _unsetenv - function removes the set environment variable
* @cmd: Parsed Command
* @er: Statue Last Command Excuted
* Return: 0 on sucess otherwise -1
*/
int _unsetenv(char **cmd, __attribute__((unused))int er)
{
char *new_name = cmd[1];
int name_length = _strlen(new_name);
int i, j;
char end_char = '\0';
if ((new_name == NULL) || (new_name[0] == end_char))
return (-1);
/* iterate throught the variables*/
for (i = 0; environ[i] != NULL; i++)
{
if (_strncmp(new_name, environ[i], name_length) == 0)
{
/* shift all variables one value back*/
for (j = i; environ[j] != NULL; j++)
{
environ[j] = environ[j + 1];
}
return (0);
}
}
return (-1);
}