[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <1fa46100c44abe54d5e66658ae69f3ba71522883.camel@linux.intel.com>
Date: Tue, 21 Aug 2018 15:00:58 +0300
From: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
To: Sergey Senozhatsky <sergey.senozhatsky.work@...il.com>,
Rasmus Villemoes <linux@...musvillemoes.dk>
Cc: Andrew Morton <akpm@...ux-foundation.org>,
Arnd Bergmann <arnd@...db.de>, Martin Wilck <mwilck@...e.com>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
linux-kernel@...r.kernel.org,
Sergey Senozhatsky <sergey.senozhatsky@...il.com>
Subject: Re: [RFC][PATCH] lib/string: introduce sysfs_strncpy() and
sysfs_strlcpy()
On Tue, 2018-08-21 at 20:44 +0900, Sergey Senozhatsky wrote:
> Something like below? Not tested, since we are still in "is this
> what we want" phase.
> Returning the length of dst/-EOVERFLOW is a bit inconvenient, because
> "the length" forces us to have size_t return, which is unsigned.
We have for ages ssize_t to workaround that.
> size_t strcpy_trim(char *dst, size_t dstsz, const char *src, size_t
> srcsz)
> {
> const char *end;
> size_t ret = 0;
> size_t len = 0;
>
> if (!dstsz || !srcsz)
> goto out;
>
> end = src + srcsz - 1;
> while (end >= src && isspace(*end))
> end--;
> end++;
> while (src < end && isspace(*src))
> src++;
> len = (src >= end) ? 0 : end - src;
> if (!len)
> goto out;
>
> ret = len;
> if (len >= dstsz)
> len = dstsz - 1;
> memcpy(dst, src, len);
> out:
> dst[len] = '\0';
> return ret == len ? ret : -EOVERFLOW;
> }
Wouldn't be better to split out something like
strnstrip() out of strim(), with simultaneous changes to strim(),
strstrip(), and use it here?
--
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
Intel Finland Oy
Powered by blists - more mailing lists