diff options
| author | millert <> | 2015-01-15 03:54:12 +0000 |
|---|---|---|
| committer | millert <> | 2015-01-15 03:54:12 +0000 |
| commit | c97f4154e07aea44fd7f923b797a79f8dbd864dd (patch) | |
| tree | d0b902c55fd7f3d376e54e326c49e699105a8479 /src/lib/libc | |
| parent | 9d1112e0335b8725290afa68e01914dbc2df0606 (diff) | |
| download | openbsd-c97f4154e07aea44fd7f923b797a79f8dbd864dd.tar.gz openbsd-c97f4154e07aea44fd7f923b797a79f8dbd864dd.tar.bz2 openbsd-c97f4154e07aea44fd7f923b797a79f8dbd864dd.zip | |
Make strlcpy/strlcat slightly easier to read.
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/string/strlcat.c | 46 | ||||
| -rw-r--r-- | src/lib/libc/string/strlcpy.c | 37 | ||||
| -rw-r--r-- | src/lib/libc/string/wcslcat.c | 47 | ||||
| -rw-r--r-- | src/lib/libc/string/wcslcpy.c | 38 |
4 files changed, 82 insertions, 86 deletions
diff --git a/src/lib/libc/string/strlcat.c b/src/lib/libc/string/strlcat.c index ceab094411..2c7404a535 100644 --- a/src/lib/libc/string/strlcat.c +++ b/src/lib/libc/string/strlcat.c | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ | 1 | /* $OpenBSD: strlcat.c,v 1.14 2015/01/15 03:54:12 millert Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | 4 | * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> |
| 5 | * | 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any | 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above | 7 | * purpose with or without fee is hereby granted, provided that the above |
| @@ -20,36 +20,36 @@ | |||
| 20 | #include <string.h> | 20 | #include <string.h> |
| 21 | 21 | ||
| 22 | /* | 22 | /* |
| 23 | * Appends src to string dst of size siz (unlike strncat, siz is the | 23 | * Appends src to string dst of size dsize (unlike strncat, dsize is the |
| 24 | * full size of dst, not space left). At most siz-1 characters | 24 | * full size of dst, not space left). At most dsize-1 characters |
| 25 | * will be copied. Always NUL terminates (unless siz <= strlen(dst)). | 25 | * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). |
| 26 | * Returns strlen(src) + MIN(siz, strlen(initial dst)). | 26 | * Returns strlen(src) + MIN(dsize, strlen(initial dst)). |
| 27 | * If retval >= siz, truncation occurred. | 27 | * If retval >= siz, truncation occurred. |
| 28 | */ | 28 | */ |
| 29 | size_t | 29 | size_t |
| 30 | strlcat(char *dst, const char *src, size_t siz) | 30 | strlcat(char *dst, const char *src, size_t dsize) |
| 31 | { | 31 | { |
| 32 | char *d = dst; | 32 | const char *odst = dst; |
| 33 | const char *s = src; | 33 | const char *osrc = src; |
| 34 | size_t n = siz; | 34 | size_t n = dsize; |
| 35 | size_t dlen; | 35 | size_t dlen; |
| 36 | 36 | ||
| 37 | /* Find the end of dst and adjust bytes left but don't go past end */ | 37 | /* Find the end of dst and adjust bytes left but don't go past end. */ |
| 38 | while (n-- != 0 && *d != '\0') | 38 | while (n-- != 0 && *dst != '\0') |
| 39 | d++; | 39 | dst++; |
| 40 | dlen = d - dst; | 40 | dlen = dst - odst; |
| 41 | n = siz - dlen; | 41 | n = dsize - dlen; |
| 42 | 42 | ||
| 43 | if (n == 0) | 43 | if (n-- == 0) |
| 44 | return(dlen + strlen(s)); | 44 | return(dlen + strlen(src)); |
| 45 | while (*s != '\0') { | 45 | while (*src != '\0') { |
| 46 | if (n != 1) { | 46 | if (n != 0) { |
| 47 | *d++ = *s; | 47 | *dst++ = *src; |
| 48 | n--; | 48 | n--; |
| 49 | } | 49 | } |
| 50 | s++; | 50 | src++; |
| 51 | } | 51 | } |
| 52 | *d = '\0'; | 52 | *dst = '\0'; |
| 53 | 53 | ||
| 54 | return(dlen + (s - src)); /* count does not include NUL */ | 54 | return(dlen + (src - osrc)); /* count does not include NUL */ |
| 55 | } | 55 | } |
diff --git a/src/lib/libc/string/strlcpy.c b/src/lib/libc/string/strlcpy.c index d32b6590f1..e9a7fe4be7 100644 --- a/src/lib/libc/string/strlcpy.c +++ b/src/lib/libc/string/strlcpy.c | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ | 1 | /* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | 4 | * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> |
| 5 | * | 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any | 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above | 7 | * purpose with or without fee is hereby granted, provided that the above |
| @@ -20,32 +20,31 @@ | |||
| 20 | #include <string.h> | 20 | #include <string.h> |
| 21 | 21 | ||
| 22 | /* | 22 | /* |
| 23 | * Copy src to string dst of size siz. At most siz-1 characters | 23 | * Copy string src to buffer dst of size dsize. At most dsize-1 |
| 24 | * will be copied. Always NUL terminates (unless siz == 0). | 24 | * chars will be copied. Always NUL terminates (unless dsize == 0). |
| 25 | * Returns strlen(src); if retval >= siz, truncation occurred. | 25 | * Returns strlen(src); if retval >= dsize, truncation occurred. |
| 26 | */ | 26 | */ |
| 27 | size_t | 27 | size_t |
| 28 | strlcpy(char *dst, const char *src, size_t siz) | 28 | strlcpy(char *dst, const char *src, size_t dsize) |
| 29 | { | 29 | { |
| 30 | char *d = dst; | 30 | const char *osrc = src; |
| 31 | const char *s = src; | 31 | size_t nleft = dsize; |
| 32 | size_t n = siz; | ||
| 33 | 32 | ||
| 34 | /* Copy as many bytes as will fit */ | 33 | /* Copy as many bytes as will fit. */ |
| 35 | if (n != 0) { | 34 | if (nleft != 0) { |
| 36 | while (--n != 0) { | 35 | while (--nleft != 0) { |
| 37 | if ((*d++ = *s++) == '\0') | 36 | if ((*dst++ = *src++) == '\0') |
| 38 | break; | 37 | break; |
| 39 | } | 38 | } |
| 40 | } | 39 | } |
| 41 | 40 | ||
| 42 | /* Not enough room in dst, add NUL and traverse rest of src */ | 41 | /* Not enough room in dst, add NUL and traverse rest of src. */ |
| 43 | if (n == 0) { | 42 | if (nleft == 0) { |
| 44 | if (siz != 0) | 43 | if (dsize != 0) |
| 45 | *d = '\0'; /* NUL-terminate dst */ | 44 | *dst = '\0'; /* NUL-terminate dst */ |
| 46 | while (*s++) | 45 | while (*src++) |
| 47 | ; | 46 | ; |
| 48 | } | 47 | } |
| 49 | 48 | ||
| 50 | return(s - src - 1); /* count does not include NUL */ | 49 | return(src - osrc - 1); /* count does not include NUL */ |
| 51 | } | 50 | } |
diff --git a/src/lib/libc/string/wcslcat.c b/src/lib/libc/string/wcslcat.c index ee8ff3c343..2dfe2aae31 100644 --- a/src/lib/libc/string/wcslcat.c +++ b/src/lib/libc/string/wcslcat.c | |||
| @@ -1,8 +1,7 @@ | |||
| 1 | /* $OpenBSD: wcslcat.c,v 1.4 2011/07/24 15:21:28 millert Exp $ */ | 1 | /* $OpenBSD: wcslcat.c,v 1.5 2015/01/15 03:54:12 millert Exp $ */ |
| 2 | /* $NetBSD: wcslcat.c,v 1.2 2001/01/03 14:33:02 lukem Exp $ */ | ||
| 3 | 2 | ||
| 4 | /* | 3 | /* |
| 5 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | 4 | * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> |
| 6 | * | 5 | * |
| 7 | * Permission to use, copy, modify, and distribute this software for any | 6 | * Permission to use, copy, modify, and distribute this software for any |
| 8 | * purpose with or without fee is hereby granted, provided that the above | 7 | * purpose with or without fee is hereby granted, provided that the above |
| @@ -21,36 +20,36 @@ | |||
| 21 | #include <wchar.h> | 20 | #include <wchar.h> |
| 22 | 21 | ||
| 23 | /* | 22 | /* |
| 24 | * Appends src to string dst of size siz (unlike wcsncat, siz is the | 23 | * Appends src to string dst of size dsize (unlike strncat, dsize is the |
| 25 | * full size of dst, not space left). At most siz-1 characters | 24 | * full size of dst, not space left). At most dsize-1 characters |
| 26 | * will be copied. Always NUL terminates (unless siz <= wcslen(dst)). | 25 | * will be copied. Always NUL terminates (unless dsize <= wcslen(dst)). |
| 27 | * Returns wcslen(src) + MIN(siz, wcslen(initial dst)). | 26 | * Returns wcslen(src) + MIN(dsize, wcslen(initial dst)). |
| 28 | * If retval >= siz, truncation occurred. | 27 | * If retval >= siz, truncation occurred. |
| 29 | */ | 28 | */ |
| 30 | size_t | 29 | size_t |
| 31 | wcslcat(wchar_t *dst, const wchar_t *src, size_t siz) | 30 | wcslcat(wchar_t *dst, const wchar_t *src, size_t dsize) |
| 32 | { | 31 | { |
| 33 | wchar_t *d = dst; | 32 | const wchar_t *odst = dst; |
| 34 | const wchar_t *s = src; | 33 | const wchar_t *osrc = src; |
| 35 | size_t n = siz; | 34 | size_t n = dsize; |
| 36 | size_t dlen; | 35 | size_t dlen; |
| 37 | 36 | ||
| 38 | /* Find the end of dst and adjust bytes left but don't go past end */ | 37 | /* Find the end of dst and adjust bytes left but don't go past end. */ |
| 39 | while (n-- != 0 && *d != '\0') | 38 | while (n-- != 0 && *dst != L'\0') |
| 40 | d++; | 39 | dst++; |
| 41 | dlen = d - dst; | 40 | dlen = dst - odst; |
| 42 | n = siz - dlen; | 41 | n = dsize - dlen; |
| 43 | 42 | ||
| 44 | if (n == 0) | 43 | if (n-- == 0) |
| 45 | return(dlen + wcslen(s)); | 44 | return(dlen + wcslen(src)); |
| 46 | while (*s != '\0') { | 45 | while (*src != L'\0') { |
| 47 | if (n != 1) { | 46 | if (n != 0) { |
| 48 | *d++ = *s; | 47 | *dst++ = *src; |
| 49 | n--; | 48 | n--; |
| 50 | } | 49 | } |
| 51 | s++; | 50 | src++; |
| 52 | } | 51 | } |
| 53 | *d = '\0'; | 52 | *dst = L'\0'; |
| 54 | 53 | ||
| 55 | return(dlen + (s - src)); /* count does not include NUL */ | 54 | return(dlen + (src - osrc)); /* count does not include NUL */ |
| 56 | } | 55 | } |
diff --git a/src/lib/libc/string/wcslcpy.c b/src/lib/libc/string/wcslcpy.c index f49936a885..1c4811577f 100644 --- a/src/lib/libc/string/wcslcpy.c +++ b/src/lib/libc/string/wcslcpy.c | |||
| @@ -1,8 +1,7 @@ | |||
| 1 | /* $OpenBSD: wcslcpy.c,v 1.5 2011/07/24 15:21:28 millert Exp $ */ | 1 | /* $OpenBSD: wcslcpy.c,v 1.6 2015/01/15 03:54:12 millert Exp $ */ |
| 2 | /* $NetBSD: wcslcpy.c,v 1.2 2001/01/03 14:33:02 lukem Exp $ */ | ||
| 3 | 2 | ||
| 4 | /* | 3 | /* |
| 5 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | 4 | * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> |
| 6 | * | 5 | * |
| 7 | * Permission to use, copy, modify, and distribute this software for any | 6 | * Permission to use, copy, modify, and distribute this software for any |
| 8 | * purpose with or without fee is hereby granted, provided that the above | 7 | * purpose with or without fee is hereby granted, provided that the above |
| @@ -21,32 +20,31 @@ | |||
| 21 | #include <wchar.h> | 20 | #include <wchar.h> |
| 22 | 21 | ||
| 23 | /* | 22 | /* |
| 24 | * Copy src to string dst of size siz. At most siz-1 characters | 23 | * Copy string src to buffer dst of size dsize. At most dsize-1 |
| 25 | * will be copied. Always NUL terminates (unless siz == 0). | 24 | * chars will be copied. Always NUL terminates (unless dsize == 0). |
| 26 | * Returns wcslen(src); if retval >= siz, truncation occurred. | 25 | * Returns wcslen(src); if retval >= dsize, truncation occurred. |
| 27 | */ | 26 | */ |
| 28 | size_t | 27 | size_t |
| 29 | wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz) | 28 | wcslcpy(wchar_t *dst, const wchar_t *src, size_t dsize) |
| 30 | { | 29 | { |
| 31 | wchar_t *d = dst; | 30 | const wchar_t *osrc = src; |
| 32 | const wchar_t *s = src; | 31 | size_t nleft = dsize; |
| 33 | size_t n = siz; | ||
| 34 | 32 | ||
| 35 | /* Copy as many bytes as will fit */ | 33 | /* Copy as many bytes as will fit. */ |
| 36 | if (n != 0) { | 34 | if (nleft != 0) { |
| 37 | while (--n != 0) { | 35 | while (--nleft != 0) { |
| 38 | if ((*d++ = *s++) == '\0') | 36 | if ((*dst++ = *src++) == L'\0') |
| 39 | break; | 37 | break; |
| 40 | } | 38 | } |
| 41 | } | 39 | } |
| 42 | 40 | ||
| 43 | /* Not enough room in dst, add NUL and traverse rest of src */ | 41 | /* Not enough room in dst, add NUL and traverse rest of src. */ |
| 44 | if (n == 0) { | 42 | if (nleft == 0) { |
| 45 | if (siz != 0) | 43 | if (dsize != 0) |
| 46 | *d = '\0'; /* NUL-terminate dst */ | 44 | *dst = L'\0'; /* NUL-terminate dst */ |
| 47 | while (*s++) | 45 | while (*src++) |
| 48 | ; | 46 | ; |
| 49 | } | 47 | } |
| 50 | 48 | ||
| 51 | return(s - src - 1); /* count does not include NUL */ | 49 | return(src - osrc - 1); /* count does not include NUL */ |
| 52 | } | 50 | } |
