-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir_stuff.c
80 lines (75 loc) · 1.62 KB
/
dir_stuff.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
#include "header.h"
/**
* dir_search - searches through the PATH for a matching command
* @argv: array of strings containing our tokenized arguments
* @path_tokens: array of strings containing our tokenized PATH
* Return: modified argv[0]
**/
char *dir_search(char **argv, char **path_tokens)
{
struct dirent *dir_store;
DIR *deer = NULL;
int i;
char *asdf = NULL;
char *store = NULL;
if (argv[0][0] == '/')
return (argv[0]);
for (i = 0; path_tokens[i] != '\0'; i++)
{
deer = opendir(path_tokens[i]);
while ((dir_store = readdir(deer)) != NULL)
{
if (_strcmp(argv[0], dir_store->d_name) == 0)
{
asdf = path_tokens[i];
store = executable_maker(asdf, argv);
closedir(deer);
return (store);
}
}
closedir(deer);
}
return (NULL);
}
/**
* executable_maker- modifies argv[0] into an executable
* @asdf: stores the path of the correct directory
* @argv: argv[0] is concatenated to asdf
* Return: modified asdf
**/
char *executable_maker(char *asdf, char **argv)
{
char *addslash = NULL;
char *newcmd = NULL;
addslash = _strcat(asdf, "/");
newcmd = _strcat(addslash, argv[0]);
free(addslash);
return (newcmd);
}
/**
*_strcat- entry point
*description: concatenates two strings
*@dest: string to copy to
*@src: string to be copied
*Return: dest
**/
char *_strcat(char *dest, char *src)
{
int a, p, x;
char *newcmd = NULL;
for (p = 0; dest[p] != '\0'; p++)
{}
for (a = 0; src[a] != '\0'; a++)
{}
newcmd = malloc(sizeof(char) * (a + p + 1));
for (x = 0; x < p; x++)
{
newcmd[x] = dest[x];
}
for (x = 0; x < a; x++)
{
newcmd[x + p] = src[x];
}
newcmd[x + p] = '\0';
return (newcmd);
}