GCC Code Coverage Report


Directory: src/
File: src/string.c
Date: 2023-08-28 07:33:56
Exec Total Coverage
Lines: 6 22 27.3%
Branches: 3 16 18.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com>
3 * This file is part of the "cimple" project.
4 * For details, see https://github.com/egor-tensin/cimple.
5 * Distributed under the MIT License.
6 */
7
8 #include "string.h"
9 #include "log.h"
10
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 /* This is not provided by glibc; however, it does provide a possible
16 * implementation in string_copying(7), which I copied from. */
17 326633 char *stpecpy(char *dst, char *end, const char *src)
18 {
19
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 326633 times.
326633 if (!dst)
20 return NULL;
21
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 326633 times.
326633 if (dst == end)
22 return end;
23
24 326633 char *p = memccpy(dst, src, '\0', end - dst);
25
1/2
✓ Branch 0 taken 326633 times.
✗ Branch 1 not taken.
326633 if (p)
26 326633 return p - 1;
27
28 end[-1] = '\0';
29 return end;
30 }
31
32 int string_to_int(const char *src, int *result)
33 {
34 char *endptr = NULL;
35
36 errno = 0;
37 long ret = strtol(src, &endptr, 10);
38
39 if (errno) {
40 log_errno("strtol");
41 return -1;
42 }
43
44 if (endptr == src || *endptr != '\0') {
45 log_err("Invalid number: %s\n", src);
46 return -1;
47 }
48
49 *result = (int)ret;
50 return 0;
51 }
52