Directory: | src/ |
---|---|
File: | src/string.c |
Date: | 2024-04-25 03:45:42 |
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.name> | ||
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 | /* glibc calls this stpecpy; it's not provided by glibc; however, it does | ||
16 | * provide a possible implementation in string_copying(7), which I copied from. */ | ||
17 | 305444 | char *string_append(char *dst, char *end, const char *src) | |
18 | { | ||
19 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 305444 times.
|
305444 | if (!dst) |
20 | ✗ | return NULL; | |
21 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 305444 times.
|
305444 | if (dst == end) |
22 | ✗ | return end; | |
23 | |||
24 | 305444 | char *p = memccpy(dst, src, '\0', end - dst); | |
25 |
1/2✓ Branch 0 taken 305444 times.
✗ Branch 1 not taken.
|
305444 | if (p) |
26 | 305444 | 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 |