Skip to content

Commit cbe51bd

Browse files
authored
Merge pull request #2 from Demidorn/demi
Demi
2 parents ec8a57c + 5932aa1 commit cbe51bd

13 files changed

+803
-0
lines changed

DL_envs.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include "header.h"
2+
3+
/**
4+
* prog_setenv - Modifies an environment variable to the path
5+
* @args: An array of arguments
6+
* @ard: Double pointer to arguments
7+
*
8+
* Return: 1 if error occurs otherwise 0
9+
*/
10+
11+
int prog_setenv(char **args, char __attribute__((__unused__)) **ard)
12+
{
13+
char **old_value = NULL, *new_value, **new_env;
14+
size_t size = 0;
15+
int start;
16+
17+
if (!args[0] || !args[1])
18+
_error(args[0], -1);
19+
new_value = malloc((_strlen(args[0]) + 1) + (_strlen(args[1]) + 1));
20+
if (!new_value)
21+
_error(args[0], -1);
22+
_strcpy(new_value, args[0]);
23+
_strcat(new_value, "=");
24+
_strcat(new_value, args[1]);
25+
old_value = _getenv(args[0]);
26+
if (old_value)
27+
{
28+
free(*old_value);
29+
*old_value = new_value;
30+
return (0);
31+
}
32+
33+
while (environ[size])
34+
size++;
35+
new_env = malloc(sizeof(char *) * (size + 2));
36+
if (!new_env)
37+
{
38+
free(new_value);
39+
_error(args[0], -1);
40+
}
41+
for (start = 0; environ[start]; start++)
42+
new_env[start] = environ[start];
43+
free(environ);
44+
environ = new_env;
45+
environ[start] = new_value;
46+
environ[start + 1] = NULL;
47+
return (0);
48+
}
49+
50+
/**
51+
* prog_unsetenv - Deletes an environment variable from the path
52+
* @args: Array of arguments
53+
* @ard: Double pointer arguments
54+
*
55+
* Return: -1 if error occurs otherwise 0
56+
*/
57+
58+
int prog_unsetenv(char **args, char __attribute__((__unused__)) **ard)
59+
{
60+
char **env_var, **new_env;
61+
size_t start = 0;
62+
int i, j;
63+
64+
i = j = 0;
65+
if (!args[0])
66+
_error(args[0], -1);
67+
env_var = _getenv(args[0]);
68+
if (!env_var)
69+
return (0);
70+
71+
while (environ[start])
72+
start++;
73+
new_env = malloc(sizeof(char *) * start);
74+
if (!new_env)
75+
_error(args[0], -1);
76+
77+
while (environ[i])
78+
{
79+
if (*env_var == environ[i])
80+
{
81+
free(*env_var);
82+
continue;
83+
}
84+
new_env[j] = environ[i];
85+
i++;
86+
j++;
87+
}
88+
free(environ);
89+
environ = new_env;
90+
environ[start - 1] = NULL;
91+
return (0);
92+
}
93+
94+
/**
95+
* _printenv - prints path to the environment variable
96+
*
97+
* Return: ....
98+
*/
99+
100+
void _printenv(void)
101+
{
102+
char **env = environ;
103+
104+
while (*env != NULL)
105+
{
106+
print_env_variable(*env);
107+
env++;
108+
}
109+
}
110+
111+
/**
112+
* print_env_variable - displays output of the environment variable
113+
* @str: string that holds the outputted env variable
114+
*
115+
* Return: ...
116+
*/
117+
118+
void print_env_variable(const char *str)
119+
{
120+
size_t len = _strlen(str);
121+
122+
write(STDOUT_FILENO, str, len);
123+
write(STDOUT_FILENO, "\n", 1);
124+
}

DL_error.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "header.h"
2+
3+
/**
4+
* _error - writes error message to sdterr
5+
* @args: array of arguments
6+
* @error_msg: error
7+
*
8+
* Return: Error
9+
*/
10+
11+
void _error(const char *args, int error_msg)
12+
{
13+
char *buffer = (char *)malloc(sizeof(char));
14+
char str_error[100];
15+
16+
if (access(args, F_OK) != 0)
17+
{
18+
strcpy(buffer, "File not found\n");
19+
}
20+
else
21+
{
22+
strcpy(buffer, "Error: ");
23+
strcat(buffer, args);
24+
strcat(buffer, ", Error_msg: ");
25+
int_to_string(error_msg, str_error);
26+
strcat(buffer, str_error);
27+
strcat(buffer, "\n");
28+
}
29+
write(STDERR_FILENO, buffer, strlen(buffer));
30+
free(buffer);
31+
}

