Libft - C Standard Library Rewrite

Remade some of the standard C library functions, plus additional functions that I thought would be useful in future projects.

By doing this project, I gained a thorough understanding of data structures, and hands-on debugging.

Language: C


Git Repo


ft_atoi

#include "libft.h" int ft_atoi(const char *str) { size_t i; size_t z; size_t sign; i = 0; z = 0; sign = 1; while (ft_isspace(str[z])) z++; if (str == '\0') return (0); if (str[z] == '-' || str[z] == '+') { sign = (str[z] == '-' ? -1 : 1); z++; } while (str[z] && str[z] >= '0' && str[z] <= '9') { i = i * 10 + str[z] - '0'; z++; } return (i * sign); }

ft_bzero

#include "libft.h" void ft_bzero(void *s, size_t n) { ft_memset(s, 0, n); }

ft_isalnum

int ft_isalnum(int c) { if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) return (1); return (0); }

ft_isalpha

int ft_isalpha(int c) { if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) return (1); return (0); }

ft_isascii

nt ft_isascii(int c) { if (0 <= c && c <= 127) return (1); return (0); }

ft_isdigit

int ft_isdigit(int c) { if ('0' <= c && c <= '9') return (1); return (0); }

ft_islower

int ft_islower(int c) { if (c <= 'z' && c >= 'a') return (1); return (0); }

ft_isprint

int ft_isprint(int c) { if (32 <= c && c <= 126) return (1); return (0); }

ft_isspace

int ft_isspace(int c) { if ((c == ' ' || c == '\t' || c == '\v' || c == '\n' || c == '\f' || c == '\r')) return (1); return (0); }

ft_isupper

int ft_isupper(int c) { if (c <= 'Z' && c >= 'A') return (1); return (0); }

ft_itoa

#include "libft.h" static void ft_isnegative(char *s, int n, size_t len) { char *stemp; int ntemp; size_t ltemp; stemp = s; ntemp = n; ltemp = len; if (ntemp < 0) { stemp[ltemp--] = '-'; stemp[ltemp + 2] = '\0'; } if (ntemp > 0) stemp[ltemp++] = '\0'; } static size_t count_numbers(int n) { size_t i; i = 1; while (n /= 10) i++; return (i); } static char *ft_iszero(char *c, int n) { if (n == 0) *c = '0'; return (c); } char *ft_itoa(int n) { char *s; size_t i; size_t len; int alts; len = count_numbers(n); i = 0; s = 0; if (n == -2147483648) return (ft_strdup("-2147483648")); if (!(s = ft_strnew(len))) return (NULL); if (n < 0) alts = n * -1; if (n > 0) alts = n; ft_iszero(s, n); while (i <= len && n != 0) { s[i++] = alts % 10 + '0'; alts /= 10; } ft_isnegative(s, n, len); ft_revstr(s); return (s); }

ft_lstadd

#include "libft.h" void ft_lstadd(t_list **alst, t_list *new) { if (alst && new) { new->next = *alst; *alst = new; } }

ft_lstdel

#include "libft.h" void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) { t_list *elem; elem = *alst; while (elem) { elem = elem->next; ft_lstdelone(alst, del); *alst = elem; } }

ft_lstdelone

#include "libft.h" void ft_lstdelone(t_list **alst, void (*del)(void*, size_t)) { del((*alst)->content, (*alst)->content_size); free(*alst); *alst = NULL; }

ft_lstiter

#include "libft.h" void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) { if (lst) { while (lst != NULL) { f(lst); lst = lst->next; } } }

ft_lstmap

#include "libft.h" t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) { t_list *element; t_list *noob; if (!lst || !f) return (NULL); element = f(lst); noob = element; while (lst->next) { lst = lst->next; if (!(element->next = f(lst))) { free(element->next); return (NULL); } else element = element->next; } return (noob); }

ft_lstnew

#include "libft.h" t_list *ft_lstnew(void const *content, size_t content_size) { t_list *elem; if (!(elem = (t_list *)malloc(sizeof(*elem)))) return (NULL); if (!content) { elem->content = NULL; elem->content_size = 0; } if (content) { if (!((elem->content = malloc(content_size)))) return (NULL); ft_memcpy(elem->content, content, content_size); elem->content_size = content_size; } elem->next = NULL; return (elem); }

