diff options
author | Ali Fardan <raiz@stellarbound.info> | 2020-10-22 13:31:28 +0300 |
---|---|---|
committer | Ali Fardan <raiz@stellarbound.info> | 2020-10-22 13:31:28 +0300 |
commit | de22e0ae21c46fc9eb1cc6717832bfb5fa0600e1 (patch) | |
tree | 1db412a2eb4974aeda045947db284dd806f021b1 /strlcat.c | |
parent | a77f42d3c2d19dfa13e0f98935de8f9b59502a6a (diff) | |
download | libgemtext-de22e0ae21c46fc9eb1cc6717832bfb5fa0600e1.tar.gz |
- implement the rest of decode.c functions: gemtext_decode_fd() and gemtext_decode_file()
- introduce gemtext_encode() with helper functions gemtext_encode_fd() and gemtext_encode_file() - simplify handling of unordered lists in decode.c - add test.c rule to makefile
Diffstat (limited to 'strlcat.c')
-rw-r--r-- | strlcat.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/strlcat.c b/strlcat.c new file mode 100644 index 0000000..c94e90d --- /dev/null +++ b/strlcat.c @@ -0,0 +1,55 @@ +/* $OpenBSD: strlcat.c,v 1.19 2019/01/25 00:19:25 millert Exp $ */ + +/* + * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <string.h> + +/* + * Appends src to string dst of size dsize (unlike strncat, dsize is the + * full size of dst, not space left). At most dsize-1 characters + * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). + * Returns strlen(src) + MIN(dsize, strlen(initial dst)). + * If retval >= dsize, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t dsize) +{ + const char *odst = dst; + const char *osrc = src; + size_t n = dsize; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end. */ + while (n-- != 0 && *dst != '\0') + dst++; + dlen = dst - odst; + n = dsize - dlen; + + if (n-- == 0) + return(dlen + strlen(src)); + while (*src != '\0') { + if (n != 0) { + *dst++ = *src; + n--; + } + src++; + } + *dst = '\0'; + + return(dlen + (src - osrc)); /* count does not include NUL */ +} |