| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 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, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file ut/ut0mem.cc |
| 29 |
Memory primitives |
| 30 |
|
| 31 |
Created 5/11/1994 Heikki Tuuri |
| 32 |
*************************************************************************/ |
| 33 |
|
| 34 |
#include "ut0mem.h" |
| 35 |
#include <stdlib.h> |
| 36 |
#include "os0thread.h" |
| 37 |
#include "srv0srv.h" |
| 38 |
|
| 39 |
/** Copies up to size - 1 characters from the NUL-terminated string src to |
| 40 |
dst, NUL-terminating the result. Returns strlen(src), so truncation |
| 41 |
occurred if the return value >= size. |
| 42 |
@return strlen(src) */ |
| 43 |
ulint ut_strlcpy(char *dst, /*!< in: destination buffer */ |
| 44 |
const char *src, /*!< in: source buffer */ |
| 45 |
ulint size) /*!< in: size of destination buffer */ |
| 46 |
{ |
| 47 |
ulint src_size = strlen(src); |
| 48 |
|
| 49 |
if (size != 0) { |
| 50 |
ulint n = std::min(src_size, size - 1); |
| 51 |
|
| 52 |
memcpy(dst, src, n); |
| 53 |
dst[n] = '\0'; |
| 54 |
} |
| 55 |
|
| 56 |
return (src_size); |
| 57 |
} |
| 58 |
|
| 59 |
/******************************************************************** |
| 60 |
Concatenate 3 strings.*/ |
| 61 |
char *ut_str3cat( |
| 62 |
/* out, own: concatenated string, must be |
| 63 |
freed with ut::free() */ |
| 64 |
const char *s1, /* in: string 1 */ |
| 65 |
const char *s2, /* in: string 2 */ |
| 66 |
const char *s3) /* in: string 3 */ |
| 67 |
{ |
| 68 |
char *s; |
| 69 |
ulint s1_len = strlen(s1); |
| 70 |
ulint s2_len = strlen(s2); |
| 71 |
ulint s3_len = strlen(s3); |
| 72 |
|
| 73 |
s = static_cast<char *>(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, |
| 74 |
s1_len + s2_len + s3_len + 1)); |
| 75 |
|
| 76 |
memcpy(s, s1, s1_len); |
| 77 |
memcpy(s + s1_len, s2, s2_len); |
| 78 |
memcpy(s + s1_len + s2_len, s3, s3_len); |
| 79 |
|
| 80 |
s[s1_len + s2_len + s3_len] = '\0'; |
| 81 |
|
| 82 |
return (s); |
| 83 |
} |