-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.c
More file actions
135 lines (120 loc) · 2.22 KB
/
process.c
File metadata and controls
135 lines (120 loc) · 2.22 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
128
129
130
131
132
133
134
135
#include "shell.h"
/**
* error - handle errors
* @name: the name of the program
* @line: line number
* @command: the command with error
* @message: error message
* @interactive: interactive mode or not
* Return: error code
*/
void error(char *name, int line, char *command, char *message, int interactive)
{
dprintf(STDERR_FILENO, "%s: %d: %s: %s\n", name, line, command, message);
free(command);
if (!interactive)
exit(127);
}
/**
* free_arr - frees an array of strings
* @arr: array of strings
*/
void free_arr(char **arr)
{
int i = 0;
while (arr[i] != NULL)
{
free(arr[i]);
i++;
}
free(arr);
}
/**
* accept_command - acceps command from stdin
* @name: name of program
* @line: line number
* Return: 0 or -1
*/
char *accept_command(char *name, int line)
{
char *buffer = NULL;
size_t n = 0;
if (getline(&buffer, &n, stdin) == -1)
{
if (feof(stdin))
{
free(buffer);
exit(EXIT_SUCCESS);
}
else
{
free(buffer);
error(name, line, NULL, NULL, isatty(fileno(stdin)));
}
}
remove_newline(buffer);
return (buffer);
}
/**
* execute - executes the command
* @args: arguments of the progrm
* @buffer: input buffer
* @full_path: path of the executable
* @name: name of the program
* @line: command number
* Return: exit status
*/
int execute(char **args, char *buffer, char *full_path, char *name, int line)
{
char message[] = "No such file or directory", path[] = "/bin/ls";
int status;
pid_t pid;
(void)line;
if (check_executable(args[0]) != 0)
{
if (args != NULL)
free_arr(args);
if (full_path != NULL)
free(full_path);
if (buffer != NULL)
free(buffer);
err(path, "/test_hbtn", message);
return (2);
}
pid = fork();
if (pid == -1)
{
free(buffer);
if (full_path != NULL)
free(full_path);
free_arr(args);
error(name, 0, NULL, NULL, isatty(fileno(stdin)));
}
if (pid == 0)
{
execve(args[0], args, environ);
}
else
{
wait(&status);
if (args != NULL)
free_arr(args);
if (full_path != NULL)
free(full_path);
if (buffer != NULL)
free(buffer);
}
return (0);
}
/**
* prompt - prints prompt
* @interactive: checks for interactive mode
*/
void prompt(int interactive)
{
if (interactive)
{
printf("aben_shell$ ");
fflush(stdout);
}
}