Fix ft_strlcpy.c

This commit is contained in:
Timo Schmidt 2023-03-24 00:47:26 +01:00
parent 1fb37fd19e
commit 7672237bc1
1 changed files with 21 additions and 15 deletions

View File

@ -6,35 +6,41 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */ /* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/19 11:49:46 by tischmid #+# #+# */ /* Created: 2023/03/19 11:49:46 by tischmid #+# #+# */
/* Updated: 2023/03/21 09:44:10 by tischmid ### ########.fr */ /* Updated: 2023/03/24 00:46:53 by tischmid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#define BUFSIZE 10
unsigned int ft_strlcpy(char *dest, char *src, unsigned int size) unsigned int ft_strlcpy(char *dest, char *src, unsigned int size)
{ {
unsigned int i; char *orig_src;
i = -1; orig_src = src;
while (*src && ++i < size - 1) while (size-- > 1 && *src)
*dest++ = *src++; *dest++ = *src++;
*dest = 0; *dest = 0;
return (i); size = 0;
while (*orig_src++)
++size;
return (size);
} }
/* //// #define START
#include <stdio.h> #include <stdio.h>
int main(void) int main(void)
{ {
char buf2[10]; char buf[BUFSIZE] = "||||||||||";
char buf[2]; unsigned int return_size;
char str[] = {'A','B','C','D','E','\0'};
ft_strlcpy(buf, str, 6); for (int j = 0; j < 11; ++j)
printf("%p\n", buf); {
printf("%s\n", buf); printf("####### Size == %d #######\n", j);
printf("%p\n", buf2); return_size = ft_strlcpy(buf, "01234", j);
printf("%s\n", buf2); printf("Return Size: %d\n", return_size);
for (int i = 0; i < BUFSIZE; ++i)
printf((buf[i] >= 32 && buf[i] <= 126) ? "%c\n" : "0x%x\n", buf[i]);
}
return (0); return (0);
} }
*/ ////