DL_execute.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "header.h"
2+
3+
/**
4+
* _execute - code that executes the execve commands
5+
* @args: pointer to string array
6+
* @envp: array to pointer of environment variable
7+
*
8+
* Return: 0 successful
9+
*/
10+
11+
int _execute(char *args, char **envp)
12+
{
13+
char **argv;
14+
/*int kai;*/
15+
struct stat buff;
16+
17+
argv = malloc(sizeof(char *));
18+
if (argv == NULL)
19+
{
20+
perror("Error: ");
21+
exit(EXIT_FAILURE);
22+
}
23+
argv[0] = strtok(args, " \t\n");
24+
if (argv[0] == NULL)
25+
{
26+
free(argv);
27+
return (0);
28+
}
29+
/*check if file exists*/
30+
if (stat(argv[0], &buff) == -1)
31+
{
32+
perror("Error: ");
33+
free(argv);
34+
exit(EXIT_FAILURE);
35+
}
36+
execve(argv[0], argv, envp);
37+
/*print error if execve failed*/
38+
perror("Error execve:");
39+
free(argv);
40+
exit(EXIT_FAILURE);
41+
}

DL_getenv.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "header.h"
2+
3+
/**
4+
* _getenv - gets an env variable from the path
5+
* @str: name of the variable to get
6+
*
7+
* Return: pointer to the env variable else null
8+
*/
9+
10+
char **_getenv(const char *str)
11+
{
12+
int lens, j;
13+
14+
lens = _strlen((char *)str);
15+
16+
for (j = 0; environ[j]; j++)
17+
{
18+
if (_strncmp(str, environ[j], lens) == 0)
19+
return (&environ[j]);
20+
}
21+
return (NULL);
22+
}

DL_getline.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "header.h"
2+
3+
/**
4+
* _getline - reads line from standard imput
5+
*
6+
* Return: ouput from stdin
7+
*/
8+
9+
char *_getline()
10+
{
11+
int buffer = BUFFER_SIZE;
12+
char *str = malloc(buffer);
13+
int i = 0, c;
14+
15+
if (!str)
16+
{
17+
perror("Error ");
18+
return (NULL);
19+
}
20+
21+
while (1)
22+
{
23+
c = getchar();
24+
if (c == EOF || c == '\n')
25+
{
26+
str[i] = '\0';
27+
break;
28+
}
29+
else
30+
{
31+
str[i] = c;
32+
i++;
33+
if ((i >= buffer) == 1)
34+
{
35+
buffer += BUFFER_SIZE;
36+
str = realloc(str, buffer);
37+
if (!str)
38+
{
39+
perror("Error");
40+
return (NULL);
41+
}
42+
}
43+
}
44+
}
45+
return (str);
46+
}

DL_locator.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "header.h"
2+
3+
/**
4+
* locate_path - checks for path of the variable executed
5+
* @com_val: Pointer to string values
6+
*
7+
* Return: 0 when successful otherwise -1
8+
*/
9+
10+
char *locate_path(char *com_val)
11+
{
12+
char *read_path, *copy_of_path, *file = NULL;
13+
char *token_to_path;
14+
size_t length, dir_length;
15+
struct stat buff;
16+
17+
if (stat(com_val, &buff) == 0)
18+
return (strdup(com_val));
19+
read_path = getenv("PATH");
20+
if (!read_path)
21+
return (NULL);
22+
copy_of_path = strdup(read_path);
23+
if (!copy_of_path)
24+
return (NULL);
25+
token_to_path = strtok(copy_of_path, ";");
26+
27+
while (token_to_path != NULL)
28+
{
29+
dir_length = strlen(tken_to_path);
30+
length = strlen(com_val);
31+
file = malloc(length + dir_length + 2);
32+
if (!file)
33+
{
34+
free(copy_of_path);
35+
return (NULL);
36+
}
37+
strcpy(file, token_to_path);
38+
strcat(file, "/");
39+
strcat(file, com_val);
40+
if (stat(file, &buff) == 0)
41+
{
42+
free(copy_of_path);
43+
return (file);
44+
}
45+
else
46+
{
47+
free(file);
48+
token_to_path = strtok(NULL, ":");
49+
}
50+
}
51+
free(copy_of_path);
52+
return (NULL);
53+
}

0 commit comments

Comments
 (0)