strlcpy add with comment and test main
This commit is contained in:
69
srcs/ft_strlcpy.c
Normal file
69
srcs/ft_strlcpy.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/19 13:03:47 by hulamy #+# #+# */
|
||||
/* Updated: 2019/11/19 14:11:39 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*
|
||||
** copy size - 1 length of src into dest,
|
||||
** terminate it with a '\0'
|
||||
** and return size of src
|
||||
**
|
||||
** TESTS :
|
||||
*/
|
||||
|
||||
/*
|
||||
** #include <stdio.h>
|
||||
** #include <stdlib.h>
|
||||
** #include <string.h>
|
||||
** #include <string.h>
|
||||
**
|
||||
** size_t ft_strlcpy(char *dest, const char *src, size_t size);
|
||||
**
|
||||
** int main(int argc, char **argv)
|
||||
** {
|
||||
** char str[100];
|
||||
** int i;
|
||||
** unsigned int u;
|
||||
** unsigned int v;
|
||||
**
|
||||
** i = atoi(argv[3]);
|
||||
** strcpy(str, argv[2]);
|
||||
** if (argc > 3)
|
||||
** {
|
||||
** u = strlcpy(argv[2], argv[1], i);
|
||||
** printf("strlcpy : %s - %s - %d",argv[1], argv[2], i);
|
||||
** printf(" - return:%d\n",u);
|
||||
** strcpy(argv[2], str);
|
||||
** printf("(re-init : %s - %s - %d)\n",argv[1], argv[2], i);
|
||||
** v = ft_strlcpy(argv[2], argv[1], i);
|
||||
** printf("ft_strlcpy: %s - %s - %d",argv[1], argv[2], i);
|
||||
** printf(" - return:%d\n",v);
|
||||
** }
|
||||
** return (0);
|
||||
** }
|
||||
*/
|
||||
|
||||
size_t ft_strlcpy(char *dest, const char *src, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (src[i] != '\0')
|
||||
{
|
||||
if (i + 1 < size)
|
||||
dest[i] = src[j++];
|
||||
i++;
|
||||
}
|
||||
if (size > 0)
|
||||
dest[j] = '\0';
|
||||
return (i);
|
||||
}
|
||||
Reference in New Issue
Block a user