Rev 197 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 195 | f9daq | 1 | /********************************************************************\ |
| 2 | |||
| 3 | Name: strlcpy.c |
||
| 4 | Created by: Stefan Ritt |
||
| 5 | Copyright 2000 + Stefan Ritt |
||
| 6 | |||
| 7 | Contents: Contains strlcpy and strlcat which are versions of |
||
| 8 | strcpy and strcat, but which avoid buffer overflows |
||
| 9 | |||
| 10 | |||
| 11 | This file is part of MIDAS XML Library. |
||
| 12 | |||
| 13 | MIDAS XML Library is free software: you can redistribute it and/or modify |
||
| 14 | it under the terms of the GNU General Public License as published by |
||
| 15 | the Free Software Foundation, either version 3 of the License, or |
||
| 16 | (at your option) any later version. |
||
| 17 | |||
| 18 | MIDAS XML Library is distributed in the hope that it will be useful, |
||
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 21 | GNU General Public License for more details. |
||
| 22 | |||
| 23 | You should have received a copy of the GNU General Public License |
||
| 24 | along with MIDAS XML Library. If not, see <http://www.gnu.org/licenses/>. |
||
| 25 | |||
| 26 | \********************************************************************/ |
||
| 27 | |||
| 28 | #include <stdio.h> |
||
| 29 | #include <string.h> |
||
| 30 | #include "strlcpy.h" |
||
| 31 | |||
| 32 | /* |
||
| 33 | * Copy src to string dst of size siz. At most siz-1 characters |
||
| 34 | * will be copied. Always NUL terminates (unless size == 0). |
||
| 35 | * Returns strlen(src); if retval >= siz, truncation occurred. |
||
| 36 | */ |
||
| 37 | #ifndef STRLCPY_DEFINED |
||
| 38 | |||
| 39 | size_t strlcpy(char *dst, const char *src, size_t size) |
||
| 40 | { |
||
| 41 | char *d = dst; |
||
| 42 | const char *s = src; |
||
| 43 | size_t n = size; |
||
| 44 | |||
| 45 | /* Copy as many bytes as will fit */ |
||
| 46 | if (n != 0 && --n != 0) { |
||
| 47 | do { |
||
| 48 | if ((*d++ = *s++) == 0) |
||
| 49 | break; |
||
| 50 | } while (--n != 0); |
||
| 51 | } |
||
| 52 | |||
| 53 | /* Not enough room in dst, add NUL and traverse rest of src */ |
||
| 54 | if (n == 0) { |
||
| 55 | if (size != 0) |
||
| 56 | *d = '\0'; /* NUL-terminate dst */ |
||
| 57 | while (*s++); |
||
| 58 | } |
||
| 59 | |||
| 60 | return (s - src - 1); /* count does not include NUL */ |
||
| 61 | } |
||
| 62 | |||
| 63 | /*-------------------------------------------------------------------*/ |
||
| 64 | |||
| 65 | /* |
||
| 66 | * Appends src to string dst of size siz (unlike strncat, siz is the |
||
| 67 | * full size of dst, not space left). At most siz-1 characters |
||
| 68 | * will be copied. Always NUL terminates (unless size <= strlen(dst)). |
||
| 69 | * Returns strlen(src) + MIN(size, strlen(initial dst)). |
||
| 70 | * If retval >= size, truncation occurred. |
||
| 71 | */ |
||
| 72 | size_t strlcat(char *dst, const char *src, size_t size) |
||
| 73 | { |
||
| 74 | char *d = dst; |
||
| 75 | const char *s = src; |
||
| 76 | size_t n = size; |
||
| 77 | size_t dlen; |
||
| 78 | |||
| 79 | /* Find the end of dst and adjust bytes left but don't go past end */ |
||
| 80 | while (n-- != 0 && *d != '\0') |
||
| 81 | d++; |
||
| 82 | dlen = d - dst; |
||
| 83 | n = size - dlen; |
||
| 84 | |||
| 85 | if (n == 0) |
||
| 86 | return (dlen + strlen(s)); |
||
| 87 | while (*s != '\0') { |
||
| 88 | if (n != 1) { |
||
| 89 | *d++ = *s; |
||
| 90 | n--; |
||
| 91 | } |
||
| 92 | s++; |
||
| 93 | } |
||
| 94 | *d = '\0'; |
||
| 95 | |||
| 96 | return (dlen + (s - src)); /* count does not include NUL */ |
||
| 97 | } |
||
| 98 | |||
| 99 | /*-------------------------------------------------------------------*/ |
||
| 100 | |||
| 101 | #endif // STRLCPY_DEFINED |