generated from amakzy/simplenotshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.c
221 lines (187 loc) · 3.37 KB
/
a.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "shell.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX_INPUT_SIZE 1024
#define MAX_TOKENS 100
#define MAX_NUM_DIRS 10
/**
* changeDirectory - built-in command functions
* @args: argument passed to command
*/
void changeDirectory(char **args);
void printEnvVariables(void);
void exitShell(char **args);
/**
* execArgs - executes commands
* @args: parsed input arguments
*/
void execArgs(char **args);
/**
* parseInput - parses input into tokens
* @input: input string to parse
* Return: array of input
*/
char **parseInput(char *input);
/**
* readInput - reads input from stdin
*
* Return: input string
*/
char *readInput();
/**
* handle_SIGINT - handle ctlr +c
*
* @signum: recive the signal
*/
void handle_SIGINT(int signum)
{
printf("Received SIGINT signal %d\n", signum);
fflush(stdout);
}
/**
* main - shell main function
*
* runs in a loop to accept user input, parse it, and execute commands.
* Return: 0
*/
int main(void)
{
char *input;
char **args;
signal(SIGINT, handle_SIGINT);
while (1)
{
/* Print prompt */
printf("> ");
/* Read input */
input = readInput();
/* Parse input into tokens */
args = parseInput(input);
/* Execute command */
execArgs(args);
/* Free parsed input */
free(input);
free(args);
}
}
/**
* findExecutable - to find the exe
* @command: the variable
* Return: exe.
*/
char *findExecutable(char *command)
{
char *path = getenv("PATH");
char *dir;
char *exe = NULL;
dir = strtok(path, ":");
while (dir != NULL)
{
exe = strcat(dir, "/");
exe = strcat(exe, command);
if (access(exe, X_OK) == 0)
{
return (exe);
}
dir = strtok(NULL, ":");
}
return (exe);
}
/**
* execArgs - executes commands
*
* @args: parsed input argument
*
* Return: data
*/
void execArgs(char **args)
{
pid_t pid;
/* Built-in commands */
if (strcmp(args[0], "cd") == 0)
{
changeDirectory(args);
}
else if (strcmp(args[0], "env") == 0)
{
printEnvVariables();
}
else if (strcmp(args[0], "exit") == 0)
{
exitShell(args);
}
else
{
/* Find the executable */
char *exe = findExecutable(args[0]);
/* Check if executable was found */
if (exe[0] == '\0')
{
printf("%s not found in PATH\n", args[0]);
return;
}
/* Fork and execute command */
pid = fork();
if (pid == 0)
{
/* Child process */
if (execve(exe, args, environ) == -1)
{
perror("execve failed");
exit(EXIT_FAILURE);
}
}
else if (pid < 0)
{
/* Fork failed */
perror("fork failed");
}
else
{
/* Parent process */
wait(NULL);
}
}
}
/* Built-in functions implementations here */
/**
* readInput - Read input from stdin
*
* Return: array of tokens
*/
char *readInput()
{
char *input = malloc(MAX_INPUT_SIZE);
fgets(input, MAX_INPUT_SIZE, stdin);
return (input);
}
/**
* parseInput - tokenize input string into arguments
*
* @input: input string param
* Return: array of tokens strings
*/
char **parseInput(char *input)
{
int bufSize = MAX_TOKENS;
int pos = 0;
char **tokens = malloc(bufSize * sizeof(char *));
char *token;
token = strtok(input, " \t\r\n\a");
while (token != NULL)
{
tokens[pos] = token;
pos++;
if (pos >= bufSize)
{
bufSize += MAX_TOKENS;
tokens = realloc(tokens, bufSize * sizeof(char *));
}
token = strtok(NULL, " \t\r\n\a");
}
tokens[pos] = NULL;
return (tokens);
}