xref: /freebsd/contrib/ntp/ntpd/rc_cmdlength.c (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1 #include <config.h>
2 #include <rc_cmdlength.h>
3 
4 #if HAVE_UNISTD_H
5 # include <unistd.h>
6 #endif
7 
8 // XXX: Move to header.
9 size_t remoteconfig_cmdlength( const char *, const char *);
10 
11 /* Bug 2853 */
12 /* evaluate the length of the command sequence. This breaks at the first
13  * char that is not >= SPACE and <= 127 after trimming from the right.
14  */
15 size_t
16 remoteconfig_cmdlength(
17 	const char *src_buf,
18 	const char *src_end
19 	)
20 {
21 	const char *scan;
22 	unsigned char ch;
23 
24 	/* trim whitespace & garbage from the right */
25 	while (src_end != src_buf) {
26 		ch = src_end[-1];
27 		if (ch > ' ' && ch < 128)
28 			break;
29 		--src_end;
30 	}
31 	/* now do a forward scan */
32 	for (scan = src_buf; scan != src_end; ++scan) {
33 		ch = scan[0];
34 		if ((ch < ' ' || ch >= 128) && ch != '\t')
35 			break;
36 	}
37 	return (size_t)(scan - src_buf);
38 }
39