Skip to content

Commit 208ee7b

Browse files
authored
Merge pull request #6 from Demidorn/demi
Demi
2 parents 4eb672f + b53085e commit 208ee7b

14 files changed

+690
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DL_getline.c
2+
DL_strtok.c
3+
.DL_getline.c.swp
4+
/test

DL_envs.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "header.h"
2+
3+
/**
4+
* _printenv - prints path to the environment variable
5+
*
6+
* Return: nothing
7+
*/
8+
9+
void _printenv(void)
10+
{
11+
char **env = environ;
12+
int j;
13+
size_t len;
14+
15+
if (env == NULL)
16+
{
17+
perror("Error: environ is not available");
18+
return;
19+
}
20+
21+
for (j = 0; env[j] != NULL; j++)
22+
{
23+
len = 0;
24+
while (env[j][len] != '\0')
25+
len++;
26+
write(STDOUT_FILENO, env[j], len);
27+
write(STDOUT_FILENO, "\n", 1);
28+
}
29+
30+
}

DL_execute.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "header.h"
2+
3+
/**
4+
* execute_command - function to handle command execution based on user input
5+
* @get_address: input line from user
6+
* @env: environment variable
7+
*
8+
* Return: output executed command else -1
9+
*/
10+
11+
int execute_command(char *get_address, char __attribute__((__unused__)) **env)
12+
{
13+
char *delim = " \n\t\r";
14+
char **tokens = NULL, *command_path = NULL;
15+
int status = 0, ex = 0;
16+
17+
tokens = tokenize(get_address, delim);
18+
if (!tokens || tokens[0] == NULL)
19+
{
20+
perror("Error ");
21+
return (ex);
22+
}
23+
if (_strcmp(tokens[0], "exit") == 0)
24+
{
25+
if (tokens[1] != NULL)
26+
{
27+
status = atoi(tokens[1]);
28+
}
29+
free(tokens);
30+
free(command_path);
31+
exit(status);
32+
}
33+
if (_strcmp(tokens[0], "env") == 0)
34+
{
35+
_printenv();
36+
free(tokens);
37+
free(command_path);
38+
return (ex);
39+
}
40+
command_path = locate_path(tokens[0]);
41+
if (!command_path)
42+
{
43+
perror("command not found");
44+
free(tokens);
45+
ex = 127;
46+
return (ex);
47+
}
48+
execute_and_wait(command_path, tokens, &ex);
49+
return (ex);
50+
}

DL_getenv.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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: value of the env variable else null
8+
*/
9+
10+
char *_getenv(const char *str)
11+
{
12+
size_t len;
13+
char *value;
14+
int l;
15+
16+
len = _strlen(str);
17+
18+
if (str == NULL || environ == NULL)
19+
{
20+
perror("Error: ");
21+
return (NULL);
22+
}
23+
24+
for (l = 0; environ[l]; l++)
25+
{
26+
if (_strncmp(str, environ[l], len) == 0 && environ[l][len] == '=')
27+
{
28+
value = _strdup(environ[l] + len + 1);
29+
if (value == NULL)
30+
{
31+
perror("Error: ");
32+
return (NULL);
33+
}
34+
return (value);
35+
}
36+
}
37+
return (NULL);
38+
}
39+
40+
/**
41+
* read_input - function that reads a line of input
42+
* @get_address: poniter to store input
43+
* @length_input: stores length of the input line
44+
*
45+
* Return: number of characters read otherwise -1 on failure
46+
*/
47+
48+
ssize_t read_input(char **get_address, size_t *length_input)
49+
{
50+
char *prompt = "$ ";
51+
ssize_t read_line;
52+
int is_terminal = isatty(0);
53+
54+
if (is_terminal == 1)
55+
write(STDOUT_FILENO, prompt, 2);
56+
read_line = getline(get_address, length_input, stdin);
57+
return (read_line);
58+
}

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(token_to_path);
30+
length = _strlen(com_val);
31+
file = malloc(dir_length + 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+
}

