Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Move strlcpy() from perf to tools/lib/string.c

strlcpy() will be needed by the subcmd library. Move it to the shared
tools/lib/string.c file which can be used by other tools.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/71e2804b973bf39ad3d3b9be10f99f2ea630be46.1450193761.git.jpoimboe@redhat.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Josh Poimboeuf and committed by
Arnaldo Carvalho de Melo
ce990917 1925459b

+33 -23
+4
tools/include/linux/string.h
··· 8 8 9 9 int strtobool(const char *s, bool *res); 10 10 11 + #ifndef __UCLIBC__ 12 + extern size_t strlcpy(char *dest, const char *src, size_t size); 13 + #endif 14 + 11 15 #endif /* _LINUX_STRING_H_ */
+27
tools/lib/string.c
··· 16 16 #include <string.h> 17 17 #include <errno.h> 18 18 #include <linux/string.h> 19 + #include <linux/compiler.h> 19 20 20 21 /** 21 22 * memdup - duplicate region of memory ··· 60 59 return -EINVAL; 61 60 } 62 61 return 0; 62 + } 63 + 64 + /** 65 + * strlcpy - Copy a C-string into a sized buffer 66 + * @dest: Where to copy the string to 67 + * @src: Where to copy the string from 68 + * @size: size of destination buffer 69 + * 70 + * Compatible with *BSD: the result is always a valid 71 + * NUL-terminated string that fits in the buffer (unless, 72 + * of course, the buffer size is zero). It does not pad 73 + * out the result like strncpy() does. 74 + * 75 + * If libc has strlcpy() then that version will override this 76 + * implementation: 77 + */ 78 + size_t __weak strlcpy(char *dest, const char *src, size_t size) 79 + { 80 + size_t ret = strlen(src); 81 + 82 + if (size) { 83 + size_t len = (ret >= size) ? size - 1 : ret; 84 + memcpy(dest, src, len); 85 + dest[len] = '\0'; 86 + } 87 + return ret; 63 88 }
+2 -5
tools/perf/util/cache.h
··· 8 8 #include "../perf.h" 9 9 #include "../ui/ui.h" 10 10 11 + #include <linux/string.h> 12 + 11 13 #define CMD_EXEC_PATH "--exec-path" 12 14 #define CMD_PERF_DIR "--perf-dir=" 13 15 #define CMD_WORK_TREE "--work-tree=" ··· 68 66 69 67 extern char *perf_pathdup(const char *fmt, ...) 70 68 __attribute__((format (printf, 1, 2))); 71 - 72 - #ifndef __UCLIBC__ 73 - /* Matches the libc/libbsd function attribute so we declare this unconditionally: */ 74 - extern size_t strlcpy(char *dest, const char *src, size_t size); 75 - #endif 76 69 77 70 #endif /* __PERF_CACHE_H */
-18
tools/perf/util/path.c
··· 22 22 return "."; 23 23 } 24 24 25 - /* 26 - * If libc has strlcpy() then that version will override this 27 - * implementation: 28 - */ 29 - size_t __weak strlcpy(char *dest, const char *src, size_t size) 30 - { 31 - size_t ret = strlen(src); 32 - 33 - if (size) { 34 - size_t len = (ret >= size) ? size - 1 : ret; 35 - 36 - memcpy(dest, src, len); 37 - dest[len] = '\0'; 38 - } 39 - 40 - return ret; 41 - } 42 - 43 25 static char *get_pathname(void) 44 26 { 45 27 static char pathname_array[4][PATH_MAX];