-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
85 lines (73 loc) · 1.32 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
77
78
79
80
81
82
83
84
85
#include "shell.h"
/**
* stok - tokeneizer func
* @lineptr: lineptr
* @wrds: array storing tokens
* @delim: delimiter for tokenization
*
* Return: No. of words
*/
int stok(char *lineptr, char ***wrds, char *delim)
{
int wordCnt, i = 0;
char *token;
if (lineptr == NULL || wrds == NULL)
return (-1);
wordCnt = wrd_counter(lineptr);
*wrds = (char **)malloc(sizeof(char *) * (wordCnt + 1));
if (*wrds == NULL)
return (-1);
token = strtok(lineptr, delim);
while (token != NULL)
{
(*wrds)[i] = _strdup(token);
token = strtok(NULL, delim);
if (token != NULL)
{
if (token[0] == '#')
token = NULL;
}
i++;
}
(*wrds)[i] = NULL;
return (wordCnt);
}
/**
* getExecutablePath - builds path
* @cmd: command
*
* Return: path
*/
char *getExecutablePath(char *cmd)
{
char *fullpath = NULL, *bin = "/bin/";
int i = 0, j = 0, direc_len;
if (access(cmd, X_OK | F_OK) == 0)
return (cmd);
if (cmd != NULL)
{
direc_len = _strlen(cmd) + _strlen(bin) + 1;
fullpath = malloc(sizeof(char) * direc_len);
if (fullpath != NULL)
{
while (i < direc_len - 1)
{
if (i < _strlen(bin))
{
fullpath[i] = bin[i];
}
else
{
fullpath[i] = cmd[j];
j++;
}
i++;
}
free(cmd);
cmd = NULL;
fullpath[i] = '\0';
return (fullpath);
}
}
return (fullpath);
}