~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/lib/string.c

Version: ~ [ linux-5.9-rc5 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/tools/lib/string.c
  3  *
  4  *  Copied from linux/lib/string.c, where it is:
  5  *
  6  *  Copyright (C) 1991, 1992  Linus Torvalds
  7  *
  8  *  More specifically, the first copied function was strtobool, which
  9  *  was introduced by:
 10  *
 11  *  d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents")
 12  *  Author: Jonathan Cameron <jic23@cam.ac.uk>
 13  */
 14 
 15 #include <stdlib.h>
 16 #include <string.h>
 17 #include <errno.h>
 18 #include <linux/string.h>
 19 #include <linux/compiler.h>
 20 
 21 /**
 22  * memdup - duplicate region of memory
 23  *
 24  * @src: memory region to duplicate
 25  * @len: memory region length
 26  */
 27 void *memdup(const void *src, size_t len)
 28 {
 29         void *p = malloc(len);
 30 
 31         if (p)
 32                 memcpy(p, src, len);
 33 
 34         return p;
 35 }
 36 
 37 /**
 38  * strtobool - convert common user inputs into boolean values
 39  * @s: input string
 40  * @res: result
 41  *
 42  * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
 43  * Otherwise it will return -EINVAL.  Value pointed to by res is
 44  * updated upon finding a match.
 45  */
 46 int strtobool(const char *s, bool *res)
 47 {
 48         switch (s[0]) {
 49         case 'y':
 50         case 'Y':
 51         case '1':
 52                 *res = true;
 53                 break;
 54         case 'n':
 55         case 'N':
 56         case '':
 57                 *res = false;
 58                 break;
 59         default:
 60                 return -EINVAL;
 61         }
 62         return 0;
 63 }
 64 
 65 /**
 66  * strlcpy - Copy a C-string into a sized buffer
 67  * @dest: Where to copy the string to
 68  * @src: Where to copy the string from
 69  * @size: size of destination buffer
 70  *
 71  * Compatible with *BSD: the result is always a valid
 72  * NUL-terminated string that fits in the buffer (unless,
 73  * of course, the buffer size is zero). It does not pad
 74  * out the result like strncpy() does.
 75  *
 76  * If libc has strlcpy() then that version will override this
 77  * implementation:
 78  */
 79 size_t __weak strlcpy(char *dest, const char *src, size_t size)
 80 {
 81         size_t ret = strlen(src);
 82 
 83         if (size) {
 84                 size_t len = (ret >= size) ? size - 1 : ret;
 85                 memcpy(dest, src, len);
 86                 dest[len] = '\0';
 87         }
 88         return ret;
 89 }
 90 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp