aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--Makefile29
-rw-r--r--auteur1
-rw-r--r--ft_atoi.c33
-rw-r--r--ft_bzero.c10
-rw-r--r--ft_isalnum.c6
-rw-r--r--ft_isalpha.c4
-rw-r--r--ft_isascii.c6
-rw-r--r--ft_isdigit.c4
-rw-r--r--ft_isprint.c4
-rw-r--r--ft_memccpy.c22
-rw-r--r--ft_memchr.c17
-rw-r--r--ft_memcmp.c19
-rw-r--r--ft_memcpy.c16
-rw-r--r--ft_memmove.c38
-rw-r--r--ft_memset.c11
-rw-r--r--ft_strcat.c17
-rw-r--r--ft_strchr.c17
-rw-r--r--ft_strcmp.c9
-rw-r--r--ft_strcpy.c13
-rw-r--r--ft_strdup.c22
-rw-r--r--ft_strlcat.c24
-rw-r--r--ft_strlen.c11
-rw-r--r--ft_strncat.c19
-rw-r--r--ft_strncmp.c15
-rw-r--r--ft_strncpy.c16
-rw-r--r--ft_strnstr.c29
-rw-r--r--ft_strrchr.c16
-rw-r--r--ft_strstr.c27
-rw-r--r--ft_tolower.c12
-rw-r--r--ft_toupper.c8
-rw-r--r--libft.h34
32 files changed, 512 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index d7756c2..e0a9dfe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,5 @@
a.out
*.o
+*.so
+*.a
+*.pdf
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..5389a10
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,29 @@
+CC = gcc
+CCFLAGS = -Wall -Wextra -Werror
+
+NAME = libft.a
+SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c ft_isprint.c \
+ ft_memccpy.c ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_strcat.c \
+ ft_strchr.c ft_strcmp.c ft_strcpy.c ft_strdup.c ft_strlcat.c ft_strlen.c ft_strncat.c \
+ ft_strncmp.c ft_strncpy.c ft_strnstr.c ft_strrchr.c ft_strstr.c ft_tolower.c \
+ ft_toupper.c
+OBJ = $(SRC:.c=.o)
+
+all: $(NAME)
+
+$(NAME): $(OBJ) libft.h
+ ar rc $(NAME) $(OBJ)
+
+%.o: %.c
+ $(CC) $(CCFLAGS) -c -o $@ $<
+
+clean:
+ rm -f $(OBJ)
+
+fclean: clean
+ rm -f $(NAME)
+
+re: fclean all
+
+so: $(OBJ) libft.h
+ $(CC) -shared -Wl,-soname,libft.so -o libft.so $(OBJ)
diff --git a/auteur b/auteur
new file mode 100644
index 0000000..718becd
--- /dev/null
+++ b/auteur
@@ -0,0 +1 @@
+cacharle
diff --git a/ft_atoi.c b/ft_atoi.c
new file mode 100644
index 0000000..94c42cc
--- /dev/null
+++ b/ft_atoi.c
@@ -0,0 +1,33 @@
+#define MIN_INT (1 << 31)
+#define MAX_INT (~(1 << 31))
+
+int ft_atoi(const char *nptr)
+{
+ unsigned int nb;
+ int i;
+ int is_negative;
+
+ while (*nptr == ' ' || *nptr == '\t'|| *nptr == '\n'
+ || *nptr == '\v'|| *nptr == '\f'|| *nptr == '\r')
+ nptr++;
+ is_negative = 0;
+ if (*nptr == '-' || *nptr == '+')
+ {
+ if (*nptr == '-')
+ is_negative = 1;
+ nptr++;
+ }
+ i = 0;
+ nb = 0;
+ while (nptr[i] >= '0' && nptr[i] <= '9')
+ {
+ if (!is_negative && nb > (unsigned int)MAX_INT)
+ return (-1);
+ else if (nb > (unsigned int)MIN_INT)
+ return (0);
+ nb *= 10;
+ nb += nptr[i] - '0';
+ i++;
+ }
+ return ((int)(is_negative ? -nb : nb));
+}
diff --git a/ft_bzero.c b/ft_bzero.c
new file mode 100644
index 0000000..e936cf1
--- /dev/null
+++ b/ft_bzero.c
@@ -0,0 +1,10 @@
+#include <string.h>
+
+void ft_bzero(void *s, size_t n)
+{
+ char *s_char_ptr;
+
+ s_char_ptr = (char*)s;
+ while (n-- > 0)
+ s_char_ptr[n] = 0;
+}
diff --git a/ft_isalnum.c b/ft_isalnum.c
new file mode 100644
index 0000000..d98da91
--- /dev/null
+++ b/ft_isalnum.c
@@ -0,0 +1,6 @@
+#include "libft.h"
+
+int ft_isalnum(int c)
+{
+ return (ft_isalpha(c) || ft_isdigit(c));
+}
diff --git a/ft_isalpha.c b/ft_isalpha.c
new file mode 100644
index 0000000..cd09abb
--- /dev/null
+++ b/ft_isalpha.c
@@ -0,0 +1,4 @@
+int ft_isalpha(int c)
+{
+ return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
+}
diff --git a/ft_isascii.c b/ft_isascii.c
new file mode 100644
index 0000000..7742ded
--- /dev/null
+++ b/ft_isascii.c
@@ -0,0 +1,6 @@
+#define MAX_CHAR ((1 << 7) - 1)
+
+int ft_isascii(int c)
+{
+ return (c >= 0 && c <= MAX_CHAR);
+}
diff --git a/ft_isdigit.c b/ft_isdigit.c
new file mode 100644
index 0000000..a41e1fd
--- /dev/null
+++ b/ft_isdigit.c
@@ -0,0 +1,4 @@
+int ft_isdigit(int c)
+{
+ return (c >= '0' && c <= '9');
+}
diff --git a/ft_isprint.c b/ft_isprint.c
new file mode 100644
index 0000000..6834120
--- /dev/null
+++ b/ft_isprint.c
@@ -0,0 +1,4 @@
+int ft_isprint(int c)
+{
+ return (c >= ' ' && c <= '~');
+}
diff --git a/ft_memccpy.c b/ft_memccpy.c
new file mode 100644
index 0000000..9cc483a
--- /dev/null
+++ b/ft_memccpy.c
@@ -0,0 +1,22 @@
+#include <string.h>
+
+void *ft_memccpy(void *dest, const void *src, int c, size_t n)
+{
+ size_t i;
+ unsigned char *uc_dest;
+ unsigned char *uc_src;
+
+ uc_dest = (unsigned char*)dest;
+ uc_src = (unsigned char*)src;
+ i = 0;
+ while (i < n)
+ {
+ uc_dest[i] = uc_src[i];
+ if (uc_dest[i] == (unsigned char)c)
+ break ;
+ i++;
+ }
+ if (i == n)
+ return (NULL);
+ return (dest + i + 1);
+}
diff --git a/ft_memchr.c b/ft_memchr.c
new file mode 100644
index 0000000..662a6c7
--- /dev/null
+++ b/ft_memchr.c
@@ -0,0 +1,17 @@
+#include <string.h>
+
+void *ft_memchr(const void *s, int c, size_t n)
+{
+ size_t i;
+ unsigned char *uc_s;
+
+ uc_s = (unsigned char*)s;
+ i = 0;
+ while (i < n)
+ {
+ if (uc_s[i] == (unsigned char)c)
+ return (uc_s + i);
+ i++;
+ }
+ return (NULL);
+}
diff --git a/ft_memcmp.c b/ft_memcmp.c
new file mode 100644
index 0000000..76cd47d
--- /dev/null
+++ b/ft_memcmp.c
@@ -0,0 +1,19 @@
+#include <string.h>
+
+int ft_memcmp(const void *s1, const void *s2, size_t n)
+{
+ size_t i;
+ unsigned char *uc_s1;
+ unsigned char *uc_s2;
+
+ uc_s1 = (unsigned char*)s1;
+ uc_s2 = (unsigned char*)s2;
+ i = 0;
+ if (n == 0)
+ return (0);
+ while (i < n && uc_s1[i] == uc_s2[i])
+ i++;
+ if (i == n)
+ i--;
+ return (uc_s1[i] - uc_s2[i]);
+}
diff --git a/ft_memcpy.c b/ft_memcpy.c
new file mode 100644
index 0000000..1ac2708
--- /dev/null
+++ b/ft_memcpy.c
@@ -0,0 +1,16 @@
+#include <string.h>
+
+void *ft_memcpy(void *dest, const void *src, size_t n)
+{
+ size_t i;
+
+ if (dest == NULL && src == NULL)
+ return (NULL);
+ i = 0;
+ while (i < n)
+ {
+ *((char*)dest + i) = *((char*)src + i);
+ i++;
+ }
+ return (dest);
+}
diff --git a/ft_memmove.c b/ft_memmove.c
new file mode 100644
index 0000000..54555cb
--- /dev/null
+++ b/ft_memmove.c
@@ -0,0 +1,38 @@
+#include <stdlib.h>
+#include <string.h>
+
+#define BUF_SIZE 4096
+
+void *ft_memmove(void *dest, const void *src, size_t n)
+{
+ size_t i;
+ size_t j;
+ size_t k;
+ unsigned char tmp[BUF_SIZE];
+ unsigned char *uc_dest;
+ unsigned char *uc_src;
+
+ if (dest == NULL && src == NULL)
+ return (NULL);
+ uc_dest = (unsigned char*) dest;
+ uc_src = (unsigned char*) src;
+ i = 0;
+ while (i < n)
+ {
+ j = 0;
+ while (j < BUF_SIZE && i < n)
+ {
+ tmp[j] = uc_src[i];
+ j++;
+ i++;
+ }
+ k = 0;
+ while (k < j)
+ {
+ uc_dest[k] = tmp[k];
+ k++;
+ }
+ i++;
+ }
+ return (dest);
+}
diff --git a/ft_memset.c b/ft_memset.c
new file mode 100644
index 0000000..e09db6c
--- /dev/null
+++ b/ft_memset.c
@@ -0,0 +1,11 @@
+#include <string.h>
+
+void *ft_memset(void *s, int c, size_t n)
+{
+ unsigned char uchar_c;
+
+ uchar_c = (unsigned char)c;
+ while (n-- > 0)
+ ((unsigned char*)s)[n] = uchar_c;
+ return (s);
+}
diff --git a/ft_strcat.c b/ft_strcat.c
new file mode 100644
index 0000000..5f03164
--- /dev/null
+++ b/ft_strcat.c
@@ -0,0 +1,17 @@
+char *ft_strcat(char *dest, const char *src)
+{
+ int i;
+ int j;
+
+ i = 0;
+ while (dest[i])
+ i++;
+ j = 0;
+ while (src[j])
+ {
+ dest[i + j] = src[j];
+ j++;
+ }
+ dest[i + j] = '\0';
+ return (dest);
+}
diff --git a/ft_strchr.c b/ft_strchr.c
new file mode 100644
index 0000000..67b969e
--- /dev/null
+++ b/ft_strchr.c
@@ -0,0 +1,17 @@
+#include <stdlib.h>
+
+char *ft_strchr(const char *s, int c)
+{
+ char *cursor;
+
+ cursor = (char*)s;
+ while (*cursor)
+ {
+ if (*cursor == (char)c)
+ return (cursor);
+ cursor++;
+ }
+ if (c == 0)
+ return (cursor);
+ return (NULL);
+}
diff --git a/ft_strcmp.c b/ft_strcmp.c
new file mode 100644
index 0000000..8a86b2a
--- /dev/null
+++ b/ft_strcmp.c
@@ -0,0 +1,9 @@
+int ft_strcmp(const char *s1, const char *s2)
+{
+ int i;
+
+ i = 0;
+ while (s1[i] && s2[i] && s1[i] == s2[i])
+ i++;
+ return ((unsigned char)s1[i] - (unsigned char)s2[i]);
+}
diff --git a/ft_strcpy.c b/ft_strcpy.c
new file mode 100644
index 0000000..8f3946d
--- /dev/null
+++ b/ft_strcpy.c
@@ -0,0 +1,13 @@
+char *ft_strcpy(char *dest, const char *src)
+{
+ int i;
+
+ i = 0;
+ while (src[i])
+ {
+ dest[i] = src[i];
+ i++;
+ }
+ dest[i] = '\0';
+ return (dest);
+}
diff --git a/ft_strdup.c b/ft_strdup.c
new file mode 100644
index 0000000..e3609cc
--- /dev/null
+++ b/ft_strdup.c
@@ -0,0 +1,22 @@
+#include <stdlib.h>
+#include "libft.h"
+
+char *ft_strdup(const char *s)
+{
+ char *clone;
+ size_t i;
+ size_t len;
+
+ len = ft_strlen(s);
+ if ((clone = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
+ return (NULL);
+ i = 0;
+ while (i < len)
+ {
+ clone[i] = s[i];
+ i++;
+ }
+ clone[i] = '\0';
+ return (clone);
+}
+
diff --git a/ft_strlcat.c b/ft_strlcat.c
new file mode 100644
index 0000000..6c550f3
--- /dev/null
+++ b/ft_strlcat.c
@@ -0,0 +1,24 @@
+#include <string.h>
+#include "libft.h"
+
+size_t ft_strlcat(char *dst, const char *src, size_t size)
+{
+ size_t i;
+ size_t dst_len;
+ size_t src_len;
+
+ dst_len = ft_strlen(dst);
+ src_len = ft_strlen(src);
+ if (size == 0)
+ return (src_len);
+ i = dst_len;
+ while (i < size - 1)
+ {
+ dst[i] = src[i - dst_len];
+ i++;
+ }
+ if (dst[size - 1] != '\0')
+ return (src_len + size);
+ dst[size - 1] = '\0';
+ return (dst_len + src_len);
+}
diff --git a/ft_strlen.c b/ft_strlen.c
new file mode 100644
index 0000000..9568fa2
--- /dev/null
+++ b/ft_strlen.c
@@ -0,0 +1,11 @@
+#include <string.h>
+
+size_t ft_strlen(const char *s)
+{
+ size_t counter;
+
+ counter = 0;
+ while (s[counter])
+ counter++;
+ return (counter);
+}
diff --git a/ft_strncat.c b/ft_strncat.c
new file mode 100644
index 0000000..41e9f67
--- /dev/null
+++ b/ft_strncat.c
@@ -0,0 +1,19 @@
+#include <string.h>
+
+char *ft_strncat(char *dest, const char *src, size_t n)
+{
+ size_t i;
+ size_t j;
+
+ i = 0;
+ while (dest[i])
+ i++;
+ j = 0;
+ while (j < n && src[j])
+ {
+ dest[i + j] = src[j];
+ j++;
+ }
+ dest[i + j] = '\0';
+ return (dest);
+}
diff --git a/ft_strncmp.c b/ft_strncmp.c
new file mode 100644
index 0000000..0f24a1e
--- /dev/null
+++ b/ft_strncmp.c
@@ -0,0 +1,15 @@
+#include <string.h>
+
+int ft_strncmp(const char *s1, const char *s2, size_t n)
+{
+ size_t i;
+
+ if (n == 0)
+ return (0);
+ i = 0;
+ while (s1[i] == s2[i] && s1[i] && s2[i] && i < n)
+ i++;
+ if (i == n)
+ i--;
+ return ((unsigned char)s1[i] - (unsigned char)s2[i]);
+}
diff --git a/ft_strncpy.c b/ft_strncpy.c
new file mode 100644
index 0000000..100c813
--- /dev/null
+++ b/ft_strncpy.c
@@ -0,0 +1,16 @@
+#include <string.h>
+
+char *ft_strncpy(char *dest, const char *src, size_t n)
+{
+ size_t i;
+
+ i = 0;
+ while (src[i] && i < n)
+ {
+ dest[i] = src[i];
+ i++;
+ }
+ while (i < n)
+ dest[i++] = '\0';
+ return (dest);
+}
diff --git a/ft_strnstr.c b/ft_strnstr.c
new file mode 100644
index 0000000..2308a57
--- /dev/null
+++ b/ft_strnstr.c
@@ -0,0 +1,29 @@
+#include <stdlib.h>
+#include <string.h>
+#include "libft.h"
+
+char *ft_strnstr(const char *big, const char *little, size_t len)
+{
+ size_t i;
+ size_t j;
+ size_t min_len;
+
+ min_len = len > ft_strlen(little) ? ft_strlen(little) : len;
+ if (min_len == 0)
+ return ((char*)big);
+ i = 0;
+ while (big[i])
+ {
+ j = 0;
+ while (little[j] && big[i + j])
+ {
+ if (little[j] != big[i + j])
+ break ;
+ j++;
+ if (j == min_len)
+ return ((char*)(big + i));
+ }
+ i++;
+ }
+ return (NULL);
+}
diff --git a/ft_strrchr.c b/ft_strrchr.c
new file mode 100644
index 0000000..7f01501
--- /dev/null
+++ b/ft_strrchr.c
@@ -0,0 +1,16 @@
+#include <string.h>
+#include "libft.h"
+
+char *ft_strrchr(const char *s, int c)
+{
+ size_t i;
+
+ i = ft_strlen(s);
+ while (s[i] != (char)c)
+ {
+ if (i == 0)
+ return (NULL);
+ i--;
+ }
+ return ((char*)s + i);
+}
diff --git a/ft_strstr.c b/ft_strstr.c
new file mode 100644
index 0000000..0209de4
--- /dev/null
+++ b/ft_strstr.c
@@ -0,0 +1,27 @@
+#include <stdlib.h>
+#include <string.h>
+#include "libft.h"
+
+char *ft_strstr(const char *haystack, const char *needle)
+{
+ size_t i;
+ char *cursor;
+
+ cursor = (char*)haystack;
+ if (!ft_strlen(needle))
+ return (cursor);
+ while (*cursor)
+ {
+ i = 0;
+ while (needle[i] && cursor[i])
+ {
+ if (needle[i] != cursor[i])
+ break ;
+ i++;
+ }
+ if (i == ft_strlen(needle))
+ return (cursor);
+ cursor++;
+ }
+ return (NULL);
+}
diff --git a/ft_tolower.c b/ft_tolower.c
new file mode 100644
index 0000000..5b5ee01
--- /dev/null
+++ b/ft_tolower.c
@@ -0,0 +1,12 @@
+int ft_tolower(int c)
+{
+ unsigned char uc;
+
+ uc = c;
+ /* if (c < -1) */
+ /* return (c + 256); */
+
+ if (uc >= 'A' && uc <= 'Z')
+ return (uc - 'A' + 'a');
+ return (c);
+}
diff --git a/ft_toupper.c b/ft_toupper.c
new file mode 100644
index 0000000..7e85f8d
--- /dev/null
+++ b/ft_toupper.c
@@ -0,0 +1,8 @@
+#include "libft.h"
+
+int ft_toupper(int c)
+{
+ if (c >= 'a' && c <= 'z')
+ return (c - 'a' + 'A');
+ return (c);
+}
diff --git a/libft.h b/libft.h
new file mode 100644
index 0000000..0b110e9
--- /dev/null
+++ b/libft.h
@@ -0,0 +1,34 @@
+#ifndef LIBFT_H
+# define LIBFT_H
+
+# include <string.h>
+
+void *ft_memset(void *s, int c, size_t n);
+void ft_bzero(void *s, size_t n);
+void *ft_memcpy(void *dest, const void *src, size_t n);
+void *ft_memccpy(void *dest, const void *src, int c, size_t n);
+void *ft_memmove(void *dest, const void *src, size_t n);
+void *ft_memchr(const void *s, int c, size_t n);
+int ft_memcmp(const void *s1, const void *s2, size_t n);
+size_t ft_strlen(const char *s);
+char *ft_strdup(const char *s);
+char *ft_strcpy(char *dest, const char *src);
+char *ft_strncpy(char *dest, const char *src, size_t n);
+char *ft_strcat(char *dest, const char *src);
+char *ft_strncat(char *dest, const char *src, size_t n);
+size_t ft_strlcat(char *dst, const char *src, size_t size);
+char *ft_strchr(const char *s, int c);
+char *ft_strrchr(const char *s, int c);
+char *ft_strstr(const char *haystack, const char *needle);
+char *ft_strnstr(const char *big, const char *little, size_t len);
+int ft_strcmp(const char *s1, const char *s2);
+int ft_strncmp(const char *s1, const char *s2, size_t n);
+int ft_atoi(const char *nptr);
+int ft_isalpha(int c);
+int ft_isdigit(int c);
+int ft_isalnum(int c);
+int ft_isascii(int c);
+int ft_isprint(int c);
+int ft_toupper(int c);
+
+#endif