/* */
/* ************************************************************************** */
+#include <errno.h>
#include <stdlib.h> // for malloc
#include "libft.h"
return (NULL);
total_bytes = nmemb * size;
if (total_bytes / size != nmemb)
+ {
+ errno = ENOMEM;
return (NULL);
+ }
res = malloc(total_bytes);
+ if (res == NULL)
+ return (NULL);
ft_memset(res, 0, total_bytes);
return (res);
}
res = 1;
if (n == 0)
- return (1);
+ return (2);
if (n < 0)
++res;
while (n != 0)
&& i + 1 < n)
++i;
if (n > 0)
- return (*((unsigned char *)s1 + i) == *((unsigned char *)s2 + i));
+ return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i));
else
return (0);
}
void ft_putnbr_fd(int n, int fd)
{
- ft_putstr_fd(ft_itoa(n), fd);
+ char digit;
+
+ if (n < 0)
+ {
+ write(fd, "-", 1);
+ digit = '0' - (n % 10);
+ if (n / 10 != 0)
+ ft_putnbr_fd(-(n / 10), fd);
+ }
+ else if (n < 10)
+ digit = '0' + n;
+ else
+ {
+ digit = '0' + n % 10;
+ ft_putnbr_fd(n / 10, fd);
+ }
+ write(fd, &digit, 1);
return ;
}
#include <stdlib.h>
#include "libft.h"
-/* If str_p points (double deref.) to character that is equal to c,
- this function advances it to first until it is not equal to c.
- Otherwise it advances the pointer to first character, that equals c.
- Function returns when encountering terminating null. */
-static void advance(const char **str_p, char c)
-{
- if (**str_p == c)
- while (**str_p == c && **str_p)
- ++(*str_p);
- else
- while (**str_p != c && **str_p)
- ++(*str_p);
- return ;
-}
-
static void free_str_array(char **arr)
{
size_t i;
return ;
}
+// Returns 0 when terminating null is encountered, otherwise returns true.
+static int to_next_word(const char **s, char c)
+{
+ while (**s && **s == c)
+ ++(*s);
+ return (**s);
+}
+
static size_t word_count(const char *s, char c)
{
size_t count;
+ if (c == '\0')
+ return (1);
count = 0;
while (*s)
{
i = 0;
while (*s)
{
- advance(&s, c);
- if (*s == '\0')
+ if (!to_next_word(&s, c))
break ;
size = (strlen_to_char(s, c) + 1);
res[i] = malloc(size * sizeof(char));
return (NULL);
}
ft_strlcpy(res[i++], s, size);
- advance(&s, c);
+ s += size - 1;
}
res[i] = NULL;
return (res);
char *ft_strchr(const char *s, int c)
{
- while (*s != '\0')
+ while (*s)
{
- if (*s == c)
+ if (*s == (char)c)
return ((char *)s);
++s;
}
size_t length;
length = 0;
- while (*dst != '\0' && size > 1)
+ while (*dst && size > length)
{
++dst;
++length;
- --size;
}
- while (*src != '\0' && size > 1)
+ while (*src && size > length + 1)
{
*dst = *src;
++src;
++dst;
++length;
- --size;
}
- while (size > 0)
- {
+ if (size > length)
*dst = '\0';
- ++dst;
- --size;
+ while (*src)
+ {
+ ++length;
+ ++src;
}
return (length);
}
size_t i;
i = 0;
- while (*src != '\0' && i + 1 < size)
+ while (src[i] && i + 1 < size)
{
dst[i] = src[i];
++i;
}
if (size > 0)
dst[i] = '\0';
- return (ft_strlen(src));
+ while (src[i])
+ ++i;
+ return (i);
}
size_t little_len;
size_t i;
+ if (*little == '\0')
+ return ((char *)big);
little_len = ft_strlen(little);
i = 0;
- while (big[i] != '\0' && i + little_len <= len)
+ while (big[i] && i + little_len <= len)
{
if (!(ft_strncmp(big + i, little, little_len)))
return ((char *)big + i);
result = NULL;
while (*s != '\0')
{
- if (*s == c)
+ if (*s == (char)c)
result = (char *)s;
++s;
}
return (res);
i = 0;
j = 0;
- while (s1[i] != '\0')
+ while (s1[i])
{
if (ft_strchr(set, s1[i]) == NULL)
res[j++] = s1[i];
++i;
}
+ res[j] = '\0';
return (res);
}
char *res;
size_t size;
- size = ft_strlen(s + start) + 1;
+ size = ft_strlen(s) + 1;
+ if (start > size)
+ {
+ res = malloc(1 * sizeof(char));
+ if (res == NULL)
+ return (NULL);
+ *res = '\0';
+ return (res);
+ }
+ else
+ size -= start;
if (size > len + 1)
size = len + 1;
res = malloc(size * sizeof(char));
if (res == NULL)
- return (res);
+ return (NULL);
ft_strlcpy(res, s + start, size);
return (res);
}