diff options
author | millert <> | 2015-01-15 03:54:12 +0000 |
---|---|---|
committer | millert <> | 2015-01-15 03:54:12 +0000 |
commit | 437c1f4105f2fba7c980036ce55fa30516d2e500 (patch) | |
tree | d0b902c55fd7f3d376e54e326c49e699105a8479 /src/lib/libc/string/strlcpy.c | |
parent | 3fd2d1d95424716eaf5c4ecf17d6bf0be444a372 (diff) | |
download | openbsd-437c1f4105f2fba7c980036ce55fa30516d2e500.tar.gz openbsd-437c1f4105f2fba7c980036ce55fa30516d2e500.tar.bz2 openbsd-437c1f4105f2fba7c980036ce55fa30516d2e500.zip |
Make strlcpy/strlcat slightly easier to read.
Diffstat (limited to 'src/lib/libc/string/strlcpy.c')
-rw-r--r-- | src/lib/libc/string/strlcpy.c | 37 |
1 files changed, 18 insertions, 19 deletions
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 | } |