/src/dcmtk/oficonv/libsrc/oficonv_strlcpy.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> |
3 | | * |
4 | | * Permission to use, copy, modify, and distribute this software for any |
5 | | * purpose with or without fee is hereby granted, provided that the above |
6 | | * copyright notice and this permission notice appear in all copies. |
7 | | * |
8 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
13 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
14 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | | */ |
16 | | |
17 | | #include "dcmtk/config/osconfig.h" |
18 | | #include "oficonv_strlcpy.h" |
19 | | |
20 | | #ifndef HAVE_STRLCPY |
21 | | |
22 | | #include <stddef.h> |
23 | | |
24 | | /* |
25 | | * Copy src to string dst of size siz. At most siz-1 characters |
26 | | * will be copied. Always NUL terminates (unless siz == 0). |
27 | | * Returns strlen(src); if retval >= siz, truncation occurred. |
28 | | */ |
29 | | size_t strlcpy(char *dst, const char *src, size_t siz) |
30 | 0 | { |
31 | 0 | char *d = dst; |
32 | 0 | const char *s = src; |
33 | 0 | size_t n = siz; |
34 | | |
35 | | /* Copy as many bytes as will fit */ |
36 | 0 | if (n != 0 && --n != 0) |
37 | 0 | { |
38 | 0 | do |
39 | 0 | { |
40 | 0 | if ((*d++ = *s++) == 0) |
41 | 0 | break; |
42 | 0 | } while (--n != 0); |
43 | 0 | } |
44 | | |
45 | | /* Not enough room in dst, add NUL and traverse rest of src */ |
46 | 0 | if (n == 0) |
47 | 0 | { |
48 | 0 | if (siz != 0) |
49 | 0 | *d = '\0'; /* NUL-terminate dst */ |
50 | 0 | while (*s++) /* do nothing */ ; |
51 | 0 | } |
52 | |
|
53 | 0 | return(s - src - 1); /* count does not include NUL */ |
54 | 0 | } |
55 | | |
56 | | #else |
57 | | |
58 | | int oficonv_strlcpy_c_dummy_to_keep_linker_from_moaning = 0; |
59 | | |
60 | | #endif /* HAVE_STRLCPY */ |