xref: /src/sys/contrib/openzfs/include/os/linux/spl/sys/string.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
4  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6  *  UCRL-CODE-235197
7  *
8  *  This file is part of the SPL, Solaris Porting Layer.
9  *
10  *  The SPL is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the
12  *  Free Software Foundation; either version 2 of the License, or (at your
13  *  option) any later version.
14  *
15  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18  *  for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef _SPL_STRING_H
25 #define	_SPL_STRING_H
26 
27 #include <linux/string.h>
28 
29 #ifndef HAVE_KERNEL_STRLCPY
30 /*
31  * strscpy is strlcpy, but returns an error on truncation. strlcpy is defined
32  * to return strlen(src), so detect error and override it.
33  */
34 static inline size_t
strlcpy(char * dest,const char * src,size_t size)35 strlcpy(char *dest, const char *src, size_t size)
36 {
37 	ssize_t ret = strscpy(dest, src, size);
38 	if (likely(ret > 0))
39 		return ((size_t)ret);
40 	return (strlen(src));
41 }
42 #endif /* HAVE_KERNEL_STRLCPY */
43 
44 #endif /* _SPL_STRING_H */
45