1 | /***************************************************************************** |
2 | |
3 | Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved. |
4 | |
5 | This program is free software; you can redistribute it and/or modify it under |
6 | the terms of the GNU General Public License as published by the Free Software |
7 | Foundation; version 2 of the License. |
8 | |
9 | This program is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
11 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License along with |
14 | this program; if not, write to the Free Software Foundation, Inc., |
15 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
16 | |
17 | *****************************************************************************/ |
18 | |
19 | /********************************************************************//** |
20 | @file ut/ut0mem.cc |
21 | Memory primitives |
22 | |
23 | Created 5/11/1994 Heikki Tuuri |
24 | *************************************************************************/ |
25 | |
26 | #include "ut0mem.h" |
27 | #include "os0thread.h" |
28 | #include "srv0srv.h" |
29 | #include <stdlib.h> |
30 | |
31 | /**********************************************************************//** |
32 | Copies up to size - 1 characters from the NUL-terminated string src to |
33 | dst, NUL-terminating the result. Returns strlen(src), so truncation |
34 | occurred if the return value >= size. |
35 | @return strlen(src) */ |
36 | ulint |
37 | ut_strlcpy( |
38 | /*=======*/ |
39 | char* dst, /*!< in: destination buffer */ |
40 | const char* src, /*!< in: source buffer */ |
41 | ulint size) /*!< in: size of destination buffer */ |
42 | { |
43 | ulint src_size = strlen(src); |
44 | |
45 | if (size != 0) { |
46 | ulint n = ut_min(src_size, size - 1); |
47 | |
48 | memcpy(dst, src, n); |
49 | dst[n] = '\0'; |
50 | } |
51 | |
52 | return(src_size); |
53 | } |
54 | |
55 | /**********************************************************************//** |
56 | Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last |
57 | (size - 1) bytes of src, not the first. |
58 | @return strlen(src) */ |
59 | ulint |
60 | ut_strlcpy_rev( |
61 | /*===========*/ |
62 | char* dst, /*!< in: destination buffer */ |
63 | const char* src, /*!< in: source buffer */ |
64 | ulint size) /*!< in: size of destination buffer */ |
65 | { |
66 | ulint src_size = strlen(src); |
67 | |
68 | if (size != 0) { |
69 | ulint n = ut_min(src_size, size - 1); |
70 | |
71 | memcpy(dst, src + src_size - n, n + 1); |
72 | } |
73 | |
74 | return(src_size); |
75 | } |
76 | |
77 | /******************************************************************** |
78 | Concatenate 3 strings.*/ |
79 | char* |
80 | ut_str3cat( |
81 | /*=======*/ |
82 | /* out, own: concatenated string, must be |
83 | freed with ut_free() */ |
84 | const char* s1, /* in: string 1 */ |
85 | const char* s2, /* in: string 2 */ |
86 | const char* s3) /* in: string 3 */ |
87 | { |
88 | char* s; |
89 | ulint s1_len = strlen(s1); |
90 | ulint s2_len = strlen(s2); |
91 | ulint s3_len = strlen(s3); |
92 | |
93 | s = static_cast<char*>(ut_malloc_nokey(s1_len + s2_len + s3_len + 1)); |
94 | |
95 | memcpy(s, s1, s1_len); |
96 | memcpy(s + s1_len, s2, s2_len); |
97 | memcpy(s + s1_len + s2_len, s3, s3_len); |
98 | |
99 | s[s1_len + s2_len + s3_len] = '\0'; |
100 | |
101 | return(s); |
102 | } |
103 | |