-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_functions_to_excute1.c
154 lines (146 loc) · 2.95 KB
/
helper_functions_to_excute1.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "shell.h"
/**
* get_av_with_flags_helper - helper function for getting argument with flags.
* @token: The token.
* @line: The input line.
* @av: The array of arguments.
* @status: The status.
* Return: The command.
*/
char *get_av_with_flags_helper(char *token, char *line, char **av, int status)
{
char *var, *cmd, *line_cpy;
line_cpy = line;
if (token == NULL)
{
free(av);
free(line_cpy);
return (NULL);
}
if (_strcmp("$$", token) == 0)
cmd = get_process_id();
else if (_strcmp("$?", token) == 0)
cmd = get_status(status);
else if ((token[0] == '$') && (token[1]))
{
var = _get_env(&token[1]);
if (var)
cmd = _strdup(var);
else
cmd = _strdup("");
}
else
cmd = _strdup(token);
return (cmd);
}
/**
* get_av_with_flags - get arguments with flags from input line.
* @line: the input line.
* @status: the status.
* Return: the array of arguments.
*/
char **get_av_with_flags(char *line, int status)
{
char *line_cpy, *token, **av, *var, *cmd;
int i = 0, c_count;
handle_comments(line);
if (line[0] == '\0')
return (NULL);
line_cpy = _strdup(line);
if (line_cpy == NULL)
return (NULL);
c_count = char_count(line_cpy, ' ');
av = malloc((c_count + 1) * sizeof(char *));
token = _strtok(line_cpy, TOK_D);
cmd = get_av_with_flags_helper(token, line, av, status);
av[i++] = cmd;
while (token != NULL)
{
token = _strtok(NULL, TOK_D);
if (token != NULL)
{
if (_strcmp("$$", token) == 0)
cmd = get_process_id();
else if (_strcmp("$?", token) == 0)
cmd = get_status(status);
else if ((token[0] == '$') && (token[1]))
{
var = _get_env(&token[1]);
if (var)
cmd = _strdup(var);
else
cmd = _strdup("");
}
else
cmd = _strdup(token);
av[i++] = cmd;
}
}
av[i] = NULL;
free(line_cpy);
return (av);
}
/**
* exit_check - check for exit command.
* @nread: the number of characters read.
* @exit_cmd: the exit command.
* Return: void.
*/
void exit_check(int nread, char *exit_cmd)
{
if (nread == EOF)
{
write(STDOUT_FILENO, "\n", 1);
exit(0);
}
if (_strcmp(exit_cmd, "exit") == 0)
{
exit(0);
}
if (_strcmp(exit_cmd, "exit") == 0)
{
exit(0);
}
}
/**
* _get_env - get value of an env var.
* @name: the name of the env variable.
* Return: the value of the env var, or NULL if not found.
*/
char *_get_env(char *name)
{
int i = 0, j = 0;
if (name == NULL)
return (NULL);
while (environ[i])
{
j = 0;
while (environ[i][j] != '=')
{
if (environ[i][j] != name[j])
break;
if (environ[i][j] == name[j] && (environ[i][j + 1] == '='))
return (&environ[i][_strlen(name) + 1]);
j++;
}
i++;
}
return (NULL);
}
/**
* char_count - count occurrences of a character in a string.
* @str: the input string.
* @c: the character to count.
* Return: the count of occurrences.
*/
unsigned int char_count(char *str, char c)
{
unsigned int count = 0;
while (*str != '\0')
{
if (*str != c && *(str + 1) == c)
count++;
str++;
}
return (count + 1);
}