-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_related.c
163 lines (139 loc) · 3.18 KB
/
path_related.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
#include "shell.h"
/**
* findPath - Determine if the given command can be executed.
* @cmd: Command to be checked.
* Return: Full path of the command or NULL if not found in PATH.
*/
char *findPath(char *cmd)
{
char *environmentPath = _getenv("PATH");
char *segment, *segmentCopy, *completePath, *envDuplicate = NULL;
if (!environmentPath)
return (NULL);
envDuplicate = _strdup(environmentPath);
segment = _strtok(envDuplicate, ":");
while (segment)
{
segmentCopy = malloc(_strlen(segment) + _strlen(cmd) + 2);
if (!segmentCopy)
{
perror("malloc");
return (NULL);
}
segmentCopy = _strcpy(segmentCopy, segment);
segmentCopy = _strcat(segmentCopy, "/");
completePath = _strcat(segmentCopy, cmd);
if (access(completePath, X_OK) == 0)
{
free(envDuplicate);
return (completePath);
}
free(segmentCopy);
segment = _strtok(NULL, ":");
}
free(envDuplicate);
return (NULL);
}
/**
* userInput - Prints a prompt, captures and returns user input.
* @commands: Struct holding command data.
* @arguments: Array of tokenized arguments.
* Return: Pointer to the captured command string.
*/
char *userInput(cmd_t *commands, cmd_t *arguments)
{
char *inputCopy;
char *input = malloc(128);
ssize_t readSize;
int bufSize = 20;
buff_t bufInfo;
if (isatty(STDIN_FILENO))
{
write(STDOUT_FILENO, "$ ", 2);
fflush(stdout);
}
bufInfo.index = 0;
bufInfo.count = 0;
readSize = _getline(&input, &bufSize, STDIN_FILENO, &bufInfo);
if (readSize == 0)
{
free(input);
free(commands);
free(arguments);
exit(0);
}
if (readSize == 1)
{
free(input);
return (NULL);
}
if (input[readSize - 1] == '\n')
input[readSize - 1] = '\0';
input[readSize] = '\0';
inputCopy = _strdup(input);
free(input);
return (inputCopy);
}
/**
* runCommand - Initiates the command execution process.
* @arguments: Array of arguments to pass.
* @commands: Command-related data.
* @cmdCount: Number of commands processed.
* Return: Nothing.
*/
void runCommand(cmd_t *arguments, cmd_t *commands, int cmdCount)
{
pid_t pid;
int procStatus;
int iter;
pid = fork();
if (pid == -1)
perror("Forking process failed");
if (pid == 0)
{
if (execve(arguments->args[0], arguments->args, environ) == -1)
perror("Command execution failed");
}
else
{
wait(&procStatus);
if (WEXITSTATUS(procStatus) != 0)
{
arguments->foundPath = 0;
if (commands->arg_count == cmdCount && arguments->pipe)
{
{
free_cmd(arguments);
free_cmd(commands);
free(environ);
exit(WEXITSTATUS(procStatus));
}
}
for (iter = 0; arguments->args[iter]; iter++)
{
free(arguments->args[iter]);
arguments->args[iter] = NULL;
}
}
}
}
/**
* tokenize - Breaks down a command string into tokens.
* @cmd: Command to tokenize.
* @argumentsArr: Array to store tokenized strings.
* @separator: Delimiter to use.
* Return: Number of tokens, or -1 if "exit" command is issued.
*/
int tokenize(char *cmd, char *argumentsArr[], char *separator)
{
int idx = 0;
char *piece;
piece = _strtok(cmd, separator);
while (piece && idx < MAX_ARGS - 1)
{
argumentsArr[idx++] = _strdup(piece);
piece = _strtok(NULL, separator);
}
argumentsArr[idx] = NULL;
return (idx);
}