Line data Source code
1 : /********************************************************************\
2 :
3 : Name: mstrlcpy.c
4 : Created by: Stefan Ritt
5 : Copyright 2000 + Stefan Ritt
6 :
7 : Contents: Contains mstrlcpy and mstrlcat 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 "mstrlcpy.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 378 : size_t mstrlcpy(char *dst, const char *src, size_t size)
38 : {
39 378 : char *d = dst;
40 378 : const char *s = src;
41 378 : size_t n = size;
42 :
43 : /* Copy as many bytes as will fit */
44 378 : if (n != 0 && --n != 0) {
45 : do {
46 5108 : if ((*d++ = *s++) == 0)
47 365 : break;
48 4743 : } while (--n != 0);
49 : }
50 :
51 : /* Not enough room in dst, add NUL and traverse rest of src */
52 378 : if (n == 0) {
53 13 : if (size != 0)
54 13 : *d = '\0'; /* NUL-terminate dst */
55 13 : while (*s++);
56 : }
57 :
58 378 : return (s - src - 1); /* count does not include NUL */
59 : }
60 :
61 : /*-------------------------------------------------------------------*/
62 :
63 : /*
64 : * Appends src to string dst of size siz (unlike strncat, siz is the
65 : * full size of dst, not space left). At most siz-1 characters
66 : * will be copied. Always NUL terminates (unless size <= strlen(dst)).
67 : * Returns strlen(src) + MIN(size, strlen(initial dst)).
68 : * If retval >= size, truncation occurred.
69 : */
70 46 : size_t mstrlcat(char *dst, const char *src, size_t size)
71 : {
72 46 : char *d = dst;
73 46 : const char *s = src;
74 46 : size_t n = size;
75 : size_t dlen;
76 :
77 : /* Find the end of dst and adjust bytes left but don't go past end */
78 114 : while (n-- != 0 && *d != '\0')
79 68 : d++;
80 46 : dlen = d - dst;
81 46 : n = size - dlen;
82 :
83 46 : if (n == 0)
84 0 : return (dlen + strlen(s));
85 519 : while (*s != '\0') {
86 473 : if (n != 1) {
87 473 : *d++ = *s;
88 473 : n--;
89 : }
90 473 : s++;
91 : }
92 46 : *d = '\0';
93 :
94 46 : return (dlen + (s - src)); /* count does not include NUL */
95 : }
96 :
97 : /*-------------------------------------------------------------------*/
|