-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.c
170 lines (152 loc) · 2.73 KB
/
environment.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "main.h"
/**
* path_cmd - Search In $PATH For Executable Command
* @cmd: Parsed Input
* Return: 1 Failure 0 Success.
*/
int path_cmd(char **cmd)
{
char *path, *value, *cmd_path;
struct stat buf;
path = _getenv("PATH");
value = _strtok(path, ":");
while (value != NULL)
{
cmd_path = build(*cmd, value);
if (stat(cmd_path, &buf) == 0)
{
*cmd = _strdup(cmd_path);
free(cmd_path);
free(path);
return (0);
}
free(cmd_path);
value = _strtok(NULL, ":");
}
free(path);
return (1);
}
/**
* build - Build Command
* @token: Executable Command
* @value: Directory Containing Command
*
* Return: Parsed Full Path Of Command Or NULL Case Failed
*/
char *build(char *token, char *value)
{
char *cmd;
size_t len;
len = _strlen(value) + _strlen(token) + 2;
cmd = malloc(sizeof(char) * len);
if (cmd == NULL)
{
return (NULL);
}
memset(cmd, 0, len);
cmd = _strcat(cmd, value);
cmd = _strcat(cmd, "/");
cmd = _strcat(cmd, token);
return (cmd);
}
/**
* _getenv - Gets The Value Of Environment Variable By Name
* @name: Environment Variable Name
* Return: The Value of the Environment Variable Else NULL.
*/
char *_getenv(char *name)
{
size_t nl, vl;
char *value;
int i, x, j;
nl = _strlen(name);
for (i = 0 ; environ[i]; i++)
{
if (_strncmp(name, environ[i], nl) == 0)
{
vl = _strlen(environ[i]) - nl;
value = malloc(sizeof(char) * vl);
if (!value)
{
free(value);
perror("unable to alloc");
return (NULL);
}
j = 0;
for (x = nl + 1; environ[i][x]; x++, j++)
{
value[j] = environ[i][x];
}
value[j] = '\0';
return (value);
}
}
return (NULL);
}
/**
* print_hist - Display History Of commands
* @c:Parsed Command
* @s:status Of Last execute
* Return: 0 success -1 Fail
*/
int print_hist(__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);
PRINT(er);
free(er);
PRINT(" ");
PRINT(line);
}
if (line)
free(line);
fclose(fp);
return (0);
}
/**
* print_echo - execute Normal Echo
* @cmd: Parsed Command
* Return: 0 success -1 Fail
*/
int print_echo(char **cmd)
{
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
if (execve("/bin/echo", cmd, environ) == -1)
{
return (-1);
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
return (-1);
}
else
{
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return (1);
}