ft_memalloc

#include "libft.h" void *ft_memalloc(size_t size) { void *magic; magic = malloc(size); if (magic) ft_bzero(magic, size); return (magic); }

ft_memccpy

#include "libft.h" void *ft_memccpy(void *dst, const void *src, int c, size_t n) { size_t duesvault; char *altdst; char *altsrc; duesvault = 0; altdst = (char*)dst; altsrc = (char*)src; while (n > 0) { altdst[duesvault] = altsrc[duesvault]; if (altsrc[duesvault] == (char)c) return (altdst + duesvault + 1); duesvault++; n--; } return (NULL); }

ft_memchr

#include "libft.h" void *ft_memchr(const void *s, int c, size_t n) { unsigned char *alts; size_t i; alts = (unsigned char *)s; i = 0; while (n > i) { if (alts[i] == (unsigned char)c) return (&alts[i]); i++; } return (NULL); }

ft_memcmp

#include "libft.h" int ft_memcmp(const void *s1, const void *s2, size_t n) { unsigned char *source1; unsigned char *source2; source1 = (unsigned char*)s1; source2 = (unsigned char*)s2; while (n > 0) { if (*source1 != *source2) return (*source1 - *source2); source1++; source2++; n--; } return (0); }

ft_memcpy

#include "libft.h" void *ft_memcpy(void *dst, const void *src, size_t n) { unsigned char *altdst; unsigned char *altsrc; altdst = (unsigned char *)dst; altsrc = (unsigned char *)src; while (n > 0) { *altdst = *altsrc; altdst++; altsrc++; n--; } return (dst); }

ft_memdel

#include "libft.h" void ft_memdel(void **ap) { if (ap) { free(*ap); *ap = NULL; } }

ft_memmove

#include "libft.h" void *ft_memmove(void *dst, const void *src, size_t len) { unsigned char *temp1; unsigned char *temp2; temp1 = dst; temp2 = (unsigned char*)src; if (dst < src) return (ft_memcpy(dst, src, len)); if (dst > src) while (len--) temp1[len] = temp2[len]; return (dst); }

ft_memset

#include "libft.h" void *ft_memset(void *b, int c, size_t len) { unsigned char *placeholder; placeholder = (unsigned char *)b; while (len > 0) { *placeholder = (unsigned char)c; placeholder++; len--; } return (b); }

ft_putchar

#include "libft.h" void ft_putchar(char c) { ft_putchar_fd(c, 1); }

ft_putchar_fd

#include "libft.h" void ft_putchar_fd(char c, int fd) { write(fd, &c, 1); }

ft_putendl

#include "libft.h" void ft_putendl(char const *s) { ft_putendl_fd(s, 1); }

ft_putendl_fd

#include "libft.h" void ft_putendl_fd(char const *s, int fd) { if (!s) return ; write(fd, s, ft_strlen(s)); write(fd, "\n", 1); }

ft_putnbr

#include "libft.h" void ft_putnbr(int n) { ft_putnbr_fd(n, 1); }

ft_putnbr_fd

#include "libft.h" void ft_putnbr_fd(int n, int fd) { if ((n > 2147483647) || (n < -2147483648)) return ; if (n == 0) { ft_putchar_fd('0', fd); return ; } if (n == -2147483648) { ft_putstr_fd("-2147483648", fd); return ; } if (n < 0) { ft_putchar_fd('-', fd); n *= -1; } if (n >= 10) { ft_putnbr_fd(n / 10, fd); ft_putnbr_fd(n % 10, fd); } else ft_putchar_fd(n + '0', fd); }

ft_putstr

#include "libft.h" void ft_putstr(char const *s) { if (!s) return ; ft_putstr_fd(s, 1); }

ft_putstr_fd

#include "libft.h" void ft_putstr_fd(char const *s, int fd) { if (!s || !fd) return ; write(fd, s, ft_strlen(s)); }

ft_revstr

#include "libft.h" void ft_revstr(char *c) { char *z; char a; z = c + ft_strlen(c) - 1; while (c < z) { a = *c; *c++ = *z; *z-- = a; } }

