-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell.c
More file actions
100 lines (95 loc) · 1.74 KB
/
Copy pathshell.c
File metadata and controls
100 lines (95 loc) · 1.74 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
#include "main.h"
/**
* main - entry point
* @argc: number of args
* @argv: args
* Return: 0 always
*/
int main(int argc __attribute__((unused)), char *argv[])
{
size_t len;
char *line = NULL, *command;
int nr, loop = 0, status = 0;
while (1)
{
if (isatty(STDIN_FILENO) == 1)
write(STDOUT_FILENO, "#ouafi/sfn$ ", 11);
nr = getline(&line, &len, stdin);
if (nr >= 0)
{
line[nr - 1] = '\0';
command = trimSpaces(line);
if (!built_in(command, line, status))
status = Executefile(command, argv[0]);
free(line);
line = NULL;
}
else
{
free(line);
break;
}
loop++;
}
return (0);
}
/**
* trimSpaces - ignores surrounded spaces
* in the command line
* @previous_line: command line
* Return: command line after the
* spaces are removed.
*/
char *trimSpaces(char *previous_line)
{
char *end_line, *new_line = previous_line;
while (*new_line == ' ')
new_line++;
end_line = new_line + (_strlen(new_line) - 1);
while (end_line > new_line && *end_line == ' ')
end_line--;
*(end_line + 1) = '\0';
return (new_line);
}
/**
* createBuffer - allocates memory
* for args
* @num_args: number of args
* @command: command line
* Return: args
*/
char **createBuffer(int num_args, char *command)
{
char **args, *delim = " ", *args_use;
int i = 0;
args = malloc((num_args + 1) * sizeof(char *));
if (!args)
return (NULL);
args_use = strtok(command, delim);
while (args_use != NULL)
{
args[i] = _strdup(args_use);
args_use = strtok(NULL, delim);
i++;
}
args[i] = NULL;
return (args);
}
/**
* customFree - free all memory
* @args: arguments
*/
void customFree(char **args)
{
int i = 0;
if (args != NULL)
{
while (args[i] != NULL)
{
free(args[i]);
i++;
}
free(args[i]);
free(args);
}
}