-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
160 lines (143 loc) · 3.04 KB
/
utils.c
File metadata and controls
160 lines (143 loc) · 3.04 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "main.h"
/**
* SIGINT_handler - handles CTRL + C signal.
*
* @signal: signal to be handled.
*
* Return: Nothing.
*/
void SIGINT_handler(int signal)
{
(void) signal;
write(STDOUT_FILENO, "\n($) ", 5);
}
/**
* add_node_end - adds a new node at the end of a Commands list.
*
* @prevIndex: the starting index.
* @lastIndex: the end index.
* @logicalOp: logical operator to be add to the node.
* @tokens: array of commands to extract each command from it.
* @head: the head Commands node.
*
* Return: the address of the new element, or NULL if it failed.
*/
Commands *add_node_end(size_t prevIndex, size_t lastIndex,
char *logicalOp, char **tokens, Commands **head)
{
Commands *new = malloc(sizeof(Commands));
Commands *traverse = *head;
size_t index;
if (new == NULL)
return (NULL);
new->op = _strdup(logicalOp);
new->cmd = malloc((lastIndex - prevIndex + 1) * sizeof(char *));
if (new->cmd == NULL)
{
free(new->op);
free(new);
return (NULL);
}
new->nextCmd = NULL;
for (index = 0; prevIndex < lastIndex; prevIndex++)
{
(new->cmd)[index] = _strdup(tokens[prevIndex]);
if ((new->cmd)[index] == NULL)
{
free(new->op);
free_2D(new->cmd, prevIndex);
return (NULL);
}
index++;
}
(new->cmd)[index] = NULL;
if (*head == NULL)
{
*head = new;
return (new);
}
while (traverse->nextCmd != NULL)
traverse = traverse->nextCmd;
traverse->nextCmd = new;
return (new);
}
/**
* free_2D - frees a 2 dimensional array previously created.
*
* @grid: 2 dimensional array.
* @height: height of the array.
*
* Return: Nothing.
*/
void free_2D(char **grid, int height)
{
int index;
for (index = 0; index < height; index++)
{
free(grid[index]);
}
free(grid);
}
/**
* absolutePath - checks if the command is absolute path to the binary or not.
*
* @cmd: command.
*
* Return: (1) absolute path, (0) otherwise.
*/
int absolutePath(char *cmd)
{
int absPath = 0;
if (cmd[0] == '.' && cmd[1] == '/')
absPath = 1;
else if (cmd[0] == '/')
absPath = 1;
else if (cmd[0] == '.' && cmd[1] == '.' && cmd[2] == '/')
absPath = 1;
return (absPath);
}
/**
* _getenv - gets the environment value for an env variable.
*
* @env: the environment variable.
*
* Return: array of env values for the env variable.
*/
char **_getenv(char *env)
{
int index, len;
char **PATH = NULL, **tempPATH = NULL;
char **envs = environ;
char *tempEnv;
tempEnv = _strdup(env);
tempEnv = _realloc(tempEnv, _strlen(tempEnv) + 1, _strlen(tempEnv) + 2);
tempEnv[_strlen(env)] = '=';
tempEnv[_strlen(env) + 1] = '\0';
for (index = 0; envs[index] != NULL; index++)
{
if (_strncmp(tempEnv, envs[index], _strlen(tempEnv)) == 0)
{
PATH = tokenizedArray(envs[index], "=");
break;
}
}
free(tempEnv);
if (PATH == NULL)
return (NULL);
else if (*PATH == NULL)
{
free(PATH);
return (NULL);
}
else if (_strcmp(*PATH, env) == 0 && PATH[1] == NULL)
{
free_2D(PATH, 1);
return (NULL);
}
for (len = 0; PATH[len] != NULL;)
len++;
tempPATH = PATH;
PATH = tokenizedArray(PATH[1], ":");
free_2D(tempPATH, len);
return (PATH);
}