1284990Scy#include <config.h>
2290000Sglebius#include <rc_cmdlength.h>
3284990Scy
4284990Scy#if HAVE_UNISTD_H
5284990Scy# include <unistd.h>
6284990Scy#endif
7284990Scy
8284990Scy
9284990Scy/* Bug 2853 */
10284990Scy/* evaluate the length of the command sequence. This breaks at the first
11284990Scy * char that is not >= SPACE and <= 127 after trimming from the right.
12284990Scy */
13284990Scysize_t
14284990Scyremoteconfig_cmdlength(
15284990Scy	const char *src_buf,
16284990Scy	const char *src_end
17284990Scy	)
18284990Scy{
19284990Scy	const char *scan;
20284990Scy	unsigned char ch;
21284990Scy
22284990Scy	/* trim whitespace & garbage from the right */
23284990Scy	while (src_end != src_buf) {
24284990Scy		ch = src_end[-1];
25284990Scy		if (ch > ' ' && ch < 128)
26284990Scy			break;
27284990Scy		--src_end;
28284990Scy	}
29284990Scy	/* now do a forward scan */
30284990Scy	for (scan = src_buf; scan != src_end; ++scan) {
31284990Scy		ch = scan[0];
32284990Scy		if ((ch < ' ' || ch >= 128) && ch != '\t')
33284990Scy			break;
34284990Scy	}
35284990Scy	return (size_t)(scan - src_buf);
36284990Scy}
37