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