ft_strcat

#include "libft.h" char *ft_strcat(char *s1, const char *s2) { char *s1alt; s1alt = s1; while (*s1alt != '\0') s1alt++; while (*s2 != '\0') { *s1alt = *s2; s1alt++; s2++; } *s1alt = '\0'; return (s1); }

ft_strchr

#include "libft.h" char *ft_strchr(const char *s, int c) { char altc; char *alts; int i; i = 0; alts = (char*)s; altc = c; while (alts[i] != altc) { if (alts[i] == '\0') { return (NULL); } i++; } return ((char*)alts + i); }

ft_strclr

#include "libft.h" void ft_strclr(char *s) { int i; int z; if (!s) return ; i = 0; z = ft_strlen(s); while (s) { s[i] = '\0'; if (i == z) return ; i++; } }

ft_strcmp

#include "libft.h" int ft_strcmp(const char *s1, const char *s2) { size_t i; i = 0; while (s1[i] != '\0' && s1[i] == s2[i]) { s1++; s2++; } return ((unsigned char)s1[i] - (unsigned char)s2[i]); }

ft_strcpy

#include "libft.h" char *ft_strcpy(char *dst, char *src) { int i; i = 0; while (src[i] != '\0') { dst[i] = src[i]; i++; } dst[i] = '\0'; return (dst); }

ft_strdel

#include "libft.h" void ft_strdel(char **as) { if (!as) return ; if (as) { free(*as); *as = NULL; } }

ft_strdup

nclude "libft.h" char *ft_strdup(const char *s1) { size_t i; char *dest; i = 0; while (s1[i]) i++; dest = (char*)malloc(sizeof(char) * (i + 1)); if (!dest) return (NULL); i = 0; while (s1[i] != '\0') { dest[i] = s1[i]; i++; } dest[i] = '\0'; return (dest); }

ft_strequ

#include "libft.h" int ft_strequ(char const *s1, char const *s2) { if (!s1 || !s2) return (0); return (!ft_strcmp(s1, s2)); }

ft_striter

#include "libft.h" void ft_striter(char *s, void (*f)(char *)) { size_t i; i = 0; if (s && f) { while (s[i]) { (*f)(&s[i]); i++; } } }

ft_striteri

#include "libft.h" void ft_striteri(char *s, void (*f)(unsigned int, char*)) { size_t i; i = 0; if (s && f) { while (s[i]) { (*f)(i, &s[i]); i++; } } }

ft_strjoin

#include "libft.h" char *ft_strjoin(char const *s1, char const *s2) { size_t i; size_t z; char *blank; char len; i = 0; z = 0; if (!s1 || !s2) return (NULL); len = ft_strlen(s1) + ft_strlen(s2); if (!(blank = (char*)malloc(sizeof(char) * (len + 1)))) return (NULL); while (s1[i] != '\0') { blank[i] = s1[i]; i++; } while (s2[z] != '\0') blank[i++] = s2[z++]; blank[i] = '\0'; return (blank); }

ft_strlcat

#include "libft.h" size_t ft_strlcat(char *dst, const char *src, size_t size) { size_t altdst; size_t altsrc; size_t i; if (!size) return (ft_strlen(dst) + size); altsrc = ft_strlen(src); i = 0; while (dst[i] && i < size) i++; altdst = i; while (src[i - altdst] && i < size - 1) { dst[i] = src[i - altdst]; i++; } if (altdst < size) dst[i] = '\0'; return (altdst + altsrc); }

ft_strlen

#include "libft.h" size_t ft_strlen(const char *s) { size_t i; i = 0; while (s[i] != '\0') i++; return (i); }

ft_strmap

#include "libft.h" char *ft_strmap(char const *s, char (*f)(char)) { char *blank; size_t i; if (!s || !f) return (NULL); if (!(blank = ft_strnew(ft_strlen(s)))) return (NULL); i = 0; while (s[i]) { blank[i] = (*f)(s[i]); i++; } blank[i] = '\0'; return (blank); }

ft_strmapi

