-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli_helpers_0.c
96 lines (88 loc) · 1.97 KB
/
cli_helpers_0.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
#include "main.h"
/**
* get_next_command - Gets the next command that can be executed by the shell
* @cur: The current command node
* @exit_code: The exit code of the last command
*
* Return: A pointer to the next executable command node
*/
cmd_t *get_next_command(cmd_t *cur, int exit_code)
{
cmd_t *node = NULL;
if (((cur->next_cond == OP_OR) && (exit_code != 0))
|| ((cur->next_cond == OP_AND) && (exit_code == 0)))
{
return (cur->next);
}
else
{
node = cur;
while (node != NULL)
{
if (node->next_cond == OP_SEP)
return (node->next);
node = node->next;
}
return (node);
}
}
/**
* handle_ctrl_d - Handles Control+D key press
* @len: The number of characters on the command line
*/
void handle_ctrl_d(int len)
{
char *buf[] = {"0"};
if (len == 0)
sc_exit(1, buf);
}
/**
* check_args - Checks the arguments for a file argument
* @ac: The number of arguments the program received
* @av: The arguments the program received
*
* Return: TRUE if the file exists and is readable, otherwise FALSE
*/
char check_args(int ac, char *av[])
{
char *buf = NULL;
if (ac > 1)
{
if ((access(av[1], R_OK) == 0))
{
return (TRUE);
}
else
{
buf = long_to_str(*((int *)get_shell_prop(LINE_NUMBER_ID)));
write(STDERR_FILENO, av[0], str_len(av[0]));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, buf, str_len(buf));
write(STDERR_FILENO, ": Can't open ", 13);
write(STDERR_FILENO, av[1], str_len(av[1]));
write(STDERR_FILENO, "\n", 1);
if (buf != NULL)
free(buf);
exit(EC_COMMAND_NOT_FOUND);
}
}
return (FALSE);
}
/**
* is_blank - Checks if the given string consists of whitespaces only
* @str: The string to check
*
* Return: TRUE if there are only whitespaces, otherwise NULL
*/
char is_blank(char *str)
{
int i = 0, j = 0, len = str_len(str);
if ((str == NULL) || (len == 0))
return (TRUE);
for (i = 0; *(str + i) != '\0'; i++)
{
if (is_whitespace(*(str + i)))
j++;
}
return (j == len ? TRUE : FALSE);
}