-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.c
158 lines (141 loc) · 3.12 KB
/
builtins.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
154
155
156
157
158
#include "main.h"
/*=======================================================*/
/*HANDLE BUILT IN COMMANDS*/
/**
* findBuiltinCmd - add descr
* @shellInfo: add descr
* Return: void
*/
int findBuiltinCmd(commandInfo *shellInfo)
{
int i, builtinReturn = -1;
commandStruct builtInCommand[] = {
{"history", NULL},
{"setenv", NULL},
{"exit", exitCommand},
{"env", envCommand},
{"cd", cdCommand},
{"alias", NULL},
{"help", NULL},
{"unsetenv", NULL},
{NULL, NULL}
/* Fix, and add more ... */
};
for (i = 0; builtInCommand[i].name; i++)
if (_strcmp(shellInfo->command_arguments[0], builtInCommand[i].name) == 0)
{
shellInfo->current_line_number++;
builtinReturn = builtInCommand[i].CommandFunction(shellInfo);
break;
}
return (builtinReturn);
}
/**
* executeExternalCmd - finds external exec command in PATH
* @shellInfo: add descr
* Return: void
*/
void executeExternalCmd(commandInfo *shellInfo)
{
char *path = NULL;
int i, k;
shellInfo->executable_path = shellInfo->command_arguments[0];
if (shellInfo->line_count_flag == 1)
{
shellInfo->current_line_number++;
shellInfo->line_count_flag = 0;
}
for (i = 0, k = 0; shellInfo->current_argument[i]; i++)
if (!checkDelim(shellInfo->current_argument[i], " \t\n"))
k++;
if (!k)
return;
path = findPath(shellInfo, retEnv(shellInfo, "PATH=")
, shellInfo->command_arguments[0]);
if (path)
{
shellInfo->executable_path = path;
fkCommand(shellInfo);
}
else
{
if (((isatty(STDIN_FILENO) && shellInfo->input_file_descriptor <= 2)
|| retEnv(shellInfo, "PATH=") || shellInfo->command_arguments[0][0] == '/')
&& isCmd(shellInfo, shellInfo->command_arguments[0]))
fkCommand(shellInfo);
else if (*(shellInfo->current_argument) != '\n')
{
shellInfo->commandExecStatus = 127;
print_error(shellInfo, "not found\n");
}
}
}
/**
* checkReg - Checks if a given path corresponds to a regular file.
* @path: add descr
* Return: 1 if the path is a regular file, 0 otherwise.
*/
int checkReg(const char *path)
{
struct stat st;
return (path && stat(path, &st) == 0 && S_ISREG(st.st_mode));
}
/**
* isCmd - add descr
* @shellInfo: the shellInfo struct
* @path: add descr
* Return: add descr
*/
int isCmd(commandInfo *shellInfo, char *path)
{
struct stat st;
(void)shellInfo;
if (!path || stat(path, &st))
return (0);
if (S_ISREG(st.st_mode))
{
return (1);
}
return (0);
}
/**
* findPath - add descr
* @shellInfo: the shellInfo struct
* @pathstr: add descr
* @cmd: add descr
*
* Return: full path of cmd if found or NULL
*/
char *findPath(commandInfo *shellInfo, char *pathstr, char *cmd)
{
int i = 0, curr_pos = 0;
char *path;
if (!pathstr)
return (NULL);
if ((_strlen(cmd) > 2) && string_starts_with(cmd, "./"))
{
if (isCmd(shellInfo, cmd))
return (cmd);
}
while (1)
{
if (!pathstr[i] || pathstr[i] == ':')
{
path = charDup(pathstr, curr_pos, i);
if (!*path)
string_concatenate(path, cmd);
else
{
string_concatenate(path, "/");
string_concatenate(path, cmd);
}
if (checkReg(path))
return (path);
if (!pathstr[i])
break;
curr_pos = i;
}
i++;
}
return (NULL);
}