Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
| 9. setenv, unsetenv | [SOON](./) |
| 10. cd | [SOON](./) |
| 11. ; | [SOON](./) |
| 12. && and || | [SOON](./) |
| 12. && and \|\| | [SOON](./) |
| 13. alias | [SOON](./) |
| 14. Variables | [SOON](./) |
| 15. Comments | [SOON](./) |
Expand Down
68 changes: 68 additions & 0 deletions _atoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <limits.h>

/**
* _isdigit - checks for a digit (0 through 9).
*
* @c: char to be checked
*
* Return: 1 if @c is digit, 0 otherwise
*/
int _isdigit(int c)
{
if (c >= 48 && c <= 57)
{
return (1);
}
else
{
return (0);
}
}

/**
* _atoi - convert a string to an integer.
*
* @s: string to be converted.
*
* Return: the converted number.
*/
int _atoi(char *s)
{
char *ptr;
int size, sign, for_counter, num_size, prev, cur;

size = prev = 0;
sign = 1;
ptr = s;
while (*ptr != '\0')
{
if (*ptr == '-')
{
sign = sign * -1;
ptr = ptr + 1;
continue;
}

if (_isdigit(*ptr))
{
if ((*(ptr + 1) == ' ') || (*(ptr + 1) == '\0'))
{
num_size = size;
for (for_counter = 0; for_counter <= size; for_counter++)
{
cur = *(ptr - num_size) - '0';
if ((prev > (INT_MAX / 10)) || ((INT_MAX - (prev * 10)) < cur))
{
return (sign == 1 ? INT_MAX : INT_MIN);
}
prev = prev * 10 + cur;
num_size--;
}
return (prev * sign);
}
size = size + 1;
}
ptr = ptr + 1;
}
return (0);
}
55 changes: 55 additions & 0 deletions _realloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "main.h"

/**
* _memcpy - copies memory area.
*
* @dest: destination location.
* @src: source location.
* @size: size of buffer to be copied.
*
* Return: pointer to dest.
*/
char *_memcpy(char *dest, char *src, unsigned int size)
{
unsigned int index;

for (index = 0; index < size; index++)
{
dest[index] = src[index];
}
return (dest);
}
/**
* _realloc - reallocates a memory block using malloc and free.
*
* @ptr: old memory location.
* @old_size: old size of the buffer.
* @new_size: new size of the allocated memory.
*
* Return: pointer to the new allocated memory.
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *new_ptr;

if (new_size == old_size)
return (ptr);

if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}

if (ptr == NULL)
return (malloc(new_size));

if (new_size > old_size)
{
new_ptr = malloc(new_size);
_memcpy(new_ptr, ptr, old_size);
free(ptr);
}

return (new_ptr);
}
28 changes: 28 additions & 0 deletions _strcat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "main.h"

/**
* _strcat - Concatenates two strings.
*
* @dest: destination var.
* @src: source buffer.
*
* Return: Pointer to the resulting string dest.
*/
char *_strcat(char *dest, char *src)
{
int i1, i2;

i1 = i2 = 0;
while (dest[i1] != '\0')
{
i1++;
}
while (src[i2] != '\0')
{
dest[i1] = src[i2];
i1++;
i2++;
}
dest[i1] = src[i2];
return (dest);
}
26 changes: 26 additions & 0 deletions _strcmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "main.h"

/**
* _strcmp - compares two strings.
*
* @s1: string no 1.
* @s2: string no 2.
*
* Return: positive num, zero and negative num.
*/
int _strcmp(char *s1, char *s2)
{
int index = 0;
int value = 0;

while ((*(s1 + index) != '\0') && (*(s2 + index) != '\0'))
{
if (*(s1 + index) != *(s2 + index))
{
value = (*(s1 + index) - *(s2 + index));
break;
}
index++;
}
return (value);
}
32 changes: 32 additions & 0 deletions _strdup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "main.h"

