Skip to content

Commit ce99091

Browse files
jpoimboeacmel
authored andcommitted
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>
1 parent 1925459 commit ce99091

4 files changed

Lines changed: 33 additions & 23 deletions

File tree

tools/include/linux/string.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ void *memdup(const void *src, size_t len);
88

99
int strtobool(const char *s, bool *res);
1010

11+
#ifndef __UCLIBC__
12+
extern size_t strlcpy(char *dest, const char *src, size_t size);
13+
#endif
14+
1115
#endif /* _LINUX_STRING_H_ */

tools/lib/string.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <string.h>
1717
#include <errno.h>
1818
#include <linux/string.h>
19+
#include <linux/compiler.h>
1920

2021
/**
2122
* memdup - duplicate region of memory
@@ -60,3 +61,29 @@ int strtobool(const char *s, bool *res)
6061
}
6162
return 0;
6263
}
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+
}

tools/perf/util/cache.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "../perf.h"
99
#include "../ui/ui.h"
1010

11+
#include <linux/string.h>
12+
1113
#define CMD_EXEC_PATH "--exec-path"
1214
#define CMD_PERF_DIR "--perf-dir="
1315
#define CMD_WORK_TREE "--work-tree="
@@ -67,9 +69,4 @@ extern char *perf_path(const char *fmt, ...) __attribute__((format (printf, 1, 2
6769
extern char *perf_pathdup(const char *fmt, ...)
6870
__attribute__((format (printf, 1, 2)));
6971

70-
#ifndef __UCLIBC__
71-
/* Matches the libc/libbsd function attribute so we declare this unconditionally: */
72-
extern size_t strlcpy(char *dest, const char *src, size_t size);
73-
#endif
74-
7572
#endif /* __PERF_CACHE_H */

tools/perf/util/path.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,6 @@ static const char *get_perf_dir(void)
2222
return ".";
2323
}
2424

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-
4325
static char *get_pathname(void)
4426
{
4527
static char pathname_array[4][PATH_MAX];

0 commit comments

Comments
 (0)