#include "libft.h" char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) { unsigned int i; char *blank; if (!s || !f) return (NULL); if (!(blank = ft_strnew(ft_strlen(s)))) return (NULL); i = 0; if (s && f) { while (s[i] != '\0') { blank[i] = (*f)(i, s[i]); i++; } } blank[i] = '\0'; return (blank); }

ft_strncat

#include "libft.h" char *ft_strncat(char *s1, const char *s2, size_t n) { char *s1alt; size_t i; s1alt = s1; i = 0; while (*s1alt != '\0') s1alt++; while (i != n && *s2 != '\0') { *s1alt = *s2; s1alt++; s2++; i++; } *s1alt = '\0'; return (s1); }

ft_strncmp

#include "libft.h" int ft_strncmp(const char *s1, const char *s2, size_t n) { unsigned int z; z = 0; while ((s1[z] != '\0' || s2[z] != '\0') && n--) { if (s1[z] != s2[z]) return ((unsigned char)s1[z] - (unsigned char)s2[z]); z++; } return (0); }

ft_strncpy

#include "libft.h" char *ft_strncpy(char *dst, const char *src, size_t len) { size_t a; size_t b; a = 0; b = len; while (a != len && src[a] != '\0') { dst[a] = src[a]; a++; b--; } while (b != 0) { dst[a] = '\0'; a++; b--; } return (dst); }

ft_strndup

#include "libft.h" char *ft_strndup(const char *s, size_t n) { char *blank; if (!(blank = ft_strnew(n))) return (NULL); ft_strncpy(blank, s, n); return (blank); }

ft_strnequ

#include "libft.h" int ft_strnequ(char const *s1, char const *s2, size_t n) { if (!s1 || !s2) return (0); return (!ft_strncmp(s1, s2, n)); }

ft_strnew

#include "libft.h" char *ft_strnew(size_t size) { char *temp; size_t i; i = 0; if (!(temp = malloc(sizeof(*temp) * (size + 1)))) return (NULL); if (temp) { while (i <= size) { temp[i] = '\0'; i++; } } return (temp); }

ft_strnlen

#include "libft.h" size_t ft_strnlen(const char *s, size_t maxlen) { size_t i; i = 0; while (s[i] != '\0' && i != maxlen) i++; return (i); }

ft_strnstr

#include "libft.h" char *ft_strnstr(const char *big, const char *little, size_t len) { size_t i; int length; char *large; char *small; if (*little == '\0') return ((char *)big); large = (char *)big; small = (char *)little; i = 0; length = ft_strlen(small); while (large[i] != '\0' && (i + length) <= len) { if (ft_strncmp((large + i), small, length) == 0) { return (large + i); } i++; } return (NULL); }

ft_strrchr

#include "libft.h" char *ft_strrchr(const char *s, int c) { size_t i; char *alts; alts = (char*)s; i = ft_strlen(s); if (c == '\0') return (alts + i); while (i != 0) { if (alts[i] == (char)c) return (alts + i); i--; } if (s[0] == (char)c) return (alts); return (0); }

ft_strsplit

#include "libft.h" static int wordcount(char const *str, char c) { int i; int timer; i = 0; timer = 0; while (str[i]) { while (str[i] == c) i++; if (str[i] != c && str[i] != '\0') timer++; while (str[i] != c && str[i] != '\0') i++; } return (timer); } static int wordlen(char const *str, char c) { int i; int timer2; i = 0; timer2 = 0; while (str[i] == c) i++; while (str[i] != c && str[i] != '\0') { timer2++; i++; } return (timer2); } char **ft_strsplit(char const *s, char c) { char **strmagic; int i; int i2; int i3; if (!s || !(strmagic = (char **)malloc(sizeof(*strmagic) * (wordcount(s, c) + 1)))) return (0); i = -1; i2 = 0; while (++i < wordcount(s, c)) { i3 = 0; if (!(strmagic[i] = ft_strnew(wordlen(&s[i2], c) + 1))) strmagic[i] = NULL; while (s[i2] == c) i2++; while (s[i2] != c && s[i2]) strmagic[i][i3++] = s[i2++]; strmagic[i][i3] = '\0'; } strmagic[i] = 0; return (strmagic); }

ft_strstr

