-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscandir.c
40 lines (37 loc) · 870 Bytes
/
scandir.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
#include "shell.h"
/**
* _scandir - scan for match in directory with command
* @command : shorthand command to search for
* @dir : directory
*
* Return: 0 if successful; 1 otherwise
*/
int _scandir(char *command, char *dir)
{
char **resolved_path = NULL;
struct dirent *read_dir = NULL;
DIR *open_dir = NULL;
open_dir = opendir(dir);
if (open_dir == NULL)
{
perror("Opendir Error");
return (EXIT_FAILURE);
}
while ((read_dir = readdir(open_dir)) != NULL)
{
if (strcmp(read_dir->d_name, ".") == 0
|| strcmp(read_dir->d_name, "..") == 0)
continue;
if (strcmp(read_dir->d_name, command) == 0)
{
*resolved_path = strcat(read_dir->d_name, command);
if ((stat_exec(resolved_path, 0)) != EXIT_SUCCESS)
{
perror("Stat Error on resolved path");
return (EXIT_FAILURE);
}
}
}
closedir(open_dir);
return (EXIT_SUCCESS);
}