-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_findcmd.c
More file actions
40 lines (38 loc) · 796 Bytes
/
_findcmd.c
File metadata and controls
40 lines (38 loc) · 796 Bytes
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
#include "shs.h"
/**
* _findcmd - finds the command adquired from input,
* searching PATH in case is not a full path
*
* @pinput: User input.
* @env: string with PATH value
* Return: -1 if pinput is found but is a directory,
* 127 if pinput can't be found in PATH, 0 otherwise
*/
int _findcmd(char **pinput, char *env)
{
char *envcp = NULL;
struct stat sb;
if (access(*pinput, F_OK) == 0)
{
stat(*pinput, &sb);
if ((sb.st_mode & S_IFMT) == S_IFDIR)
return (126);
else if ((sb.st_mode & S_IFMT) == S_IFREG)
return (0);
}
if (env != NULL)
{
envcp = malloc(_strlen(env) + 1);
if (envcp == NULL)
return (-1);
_strcpy(envcp, env);
if (_getpath(pinput, envcp) == 0)
{
free(envcp);
return (0);
}
free(envcp);
return (127);
} else
return (-1);
}