-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
76 lines (69 loc) · 1.35 KB
/
parse.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
#include "main.h"
/**
* is_path_form - checks if the given filename is a path
* @data: the data struct pointer
*
* Return: (Success)
* ------- (Fail) otherwise
*/
int is_path_form(sh_t *data)
{
if (_strchr(data->args[0], '/') != 0)
{
data->cmd = data->args[0];
return (SUCCESS);
}
return (FAIL);
}
#define DELIMITER ":"
/**
* is_short_form - check if the given filename is short form
* @data: the data struct pointer
*
* Return: (Success)
* ------- (Fail) otherwise
*/
void is_short_form(sh_t *data)
{
char *path, *token, *_path;
struct stat st;
path = _getenv("PATH", data);
_path = _strdup(path);
token = strtok(_path, DELIMITER);
while (token)
{
data->cmd = _strcat(token, data->args[0]);
if (stat(data->cmd, &st) == 0)
break;
free(data->cmd);
token = strtok(NULL, DELIMITER);
}
if (*data->cmd == '\0')
data->cmd = _strdup(data->args[0]);
free(_path);
}
#undef DELIMITER
/**
* is_builtin - checks if the command is builtin
* @data: a pointer to the data structure
*
* Return: (Success) 0 is returned
* ------- (Fail) negative number will returned
*/
int is_builtin(sh_t *data)
{
blt_t blt[] = {
{"exit", abort_prg},
{"cd", change_dir},
{"help", display_help},
{NULL, NULL}
};
int i = 0;
while ((blt + i)->cmd)
{
if (_strcmp(data->args[0], (blt + i)->cmd) == 0)
return (SUCCESS);
i++;
}
return (NEUTRAL);
}