-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecho_command_helpers.c
103 lines (88 loc) · 1.9 KB
/
echo_command_helpers.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
#include "shell.h"
/**
* print_var - helper function for echo_command.
* @var_name: variable name.
* Return: Nothing.
*/
void print_var(char *var_name)
{
int stdout_fd = 1;
char *var_value;
size_t var_value_len;
var_value = getenv(var_name);
if (var_value != NULL)
{
var_value_len = _strlen(var_value);
write(stdout_fd, var_value, var_value_len);
}
}
/**
* print_exit_status - helper function for echo_command.
* @exit_status_str: exit status strting.
* @exit_status_len: exit status length.
* @exit_status: exit status
* @stdout_fd: file desctiptor.
* Return: Nothing.
*/
void print_exit_status(char exit_status_str[],
int *exit_status_len,
int exit_status, int stdout_fd)
{
char tmp;
int j;
if (exit_status == 0)
exit_status_str[(*exit_status_len)++] = '0';
else
{
while (exit_status != 0)
{
exit_status_str[(*exit_status_len)++] = '0' + (exit_status % 10);
exit_status /= 10;
}
}
for (j = 0; j < *exit_status_len / 2; j++)
{
tmp = exit_status_str[j];
exit_status_str[j] = exit_status_str[*exit_status_len - j - 1];
exit_status_str[*exit_status_len - j - 1] = tmp;
}
write(stdout_fd, exit_status_str, *exit_status_len);
}
/**
* print_pid - helper function for echo_command.
* @pid_str: pid string.
* @pid_len: pid length.
* @stdout_fd: file desctiptor.
* Return: Nothing.
*/
void print_pid(char pid_str[], int *pid_len, int stdout_fd)
{
char tmp;
int pid = getpid(), j;
if (pid == 0)
pid_str[(*pid_len)++] = '0';
else
{
while (pid != 0)
{
pid_str[(*pid_len)++] = '0' + (pid % 10);
pid /= 10;
}
}
for (j = 0; j < *pid_len / 2; j++)
{
tmp = pid_str[j];
pid_str[j] = pid_str[*pid_len - j - 1];
pid_str[*pid_len - j - 1] = tmp;
}
write(stdout_fd, pid_str, *pid_len);
}
/**
* print_variable - helper function for echo_command.
* @arg: arguments.
* Return: Nothing.
*/
void print_variable(char arg[])
{
print_var(arg + 1);
}