From 7fd21dc25dc31a3807962a3d1489fbfcb951b717 Mon Sep 17 00:00:00 2001 From: yctct Date: Tue, 17 Feb 2026 09:20:01 +0100 Subject: Add files, first commit --- ft_getline_utils.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 ft_getline_utils.c (limited to 'ft_getline_utils.c') diff --git a/ft_getline_utils.c b/ft_getline_utils.c new file mode 100644 index 0000000..d16dae1 --- /dev/null +++ b/ft_getline_utils.c @@ -0,0 +1,137 @@ +/* +ft_getline Copyright (C) 2026 yctct + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + + +#include "ft_getline.h" + +char *ft_strchr(const char *s, int c) +{ + size_t i; + size_t size; + + size = 0; + while (s[size]) + size++; + i = 0; + while (i <= size) + { + if (s[i] == (char)c) + return ((char *)&s[i]); + i++; + } + return (0); +} +char *ft_strjoin(char *s1, char *s2) +{ + char *ptr; + int len_s1; + int len_s2; + + len_s1 = 0; + while (s1[len_s1]) + len_s1++; + len_s2 = 0; + while (s2[len_s2]) + len_s2++; + ptr = malloc(sizeof(char) * (len_s1 + len_s2 + 1)); + if (!ptr) + { + free(s1); + return (NULL); + } + ft_strlcpy(ptr, s1, (len_s1 + 1)); + ft_strlcat(ptr, s2, (len_s1 + len_s2 + 1)); + free(s1); + return (ptr); +} + +size_t ft_strlcat(char *dest, const char *src, size_t size) +{ + size_t dlen; + size_t slen; + + dlen = 0; + while (dest[dlen]) + dlen++; + slen = 0; + while (src[slen]) + slen++; + if (dlen >= size) + return (size + slen); + if (slen < size - dlen) + ft_memcpy(dest + dlen, src, slen + 1); + else + { + ft_memcpy(dest + dlen, src, size - dlen - 1); + dest[size - 1] = '\0'; + } + return (dlen + slen); +} + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + unsigned char *a; + const char *b; + + if (n == 0) + return (dest); + if (!dest || !src) + return (dest); + a = dest; + b = src; + while (n > 0) + { + *a++ = *b++; + n--; + } + return (dest); +} + +size_t ft_strlcpy(char *dest, const char *src, size_t size) +{ + unsigned int i; + + i = 0; + if (size > 0) + { + while (src[i] && i < (size - 1)) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + } + i = 0; + while (src[i]) + i++; + return (i); +} + +char *ft_strdup(const char *s) +{ + char *ptr; + int len; + + len = 0; + while (s[len]) + len++; + ptr = (char *)malloc(len + 1); + if (!ptr) + return (NULL); + ft_strlcpy(ptr, s, len + 1); + return (ptr); +} -- cgit v1.2.3