-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_1.c
150 lines (135 loc) · 2.45 KB
/
utils_1.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
#include "main.h"
/**
* check_exec - ...
* @file: ...
*
* Return: 1 if file exists and is executable,
* 0 if file exists but not executable (no permission),
* -1 if file does not exist
*/
int check_exec(char *file)
{
struct stat sb;
int slash = 0, i = 0;
while (file[i])
{
if (file[i] == '/')
{
slash = 1;
break;
}
i++;
}
if (stat(file, &sb) == 0 && sb.st_mode & S_IXUSR && slash)
return (1);
else if (stat(file, &sb) == 0 && slash)
return (0);
return (-1);
}
/**
* check_path - ...
* @file: ...
* @dirs: ...
*
* Return: the path of the executable if found
* NULL otherwise
*/
char *check_path(char *file, char **dirs)
{
struct stat sb;
int i = 0;
char *concat, *concat_path;
concat = string_concat("/", file);
if (concat == NULL)
{
exit(EXIT_FAILURE);
}
for (i = 0; dirs[i]; i++)
{
concat_path = string_concat(dirs[i], concat);
if (concat_path == NULL)
{
free(concat);
exit(EXIT_FAILURE);
}
if (stat(concat_path, &sb) == 0 && sb.st_mode & S_IXUSR)
{
free(concat);
return (concat_path);
}
free(concat_path);
}
free(concat);
return (NULL); /* no path was found */
}
/**
* string_concat - a function that concatenates two strings.
*
* @s1: first string
* @s2: second string
*
* Return: final string, NULL on failure
*/
char *string_concat(char *s1, char *s2)
{
int i, j, k;
char *s;
for (i = 0; s1[i]; i++)
;
for (j = 0; s2[j]; j++)
;
s = (char *)malloc(sizeof(char) * (i + j + 1));
if (s == NULL)
return (NULL);
for (k = 0; k < i; k++)
s[k] = s1[k];
for (k = 0; k < j; k++)
s[k + i] = s2[k];
s[i + j] = '\0';
return (s);
}
/**
* _strdup - returns a pointer to a newly allocated space in memory,
* which contains a copy of the string given as a parameter.
* @str: the source string
*
* Return: returns a pointer to the duplicated string.
* It returns NULL if insufficient memory was available
*/
char *_strdup(char *str)
{
char *copy;
int i, size = 0;
if (str == NULL)
return (NULL);
while (str[size] != '\0')
size++;
copy = (char *)malloc((sizeof(char) * size) + 1);
if (copy == NULL)
return (NULL);
for (i = 0; i < size; i++)
copy[i] = str[i];
copy[size] = '\0';
return (copy);
}
/**
* _strcmp - compares 2 strings
* @x: ...
* @y: ...
*
* Return: 0 or 1
*/
int _strcmp(char *x, char *y)
{
int i, j;
for (i = 0; x[i]; i++)
;
for (j = 0; y[j]; j++)
;
if (i != j)
return (1);
for (i = 0; x[i]; i++)
if (x[i] != y[i])
return (1);
return (0);
}