/**
* _strdup - returns a pointer to anewly allocated space in memory,
* which contains a copy of the string given as parameter.
*
* @string: string to be copied.
*
* Return: pointer to the newly allocated space in memory, or NULL
* in some conditions.
*/
char *_strdup(char *string)
{
unsigned int index, size;
char *ptr;

if (string == NULL)
return (NULL);

for (size = 0; string[size];)
size++;

ptr = malloc((size + 1) * sizeof(char));

if (ptr == NULL)
return (NULL);

for (index = 0; index <= size; index++)
ptr[index] = string[index];

return (ptr);
}
22 changes: 22 additions & 0 deletions _strlen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "main.h"

/**
* _strlen - return the length of a string.
*
* @s: string.
*
* Return: length of the string.
*/
int _strlen(char *s)
{
char *ptr;
int size = 0;

ptr = s;
while (*ptr != '\0')
{
size = sizeof(*ptr) + size;
ptr = ptr + 1;
}
return (size);
}
27 changes: 27 additions & 0 deletions _strncmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "main.h"

/**
* _strncmp - compares n characters of two strings .
*
* @s1: string no 1.
* @s2: string no 2.
* @n: number of characters to be compared.
*
* Return: positive num, zero and negative num.
*/
int _strncmp(char *s1, char *s2, int n)
{
int index = 0, value = 0, counter = 0;

while ((*(s1 + index) != '\0') && (*(s2 + index) != '\0') && counter < n)
{
if (*(s1 + index) != *(s2 + index))
{
value = (*(s1 + index) - *(s2 + index));
break;
}
index++;
counter++;
}
return (value);
}
27 changes: 27 additions & 0 deletions built_in_cmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "main.h"

/**
* built_in - checks whether the command is build int command or not.
*
* @cmd: command.
*
* Return: pointer to function.
*/
int (*built_in(char *cmd))(Commands *, int *, char *, char *)
{
built_in_t commands[] = {
{"env", _env},
{"exit", ___exit},
{NULL, NULL}
};
int index;

index = 0;
while (commands[index].cmd != NULL)
{
if (_strcmp(cmd, commands[index].cmd) == 0)
return (commands[index].func);
index++;
}
return (NULL);
}
28 changes: 28 additions & 0 deletions built_in_env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "main.h"

/**
* _env - prints the environment variables.
*
* @command: the command that the user type.
* @lastSignal: last signal from executed functions.
* @programName: the running executable name.
* @buffer: the original buffer from getline function.
*
* Return: (0) always success, (1) otherwise.
*/
int _env(__attribute__((unused)) Commands * command,
int *lastSignal, char *programName, char *buffer)
{
size_t index, stringLen;

(void) lastSignal;
(void) buffer;
(void) programName;
for (index = 0; environ[index] != NULL; index++)
{
stringLen = _strlen(environ[index]);
write(STDOUT_FILENO, environ[index], stringLen);
write(STDOUT_FILENO, "\n", 1);
}
return (0);
}
41 changes: 41 additions & 0 deletions built_in_exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "main.h"

/**
* ___exit - exits the shell.
*
* @CMD: Command type single list.
* @lastSignal: last signal from executed functions.
* @programName: the running executable name.
* @buffer: the original buffer from getline function.
*
* Return: (0) always success, (EXIT_CODE) otherwise.
*/
int ___exit(Commands *CMD, int *lastSignal, char *programName, char *buffer)
{
int argsLen, status;
char **command = CMD->cmd;
char *msg = ": 1: exit: Illegal number: ";

for (argsLen = 0; command[argsLen];)
argsLen++;

fflush(NULL);
free(buffer);
if (argsLen > 1)
{
status = _atoi(command[1]);
if (status == 0 && command[1][0] != '0')
{
write(STDERR_FILENO, programName, _strlen(programName));
write(STDERR_FILENO, msg, _strlen(msg));
write(STDERR_FILENO, command[1], _strlen(command[1]));
write(STDERR_FILENO, "\n", 1);
status = 2;
}
free_commands(CMD);
exit(status);
}

free_commands(CMD);
exit(*lastSignal);
}
Loading