opal/util/string_copy: add "safe" string copy function
Do essentially the same thing as strncpy(3), but a) ensure to always terminate the destination buffer with a \0, and b) do not \0-pad to the right. Signed-off-by: Jeff Squyres <jsquyres@cisco.com>
Этот коммит содержится в:
родитель
38fefcf1cf
Коммит
e1cf8757ae
@ -71,6 +71,7 @@ headers = \
|
|||||||
show_help_lex.h \
|
show_help_lex.h \
|
||||||
stacktrace.h \
|
stacktrace.h \
|
||||||
strncpy.h \
|
strncpy.h \
|
||||||
|
string_copy.h \
|
||||||
sys_limits.h \
|
sys_limits.h \
|
||||||
timings.h \
|
timings.h \
|
||||||
uri.h \
|
uri.h \
|
||||||
@ -110,6 +111,7 @@ libopalutil_la_SOURCES = \
|
|||||||
show_help_lex.l \
|
show_help_lex.l \
|
||||||
stacktrace.c \
|
stacktrace.c \
|
||||||
strncpy.c \
|
strncpy.c \
|
||||||
|
string_copy.c \
|
||||||
sys_limits.c \
|
sys_limits.c \
|
||||||
uri.c \
|
uri.c \
|
||||||
info_subscriber.c \
|
info_subscriber.c \
|
||||||
|
28
opal/util/string_copy.c
Обычный файл
28
opal/util/string_copy.c
Обычный файл
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018 Cisco Systems, Inc. All rights reserved.
|
||||||
|
* $COPYRIGHT$
|
||||||
|
*
|
||||||
|
* Additional copyrights may follow
|
||||||
|
*
|
||||||
|
* $HEADER$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "opal_config.h"
|
||||||
|
|
||||||
|
#include "opal/util/string_copy.h"
|
||||||
|
|
||||||
|
|
||||||
|
void opal_string_copy(char *dest, const char *src, size_t len)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char *new_dest = dest;
|
||||||
|
|
||||||
|
for (i = 0; i < len; ++i, ++src, ++new_dest) {
|
||||||
|
*new_dest = *src;
|
||||||
|
if ('\0' == *src) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dest[i - 1] = '\0';
|
||||||
|
}
|
62
opal/util/string_copy.h
Обычный файл
62
opal/util/string_copy.h
Обычный файл
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
||||||
|
* University Research and Technology
|
||||||
|
* Corporation. All rights reserved.
|
||||||
|
* Copyright (c) 2004-2006 The University of Tennessee and The University
|
||||||
|
* of Tennessee Research Foundation. All rights
|
||||||
|
* reserved.
|
||||||
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
||||||
|
* University of Stuttgart. All rights reserved.
|
||||||
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
||||||
|
* All rights reserved.
|
||||||
|
* $COPYRIGHT$
|
||||||
|
*
|
||||||
|
* Additional copyrights may follow
|
||||||
|
*
|
||||||
|
* $HEADER$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef OPAL_STRING_COPY_H
|
||||||
|
#define OPAL_STRING_COPY_H
|
||||||
|
|
||||||
|
#include "opal_config.h"
|
||||||
|
#ifdef HAVE_SYS_TYPES_H
|
||||||
|
#include <sys/types.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
BEGIN_C_DECLS
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Do a "safe" string copy (i.e., guarantee to \0-terminate the
|
||||||
|
* destination string).
|
||||||
|
*
|
||||||
|
* @param dest Destination string buffer.
|
||||||
|
* @param src Source string buffer.
|
||||||
|
* @param len Length of the destination string buffer.
|
||||||
|
*
|
||||||
|
* This function is similar to, but different than, strcpy() and
|
||||||
|
* strncpy().
|
||||||
|
*
|
||||||
|
* It is invalid to pass NULL for either dest or src.
|
||||||
|
*
|
||||||
|
* There is no return value.
|
||||||
|
*
|
||||||
|
* This function will essentially do the same thing as strncpy(),
|
||||||
|
* except that a) it will guarantee to to terminate the destination
|
||||||
|
* string with a \0, and b) it will not \0-pad to the right.
|
||||||
|
* Specifically:
|
||||||
|
*
|
||||||
|
* - If the length of the source string is less than (len), the entire
|
||||||
|
* source string will be copied to the destination, including the
|
||||||
|
* \0.
|
||||||
|
* - If the length of the source string is greater than (len), then
|
||||||
|
* (len-1) characters of the source string will be copied to the
|
||||||
|
* destination, and dest[len-1] will be set to '\0'.
|
||||||
|
*/
|
||||||
|
OPAL_DECLSPEC void opal_string_copy(char *dest, const char *src,
|
||||||
|
size_t len)
|
||||||
|
__opal_attribute_nonnull__(1) __opal_attribute_nonnull__(2);
|
||||||
|
|
||||||
|
END_C_DECLS
|
||||||
|
|
||||||
|
#endif /* OPAL_STRING_COPY_H */
|
Загрузка…
x
Ссылка в новой задаче
Block a user