-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_command.c
51 lines (49 loc) · 1.11 KB
/
search_command.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
#include "main.h"
/**
* searchCmd - determines the location of the command
* @commandName: name of the command
*
* Return: return a data structure containing info of a commands locations or
* null otherwise
*/
cmd_t *searchCmd(char *commandName)
{
/* declare variables */
cmd_t *commandData;
struct stat st;
char *cmdPath;
/* initialize variables */
commandData = malloc(sizeof(cmd_t));
if (commandData == NULL)
return (NULL);
cmdPath = NULL;
/* check if commandName contain a forward slash */
if (strchr(commandName, '/'))
{
if (stat(commandName, &st) == 0)
{
commandData->cmd = commandName;
commandData->locationFlag = 2;
return (commandData);
}
free(commandData);
return (NULL);
}
/*check the builtin functions for a command name match */
if (isBuiltInCmd(commandName))
{
commandData->cmd = commandName;
commandData->locationFlag = 0;
return (commandData);
}
/* search for command in PATH */
cmdPath = searchPath(commandName);
if (cmdPath != NULL)
{
commandData->cmd = cmdPath;
commandData->locationFlag = 1;
return (commandData);
}
free(commandData);
return (NULL);
}