/*
 * Helper function for splitting a string into an argv-like array.
 */

#include <linux/kernel.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/export.h>

static int count_argc(const char *str)
{
	int count = 0;
	bool was_space;

	for (was_space = true; *str; str++) {
		if (isspace(*str)) {
			was_space = true;
		} else if (was_space) {
			was_space = false;
			count++;
		}
	}

	return count;
}

/**
 * argv_free - free an argv
 * @argv - the argument vector to be freed
 *
 * Frees an argv and the strings it points to.
 */
void argv_free(char **argv)
{
	argv--;
	kfree(argv[0]);
	kfree(argv);
}
EXPORT_SYMBOL(argv_free);

/**
 * argv_split - split a string at whitespace, returning an argv
 * @gfp: the GFP mask used to allocate memory
 * @str: the string to be split
 * @argcp: returned argument count
 *
 * Returns an array of pointers to strings which are split out from
 * @str.  This is performed by strictly splitting on white-space; no
 * quote processing is performed.  Multiple whitespace characters are
 * considered to be a single argument separator.  The returned array
 * is always NULL-terminated.  Returns NULL on memory allocation
 * failure.
 *
 * The source string at `str' may be undergoing concurrent alteration via
 * userspace sysctl activity (at least).  The argv_split() implementation
 * attempts to handle this gracefully by taking a local copy to work on.
 */
char **argv_split(gfp_t gfp, const char *str, int *argcp)
{
	char *argv_str;
	bool was_space;
	char **argv, **argv_ret;
	int argc;

	argv_str = kstrndup(str, KMALLOC_MAX_SIZE - 1, gfp);
	if (!argv_str)
		return NULL;

	argc = count_argc(argv_str);
	argv = kmalloc(sizeof(*argv) * (argc + 2), gfp);
	if (!argv) {
		kfree(argv_str);
		return NULL;
	}

	*argv = argv_str;
	argv_ret = ++argv;
	for (was_space = true; *argv_str; argv_str++) {
		if (isspace(*argv_str)) {
			was_space = true;
			*argv_str = 0;
		} else if (was_space) {
			was_space = false;
			*argv++ = argv_str;
		}
	}
	*argv = NULL;

	if (argcp)
		*argcp = argc;
	return argv_ret;
}
EXPORT_SYMBOL(argv_split);
q_clientmgr.h?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>diff</a></td><td class='form'><form class='right' method='get' action='/cgit.cgi/linux/net-next.git/log/sound/core/seq/seq_clientmgr.h'>
<input type='hidden' name='id' value='23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/cgit.cgi/linux/net-next.git/commit/?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>root</a>/<a href='/cgit.cgi/linux/net-next.git/commit/sound?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>sound</a>/<a href='/cgit.cgi/linux/net-next.git/commit/sound/core?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>core</a>/<a href='/cgit.cgi/linux/net-next.git/commit/sound/core/seq?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>seq</a>/<a href='/cgit.cgi/linux/net-next.git/commit/sound/core/seq/seq_clientmgr.h?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>seq_clientmgr.h</a></div><div class='content'><div class='cgit-panel'><b>diff options</b><form method='get'><input type='hidden' name='id' value='23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'/><table><tr><td colspan='2'/></tr><tr><td class='label'>context:</td><td class='ctrl'><select name='context' onchange='this.form.submit();'><option value='1'>1</option><option value='2'>2</option><option value='3' selected='selected'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><table summary='commit info' class='commit-info'>
<tr><th>author</th><td>Linus Torvalds &lt;torvalds@linux-foundation.org&gt;</td><td class='right'>2017-02-08 09:56:15 -0800</td></tr>
<tr><th>committer</th><td>Linus Torvalds &lt;torvalds@linux-foundation.org&gt;</td><td class='right'>2017-02-08 09:56:15 -0800</td></tr>
<tr><th>commit</th><td colspan='2' class='oid'><a href='/cgit.cgi/linux/net-next.git/commit/sound/core/seq/seq_clientmgr.h?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b</a> (<a href='/cgit.cgi/linux/net-next.git/patch/sound/core/seq/seq_clientmgr.h?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>patch</a>)</td></tr>
<tr><th>tree</th><td colspan='2' class='oid'><a href='/cgit.cgi/linux/net-next.git/tree/?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>4aff32d5f2f6fe2f54028bfd4d1b66fdd2f8d281</a> /<a href='/cgit.cgi/linux/net-next.git/tree/sound/core/seq/seq_clientmgr.h?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>sound/core/seq/seq_clientmgr.h</a></td></tr>
<tr><th>parent</th><td colspan='2' class='oid'><a href='/cgit.cgi/linux/net-next.git/commit/sound/core/seq/seq_clientmgr.h?id=926af6273fc683cd98cd0ce7bf0d04a02eed6742'>926af6273fc683cd98cd0ce7bf0d04a02eed6742</a> (<a href='/cgit.cgi/linux/net-next.git/diff/sound/core/seq/seq_clientmgr.h?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b&amp;id2=926af6273fc683cd98cd0ce7bf0d04a02eed6742'>diff</a>)</td></tr><tr><th>parent</th><td colspan='2' class='oid'><a href='/cgit.cgi/linux/net-next.git/commit/sound/core/seq/seq_clientmgr.h?id=eeeefd41843218c55a8782a6920f044d9bf6207a'>eeeefd41843218c55a8782a6920f044d9bf6207a</a> (<a href='/cgit.cgi/linux/net-next.git/diff/sound/core/seq/seq_clientmgr.h?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b&amp;id2=eeeefd41843218c55a8782a6920f044d9bf6207a'>diff</a>)</td></tr></table>
<div class='commit-subject'>Merge branch 'for-linus' of git://git.kernel.dk/linux-block</div><div class='commit-msg'>Pull block fix from Jens Axboe:
 "A single fix that should go into 4.10, fixing a regression on some
  devices with the WRITE_SAME command"

* 'for-linus' of git://git.kernel.dk/linux-block:
  block: don't try Write Same from __blkdev_issue_zeroout
</div><div class='diffstat-header'><a href='/cgit.cgi/linux/net-next.git/diff/?id=23fbe2cdc1de80120cf9ccd478ac57c3a3a0764b'>Diffstat</a> (limited to 'sound/core/seq/seq_clientmgr.h')</div><table summary='diffstat' class='diffstat'>