-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
127 lines (108 loc) · 3.16 KB
/
shell.c
File metadata and controls
127 lines (108 loc) · 3.16 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_INPUT_LEN (1024)
char **recive_cmd(char **cmd, char ***args, size_t *noft);
char **strsplit(const char* str, const char* delim, size_t *noft);
int main(int argc, char *argv[])
{
size_t noft = 0;
bool is_exit = false;
pid_t pid = 0;
int status = 0,
c_ret = 0;
char *cmd = NULL,
**args = NULL,
**words = NULL;
while (!is_exit)
{
words = recive_cmd(&cmd, &args, &noft);
if (strcmp(cmd, "exit"))
{
/* The New Process Creation */
pid = fork();
if (pid == 0)
{
/* Creates a new Adress-Space and loads the image */
execvp(cmd, args);
}
if ((c_ret = waitpid(pid, &status, 0)) == -1)
fprintf(stderr, "Can not run the command '%s' with the given arguments.\n", cmd);
if (c_ret == pid)
printf("'%s' returned with value: %d (%s)\n", cmd, status, !status ? "succeed" : "failed");
}
else
is_exit = true;
/* Free the dynamic allocations */
for (size_t i = 0; i <= noft; i++)
{
free(words[i]);
words[i] = NULL;
}
free(words);
words = NULL;
}
return (0);
}
/**
* recive_cmd() - Recives a command (and arguments) from the user
*
* @cmd: A pointer to the command varible (the command will be stored there)
* @args A pointer to the args variable (the arguments will be stored there)
* @nofa: A pointer to the number of arguments (the number will be stored there)
*
* Return: A list of arguments, represented as an array of literals
*/
char **recive_cmd(char **cmd, char ***args, size_t *nofa)
{
char input[MAX_INPUT_LEN] = { 0 };
size_t num_of_args = 0;
printf("ashell:>> ");
fgets(input, MAX_INPUT_LEN, stdin);
input[strlen(input) - 1] = '\0';
char **words = strsplit(input, " ", &num_of_args);
num_of_args -= 1;
*cmd = words[0];
*args = words;
*nofa = num_of_args;
return words;
}
/**
* strsplit() - Creates an array of substrings by splitting the input string based on a delimiter
*
* @str: The string which should be splitted
* @delim: The delimiter
* @nofa: A pointer to the number of arguments (the number will be stored there)
*
* Return: An array of substrings (based on @delim)
*/
char **strsplit(const char* str, const char* delim, size_t *noft)
{
char *s = strdup(str);
size_t tokens_alloc = 1;
size_t tokens_used = 0;
char **tokens = (char **) calloc(tokens_alloc, sizeof(char*));
char *token, *rest = s;
while ((token = strsep(&rest, delim)) != NULL)
{
if (tokens_used == tokens_alloc) {
tokens_alloc *= 2;
tokens = (char **) realloc(tokens, tokens_alloc * sizeof(char*));
}
tokens[tokens_used++] = strdup(token);
}
if (tokens_used == 0)
{
free(tokens);
tokens = NULL;
}
else
tokens = (char **) realloc(tokens, tokens_used * sizeof(char*));
*noft = tokens_used;
free(s);
return tokens;
}