Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2024 | ||
3 | ** my_strndup | ||
4 | ** File description: | ||
5 | ** Returns a duplication of a string (src) with a defined size (n) | ||
6 | */ | ||
7 | /** | ||
8 | * @file my_strndup.c | ||
9 | * @brief The file containing the my_strndup function | ||
10 | * @author Nicolas TORO | ||
11 | */ | ||
12 | |||
13 | #include "my.h" | ||
14 | |||
15 | 2 | char *my_strndup(char const *src, int n) | |
16 | { | ||
17 | 2 | char *dest = malloc(sizeof(char) * (n + 1)); | |
18 | 2 | int i = 0; | |
19 | |||
20 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (src == NULL) |
21 | 1 | return NULL; | |
22 |
3/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
|
4 | while (src[i] != '\0' && i < n) { |
23 | 3 | dest[i] = src[i]; | |
24 | 3 | i++; | |
25 | } | ||
26 | 1 | dest[i] = '\0'; | |
27 | 1 | return dest; | |
28 | } | ||
29 |