#include "libft.h" char *ft_strstr(const char *big, const char *little) { size_t i; size_t z; i = 0; if (*little == '\0') return ((char *)big); while (big[i] != '\0') { z = 0; while (little[z] == big[i + z]) { if (little[z + 1] == '\0') { return ((char *)big + i); } z++; } i++; } return (0); }

ft_strsub

#include "libft.h" char *ft_strsub(char const *s, unsigned int start, size_t len) { unsigned int i; char *blank; i = 0; if (!s) return (NULL); blank = ft_strnew(len); if (!blank) return (NULL); if (s) { while (i != len) { blank[i] = s[start + i]; i++; } } blank[len] = '\0'; return (blank); }

ft_strtrim

#include "libft.h" static int whatspace(int c) { if ((c == ' ' || c == '\n' || c == '\t')) return (1); return (0); } static char strspace(char const *s) { int i; int z; i = 0; z = 0; while (s[i] != '\0') { if ((s[i] == ' ' || s[i] == '\n' || s[i] == '\t')) z++; i++; } if (i == z) return (1); return (0); } char *ft_strtrim(char const *s) { size_t front; size_t back; size_t z; char *middle; if (!s || !ft_strlen(s)) return ((char*)s); if (strspace(s) == 1) return (ft_strnew(1)); front = 0; back = ft_strlen(s) - 1; z = 0; while (whatspace(s[front])) front++; while (whatspace(s[back]) != 0) back--; z = back - front + 1; if (!(middle = ft_strnew(z + 1))) return (NULL); return (ft_strncpy(middle, &s[front], z)); }

ft_tolower

#include "libft.h" int ft_tolower(int c) { if (c >= 'A' && c <= 'Z') return (c + 'a' - 'A'); return (c); }

ft_toupper

#include "libft.h" int ft_toupper(int c) { if (c >= 'a' && c <= 'z') return (c - 'a' + 'A'); return (c); }

libft.h

#ifndef LIBFT_H # define LIBFT_H # include <string.h> # include <stdlib.h> # include <unistd.h> typedeGet Next Line struct s_list { void *content; size_t content_size; struct s_list *next; } t_list; size_t ft_strlen(const char *s); void ft_bzero(void *s, size_t n); char *ft_strcpy(char *dst, char *src); char *ft_strncpy(char *dst, const char *src, size_t len); int ft_strcmp(const char *s1, const char *s2); int ft_strncmp(const char *s1, const char *s2, size_t n); char *ft_strdup(const char *s1); int ft_atoi(const char *str); int ft_isdigit(int c); int ft_isascii(int c); int ft_isprint(int c); int ft_isalpha(int c); int ft_isalnum(int c); int ft_toupper(int c); int ft_tolower(int c); int ft_isspace(int c); char *ft_strstr(const char *big, const char *little); char *ft_strnstr(const char *big, const char *little, size_t len); char *ft_strcat(char *s1, const char *s2); char *ft_strncat(char *s1, const char *s2, size_t n); size_t ft_strlcat(char *dest, const char *src, size_t size); void *ft_memset(void *b, int c, size_t len); void *ft_memcpy(void *dst, const void *src, size_t n); void *ft_memccpy(void *dst, const void *src, int c, size_t n); void *ft_revmemcpy(void *dst, const void *src, size_t n); int ft_memcmp(const void *s1, const void *s2, size_t n); void *ft_memmove(void *dst, const void *src, size_t len); void *ft_memchr(const void *s, int c, size_t n); char *ft_strchr(const char *s, int c); char *ft_strrchr(const char *s, int c); void ft_putchar(char c); void ft_putchar_fd(char c, int fd); void ft_putstr(char const *s); void ft_putstr_fd(char const *s, int fd); void ft_putnbr(int n); void ft_putnbr_fd(int n, int fd); void ft_putendl(char const *s); void ft_putendl_fd(char const *s, int fd); void ft_strclr(char *s); void ft_strdel(char **as); void ft_memdel(void **ap); char *ft_strnew(size_t size); void *ft_memalloc(size_t size); char *ft_strtrim(char const *s); int ft_strequ(char const *s1, char const *s2); int ft_strnequ(char const *s1, char const *s2, size_t n); void ft_striter(char *s, void(*f)(char*)); void ft_striteri(char *s, void (*f)(unsigned int, char*)); char *ft_strmap(char const *s, char (*f)(char)); char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); char *ft_strsub(char const *s, unsigned int start, size_t len); char *ft_strjoin(char const *s1, char const *s2); int ft_islower(int c); int ft_isupper(int c); char *ft_strndup(const char *s, size_t n); size_t ft_strnlen(const char *s, size_t maxlen); void ft_revstr(char *c); char *ft_itoa(int c); t_list *ft_lstnew(void const *content, size_t content_size); void ft_lstdelone(t_list **alst, void (*del)(void*, size_t)); void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); void ft_lstadd(t_list **alst, t_list *new); void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); char **ft_strsplit(char const *s, char c); char *ft_strxjoin(char *s1, char *s2, int l); #endif

