|
1 | 1 | // https://gist.github.com/Fonger/98cc95ac39fbe1a7e4d9
|
2 | 2 |
|
3 |
| -#ifndef HAVE_STRLCAT |
4 |
| -/* |
5 |
| - '_cups_strlcat()' - Safely concatenate two strings. |
6 |
| -*/ |
7 |
| - |
8 |
| -size_t /* O - Length of string */ |
9 |
| -strlcat(char* dst, /* O - Destination string */ |
10 |
| - const char* src, /* I - Source string */ |
11 |
| - size_t size) /* I - Size of destination string buffer */ |
| 3 | +#include <cstddef> |
| 4 | +#include <cstdlib> |
| 5 | +#include <cstring> |
| 6 | + |
| 7 | +extern "C" |
12 | 8 | {
|
13 |
| - size_t srclen; /* Length of source string */ |
14 |
| - size_t dstlen; /* Length of destination string */ |
| 9 | +#ifdef STRLCAT_MISSING |
| 10 | + // '_cups_strlcat()' - Safely concatenate two strings. |
15 | 11 |
|
16 |
| - /* |
17 |
| - Figure out how much room is left... |
18 |
| - */ |
| 12 | + size_t /* O - Length of string */ |
| 13 | + strlcat(char* dst, /* O - Destination string */ |
| 14 | + const char* src, /* I - Source string */ |
| 15 | + size_t size) /* I - Size of destination string buffer */ |
| 16 | + { |
| 17 | + size_t srclen; /* Length of source string */ |
| 18 | + size_t dstlen; /* Length of destination string */ |
19 | 19 |
|
20 |
| - dstlen = strlen(dst); |
21 |
| - size -= dstlen + 1; |
| 20 | + // Figure out how much room is left... |
22 | 21 |
|
23 |
| - if (!size) |
24 |
| - { |
25 |
| - return (dstlen); /* No room, return immediately... */ |
26 |
| - } |
| 22 | + dstlen = strlen(dst); |
| 23 | + size -= dstlen + 1; |
27 | 24 |
|
28 |
| - /* |
29 |
| - Figure out how much room is needed... |
30 |
| - */ |
| 25 | + if (!size) |
| 26 | + { |
| 27 | + return (dstlen); /* No room, return immediately... */ |
| 28 | + } |
31 | 29 |
|
32 |
| - srclen = strlen(src); |
| 30 | + // Figure out how much room is needed... |
33 | 31 |
|
34 |
| - /* |
35 |
| - Copy the appropriate amount... |
36 |
| - */ |
| 32 | + srclen = strlen(src); |
37 | 33 |
|
38 |
| - if (srclen > size) |
39 |
| - { |
40 |
| - srclen = size; |
| 34 | + // Copy the appropriate amount... |
| 35 | + |
| 36 | + if (srclen > size) |
| 37 | + { |
| 38 | + srclen = size; |
| 39 | + } |
| 40 | + |
| 41 | + memcpy(dst + dstlen, src, srclen); |
| 42 | + dst[dstlen + srclen] = '\0'; |
| 43 | + |
| 44 | + return (dstlen + srclen); |
41 | 45 | }
|
| 46 | +#endif /* STRLCAT_MISSING */ |
42 | 47 |
|
43 |
| - memcpy(dst + dstlen, src, srclen); |
44 |
| - dst[dstlen + srclen] = '\0'; |
| 48 | +#ifdef STRLCPY_MISSING |
| 49 | + // '_cups_strlcpy()' - Safely copy two strings. |
45 | 50 |
|
46 |
| - return (dstlen + srclen); |
47 |
| -} |
48 |
| -#endif /* !HAVE_STRLCAT */ |
| 51 | + size_t /* O - Length of string */ |
| 52 | + strlcpy(char* dst, /* O - Destination string */ |
| 53 | + const char* src, /* I - Source string */ |
| 54 | + size_t size) /* I - Size of destination string buffer */ |
| 55 | + { |
| 56 | + size_t srclen; /* Length of source string */ |
49 | 57 |
|
50 |
| -#ifndef HAVE_STRLCPY |
51 |
| -/* |
52 |
| - '_cups_strlcpy()' - Safely copy two strings. |
53 |
| -*/ |
| 58 | + // Figure out how much room is needed... |
54 | 59 |
|
55 |
| -size_t /* O - Length of string */ |
56 |
| -strlcpy(char* dst, /* O - Destination string */ |
57 |
| - const char* src, /* I - Source string */ |
58 |
| - size_t size) /* I - Size of destination string buffer */ |
59 |
| -{ |
60 |
| - size_t srclen; /* Length of source string */ |
| 60 | + size--; |
61 | 61 |
|
62 |
| - /* |
63 |
| - Figure out how much room is needed... |
64 |
| - */ |
| 62 | + srclen = strlen(src); |
65 | 63 |
|
66 |
| - size--; |
| 64 | + // Copy the appropriate amount... |
67 | 65 |
|
68 |
| - srclen = strlen(src); |
| 66 | + if (srclen > size) |
| 67 | + { |
| 68 | + srclen = size; |
| 69 | + } |
69 | 70 |
|
70 |
| - /* |
71 |
| - Copy the appropriate amount... |
72 |
| - */ |
| 71 | + memcpy(dst, src, srclen); |
| 72 | + dst[srclen] = '\0'; |
73 | 73 |
|
74 |
| - if (srclen > size) |
75 |
| - { |
76 |
| - srclen = size; |
| 74 | + return (srclen); |
77 | 75 | }
|
| 76 | +#endif /* STRLCPY_MISSING */ |
78 | 77 |
|
79 |
| - memcpy(dst, src, srclen); |
80 |
| - dst[srclen] = '\0'; |
81 |
| - |
82 |
| - return (srclen); |
83 |
| -} |
84 |
| -#endif /* !HAVE_STRLCPY */ |
| 78 | +} // extern "C" |
0 commit comments