public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Arnd Bergmann <arnd@arndb.de>, Martin Wilck <mwilck@suse.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	linux-kernel@vger.kernel.org,
	Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
	Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Subject: Re: [RFC][PATCH] lib/string: introduce sysfs_strncpy() and sysfs_strlcpy()
Date: Tue, 21 Aug 2018 20:44:05 +0900	[thread overview]
Message-ID: <20180821114405.GA597@jagdpanzerIV> (raw)
In-Reply-To: <20180821095055.GA400@jagdpanzerIV>

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;
}

  parent reply	other threads:[~2018-08-21 11:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-21  6:24 [RFC][PATCH] lib/string: introduce sysfs_strncpy() and sysfs_strlcpy() Sergey Senozhatsky
2018-08-21  7:59 ` Rasmus Villemoes
2018-08-21  9:50   ` Sergey Senozhatsky
2018-08-21  9:54     ` Sergey Senozhatsky
2018-08-21 11:44     ` Sergey Senozhatsky [this message]
2018-08-21 12:00       ` Andy Shevchenko
2018-08-22  0:32         ` Sergey Senozhatsky
2018-08-21 12:43     ` Rasmus Villemoes
2018-08-22  5:13       ` Sergey Senozhatsky
2018-08-21 13:57     ` Greg Kroah-Hartman
2018-08-22  4:58       ` Sergey Senozhatsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180821114405.GA597@jagdpanzerIV \
    --to=sergey.senozhatsky.work@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=mwilck@suse.com \
    --cc=sergey.senozhatsky@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox