[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20180821114405.GA597@jagdpanzerIV>
Date: Tue, 21 Aug 2018 20:44:05 +0900
From: Sergey Senozhatsky <sergey.senozhatsky.work@...il.com>
To: 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>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
linux-kernel@...r.kernel.org,
Sergey Senozhatsky <sergey.senozhatsky@...il.com>,
Sergey Senozhatsky <sergey.senozhatsky.work@...il.com>
Subject: Re: [RFC][PATCH] lib/string: introduce sysfs_strncpy() and
sysfs_strlcpy()
On (08/21/18 18:50), Sergey Senozhatsky wrote:
>
> > int strcpy_trim(char *dst, size_t dstsize, const char *src, size_t
> > srcsize) - copy (potentially not '\0'-terminated) src to dst, trimming
> > leading and trailing whitespace. dstsize must be positive, and dst is
> > guaranteed to be '\0'-terminated. Returns the length of the string now
> > in dst, or -EOVERFLOW if some none-whitespace character was chopped.
> >
> > would cover all use cases?
>
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.
This one returns a number of non-NULL bytes, which were copied
to dst, or 0.
---
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;
}
Powered by blists - more mailing lists