-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.c
99 lines (90 loc) · 1.46 KB
/
handlers.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
#include "shell.h"
/**
* execute_echo - Implements the 'echo' built-in command.
* @args: Array of command arguments.
*/
void execute_echo(char **args)
{
if (args[1] != NULL)
{
char *output = args[1];
replace_env_vars(output);
_puts(output);
}
else
{
_putchar('\n');
}
}
/**
* handle_exit - Handles the 'exit' built-in command.
* @args: Array of command arguments.
*/
void handle_exit(char **args)
{
int exit_stat;
if (args[1] != NULL)
{
exit_stat = _atoi(args[1]);
exit(exit_stat);
}
else
{
exit(0);
}
}
/**
* handle_env - Handles the 'env' built-in command.
*/
void handle_env(void)
{
char **env = environ;
int i = 0;
while (env[i] != NULL)
{
_puts(env[i]);
i++;
}
}
/**
* read_line - Reads a line of input from the user.
* Return: A pointer to the input line.
*/
char *read_line(void)
{
int buffer_size = BUFFER_SIZE;
int position = 0;
char *buffer = malloc(buffer_size * sizeof(char));
int c;
if (!buffer)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
while (1)
{
c = my_getchar();
if (c == EOF || c == '\n')
{
if (c == EOF && position == 0)
{
free(buffer);
return (NULL);
}
buffer[position] = '\0';
return (buffer);
}
buffer[position] = c;
position++;
if (position >= buffer_size)
{
buffer_size += BUFFER_SIZE;
buffer = _realloc(buffer, buffer_size * sizeof(char));
if (!buffer)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
}
}
}