diff options
| author | Jeremy Fitzhardinge <jeremy@xensource.com> | 2007-07-17 21:37:02 -0400 |
|---|---|---|
| committer | Jeremy Fitzhardinge <jeremy@goop.org> | 2007-07-18 11:47:39 -0400 |
| commit | 1e66df3ee301209f4a38df097d7cc5cb9b367a3f (patch) | |
| tree | 55beb2a342dbe08c0404f749e02808e3f09023ac /mm/util.c | |
| parent | 8b4a40809e5330c9da5d20107d693d92d73b31dc (diff) | |
add kstrndup
Add a kstrndup function, modelled on strndup. Like strndup this
returns a string copied into its own allocated memory, but it copies
no more than the specified number of bytes from the source.
Remove private strndup() from irda code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Randy Dunlap <randy.dunlap@oracle.com>
Cc: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
Cc: Akinobu Mita <akinobu.mita@gmail.com>
Cc: Arnaldo Carvalho de Melo <acme@mandriva.com>
Cc: Al Viro <viro@ftp.linux.org.uk>
Cc: Panagiotis Issaris <takis@issaris.org>
Cc: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Diffstat (limited to 'mm/util.c')
| -rw-r--r-- | mm/util.c | 26 |
1 files changed, 24 insertions, 2 deletions
| @@ -6,7 +6,6 @@ | |||
| 6 | 6 | ||
| 7 | /** | 7 | /** |
| 8 | * kstrdup - allocate space for and copy an existing string | 8 | * kstrdup - allocate space for and copy an existing string |
| 9 | * | ||
| 10 | * @s: the string to duplicate | 9 | * @s: the string to duplicate |
| 11 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory | 10 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory |
| 12 | */ | 11 | */ |
| @@ -27,6 +26,30 @@ char *kstrdup(const char *s, gfp_t gfp) | |||
| 27 | EXPORT_SYMBOL(kstrdup); | 26 | EXPORT_SYMBOL(kstrdup); |
| 28 | 27 | ||
| 29 | /** | 28 | /** |
| 29 | * kstrndup - allocate space for and copy an existing string | ||
| 30 | * @s: the string to duplicate | ||
| 31 | * @max: read at most @max chars from @s | ||
| 32 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory | ||
| 33 | */ | ||
| 34 | char *kstrndup(const char *s, size_t max, gfp_t gfp) | ||
| 35 | { | ||
| 36 | size_t len; | ||
| 37 | char *buf; | ||
| 38 | |||
| 39 | if (!s) | ||
| 40 | return NULL; | ||
| 41 | |||
| 42 | len = strnlen(s, max); | ||
| 43 | buf = kmalloc_track_caller(len+1, gfp); | ||
| 44 | if (buf) { | ||
| 45 | memcpy(buf, s, len); | ||
| 46 | buf[len] = '\0'; | ||
| 47 | } | ||
| 48 | return buf; | ||
| 49 | } | ||
| 50 | EXPORT_SYMBOL(kstrndup); | ||
| 51 | |||
| 52 | /** | ||
| 30 | * kmemdup - duplicate region of memory | 53 | * kmemdup - duplicate region of memory |
| 31 | * | 54 | * |
| 32 | * @src: memory region to duplicate | 55 | * @src: memory region to duplicate |
| @@ -80,7 +103,6 @@ EXPORT_SYMBOL(krealloc); | |||
| 80 | 103 | ||
| 81 | /* | 104 | /* |
| 82 | * strndup_user - duplicate an existing string from user space | 105 | * strndup_user - duplicate an existing string from user space |
| 83 | * | ||
| 84 | * @s: The string to duplicate | 106 | * @s: The string to duplicate |
| 85 | * @n: Maximum number of bytes to copy, including the trailing NUL. | 107 | * @n: Maximum number of bytes to copy, including the trailing NUL. |
| 86 | */ | 108 | */ |