Makefile

NAME = libft.a SRC = ft_bzero.c ft_strlen.c ft_strcpy.c ft_strncpy.c ft_strcmp.c\ ft_strncmp.c ft_strdup.c ft_atoi.c ft_isdigit.c ft_isascii.c\ ft_isprint.c ft_isalpha.c ft_isalnum.c ft_toupper.c ft_tolower.c\ ft_isspace.c ft_strstr.c ft_strnstr.c ft_strcat.c ft_strncat.c\ ft_strlcat.c ft_memset.c ft_memcpy.c ft_memccpy.c ft_memcmp.c\ ft_memmove.c ft_memchr.c ft_strchr.c ft_strrchr.c ft_putchar.c\ ft_putchar_fd.c ft_putstr.c ft_putstr_fd.c ft_putnbr.c ft_putnbr_fd.c\ ft_putendl.c ft_putendl_fd.c ft_strclr.c ft_memdel.c ft_strdel.c\ ft_strnew.c ft_memalloc.c ft_strtrim.c ft_strequ.c ft_strnequ.c\ ft_striter.c ft_striteri.c ft_strmap.c ft_strmapi.c ft_strsub.c\ ft_strjoin.c ft_islower.c ft_isupper.c ft_strndup.c ft_strnlen.c\ ft_revstr.c ft_itoa.c ft_lstnew.c ft_lstdelone.c ft_lstdel.c ft_lstadd.c\ ft_lstiter.c ft_lstmap.c ft_strsplit.c ft_strxjoin.c\ OBJS = ft_bzero.o ft_strlen.o ft_strcpy.o ft_strncpy.o ft_strcmp.o\ ft_strncmp.o ft_strdup.o ft_atoi.o ft_isdigit.o ft_isascii.o\ ft_isprint.o ft_isalpha.o ft_isalnum.o ft_toupper.o ft_tolower.o\ ft_isspace.o ft_strstr.o ft_strnstr.o ft_strcat.o ft_strncat.o\ ft_strlcat.o ft_memset.o ft_memcpy.o ft_memccpy.o ft_memcmp.o\ ft_memmove.o ft_memchr.o ft_strchr.o ft_strrchr.o ft_putchar.o\ ft_putchar_fd.o ft_putstr.o ft_putstr_fd.o ft_putnbr.o ft_putnbr_fd.o\ ft_putendl.o ft_putendl_fd.o ft_strclr.o ft_memdel.o ft_strdel.o\ ft_strnew.o ft_memalloc.o ft_strtrim.o ft_strequ.o ft_strnequ.o\ ft_striter.o ft_striteri.o ft_strmap.o ft_strmapi.o ft_strsub.o\ ft_strjoin.o ft_islower.o ft_isupper.o ft_strndup.o ft_strnlen.o\ ft_revstr.o ft_itoa.o ft_lstnew.o ft_lstdelone.o ft_lstdel.o ft_lstadd.o\ ft_lstiter.o ft_lstmap.o ft_strsplit.o ft_strxjoin.o\ FLAGS = -Wall -Wextra -Werror CC = gcc HEADER = libft.h all: $(NAME) $(NAME): $(CC) $(FLAGS) -c $(SRC) ar rc $(NAME) $(OBJS) ranlib $(NAME) clean: rm -rf $(OBJS) fclean: clean rm -f $(NAME) re: fclean all