-
Notifications
You must be signed in to change notification settings - Fork 0
/
more_helper_functions.c
154 lines (137 loc) · 2.57 KB
/
more_helper_functions.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
151
152
153
154
#include "main.h"
/**
* check_path - Entry point
*
* Descritpion: 'checks for path'
*
* @path: 'path'
* Return: 1 - true & 0 - false
*/
int check_path(char *path)
{
struct stat buff;
return ((stat(path, &buff) == 0));
} /* End function */
/**
* _which - Entry point
* Description: 'to find the full path name'
*
* @filename: 'file name'
* @path: 'path'
* Return: full path name if found && 0 if not found.
*/
char *_which(char *filename, char *path)
{
char *path_cpy;
char *tokens[1024];
char *pathname;
int i;
if (path == NULL)
{
return (NULL);
} /* End if */
path_cpy = strdup(path);
/* Tokenize path */
i = 0;
tokens[i] = strtok(path_cpy, ":");
while (tokens[i] != NULL)
{
pathname = malloc(strlen(tokens[i]) + strlen(filename) + 2);
_strcpy(pathname, tokens[i]);
_strcat(pathname, "/");
_strcat(pathname, filename);
if (check_path(pathname))
{
free(path_cpy);
return (pathname);
} /* end if */
free(pathname);
i++;
tokens[i] = strtok(NULL, ":");
} /* end while */
free(path_cpy);
return (NULL);
} /* end function */
/**
* _strncmp - compare n character of str1 and str2
* @str1: string 1
* @str2: string 2
* @n: number of characters
* Return: 0 on success
*/
int _strncmp(char *str1, char *str2, int n)
{
int i = 0;
while (i < n)
{
if (str1[i] != str2[i])
return (-1);
i++;
}
return (0);
}
/**
* _getenv - get an environment variable given name
* @name: name of environment variable
* Return: environment variable value on success
*/
char *_getenv(char *name)
{
int namelen = _strlen(name);
int i;
for (i = 0; environ[i] != NULL; i++)
{
if (_strncmp(name, environ[i], namelen) == 0 && environ[i][namelen] == '=')
{
return (environ[i] + namelen + 1);
}
}
return (NULL);
}
/**
* _unsetenv - remove env variable
* @name: variable name
* Return: 0 on success
*/
int _unsetenv(char *name)
{
char **env = environ;
int len, nameLen;
int environ_cnt = 0, i = 0, j;
char **new_arr;
if (name == NULL)
return (-1);
while (*env != NULL)
{
environ_cnt++;
env++;
}
new_arr = malloc((environ_cnt + 1) * sizeof(char *));
if (new_arr == NULL)
return (-1);
env = environ;
nameLen = _strlen(name);
while (*env != NULL)
{
if (_strncmp(name, *env, nameLen) != 0 || (*env)[nameLen] != '=')
{
len = _strlen(*env);
new_arr[i] = malloc((len + 1) * sizeof(char));
if (new_arr[i] == NULL)
{
for (j = 0; j < i; j++)
{
free(new_arr[j]);
}
free(new_arr);
return (-1);
}
_strcpy(new_arr[i], *env);
i++;
}
env++;
}
new_arr[i] = NULL;
environ = new_arr;
return (0);
}