DL_main.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "header.h"
2+
3+
/**
4+
* main - Entry point of the program that displays a prompt
5+
* whenever the function is called upon.
6+
* @argc: Number of commandline arguments.
7+
* @argv: Pointer array of strings containing commandline arguments.
8+
* @env: An array of strings containing environment variables.
9+
*
10+
* Return: 0 when successfull.
11+
*/
12+
13+
int main(int argc, char __attribute__((__unused__)) *argv[], char **env)
14+
{
15+
char *get_address = NULL;
16+
size_t length_input = 0;
17+
ssize_t read_line;
18+
int string_line;
19+
20+
(void)argc;
21+
22+
while (1)
23+
{
24+
read_line = read_input(&get_address, &length_input);
25+
if (read_line == -1)
26+
{
27+
free(get_address);
28+
exit(127);
29+
}
30+
if (get_address[0] != '\0')
31+
{
32+
string_line = execute_command(get_address, env);
33+
if (string_line != 0)
34+
return (string_line);
35+
}
36+
/*free(get_address);*/
37+
}
38+
return (0);
39+
}

DL_pid.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "header.h"
2+
3+
/**
4+
* execute_and_wait - executes a command and waits for its completion
5+
* @command_path: path command to execute
6+
* @tokens: array of commandline arguments
7+
* @e_status: pointer to store exit status of the command
8+
*
9+
* Return: 0 when succeful else 1
10+
*/
11+
12+
int execute_and_wait(char *command_path, char **tokens, int *e_status)
13+
{
14+
pid_t pid;
15+
int status;
16+
17+
pid = fork();
18+
if (pid == -1)
19+
{
20+
perror("Error");
21+
return (-1);
22+
}
23+
if (pid == 0)
24+
{
25+
execve(command_path, tokens, environ);
26+
perror("Execve");
27+
exit(2);
28+
}
29+
else
30+
{
31+
wait(&status);
32+
if (WIFEXITED(status))
33+
*e_status = WEXITSTATUS(status);
34+
else
35+
*e_status = -1;
36+
free(command_path);
37+
free(tokens);
38+
}
39+
return (0);
40+
}

DL_stringHandlers1.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include "header.h"
2+
3+
/**
4+
* int_to_string - converts integer to string
5+
* @result: array to hold string
6+
* @value: integer value to be converted
7+
*
8+
* Return: converted string
9+
*/
10+
11+
void int_to_string(int value, char *result)
12+
{
13+
int temp, position, d, num = 0;
14+
15+
if (value == 0)
16+
{
17+
result[0] = '0';
18+
result[1] = '\0';
19+
return;
20+
}
21+
temp = value;
22+
23+
while (temp != 0)
24+
{
25+
temp /= 10;
26+
num++;
27+
}
28+
position = num;
29+
result[position] = '\0';
30+
31+
while (value != 0)
32+
{
33+
position--;
34+
d = value % 10;
35+
result[position] = '0' + d;
36+
value /= 10;
37+
}
38+
}
39+
40+
/**
41+
* _strcmp - compares two strings
42+
* @s1: First string
43+
* @s2: Second string
44+
* Return: The value
45+
*/
46+
47+
int _strcmp(char *s1, char *s2)
48+
{
49+
int i = 0;
50+
51+
while (s1[i] != '\0')
52+
{
53+
if (s1[i] != s2[i])
54+
break;
55+
i++;
56+
}
57+
return (s1[i] - s2[i]);
58+
}
59+
60+
/**
61+
* _strcat - concantnates two strings
62+
* @dest: Destination string
63+
* @src: Source string
64+
* Return: Result of two strings together
65+
*/
66+
67+
char *_strcat(char *dest, const char *src)
68+
{
69+
int i = 0, j;
70+
71+
while (dest[i] != '\0')
72+
{
73+
i++;
74+
}
75+
for (j = 0; src[j] != 0; j++)
76+
{
77+
dest[i] = src[j];
78+
i++;
79+
}
80+
dest[i] = '\0';
81+
return (dest);
82+
}
83+
84+
/**
85+
* _strncmp - compares amount of characters of two strings
86+
* @s1: first string
87+
* @s2: second string
88+
* @s: amount of characters to compare
89+
* Return: 0 (successful) otherwise 1
90+
*/
91+
int _strncmp(const char *s1, const char *s2, size_t s)
92+
{
93+
size_t j;
94+
95+
if (s1 == NULL)
96+
return (-1);
97+
98+
for (j = 0; j < s && s2[j]; j++)
99+
{
100+
if (s1[j] != s2[j])
101+
{
102+
return (1);
103+
}
104+
}
105+
return (0);
106+
}

0 commit comments

Comments
 (0)