xref: /freebsd-src/contrib/ntp/ntpd/rc_cmdlength.c (revision 7847e04111f2c2b06b36f6d19a46d78814d7836d)
1276da39aSCy Schubert #include <config.h>
29034852cSGleb Smirnoff #include <rc_cmdlength.h>
3276da39aSCy Schubert 
4276da39aSCy Schubert #if HAVE_UNISTD_H
5276da39aSCy Schubert # include <unistd.h>
6276da39aSCy Schubert #endif
7276da39aSCy Schubert 
8*4e1ef62aSXin LI // XXX: Move to header.
9*4e1ef62aSXin LI size_t remoteconfig_cmdlength( const char *, const char *);
10276da39aSCy Schubert 
11276da39aSCy Schubert /* Bug 2853 */
12276da39aSCy Schubert /* evaluate the length of the command sequence. This breaks at the first
13276da39aSCy Schubert  * char that is not >= SPACE and <= 127 after trimming from the right.
14276da39aSCy Schubert  */
15276da39aSCy Schubert size_t
remoteconfig_cmdlength(const char * src_buf,const char * src_end)16276da39aSCy Schubert remoteconfig_cmdlength(
17276da39aSCy Schubert 	const char *src_buf,
18276da39aSCy Schubert 	const char *src_end
19276da39aSCy Schubert 	)
20276da39aSCy Schubert {
21276da39aSCy Schubert 	const char *scan;
22276da39aSCy Schubert 	unsigned char ch;
23276da39aSCy Schubert 
24276da39aSCy Schubert 	/* trim whitespace & garbage from the right */
25276da39aSCy Schubert 	while (src_end != src_buf) {
26276da39aSCy Schubert 		ch = src_end[-1];
27276da39aSCy Schubert 		if (ch > ' ' && ch < 128)
28276da39aSCy Schubert 			break;
29276da39aSCy Schubert 		--src_end;
30276da39aSCy Schubert 	}
31276da39aSCy Schubert 	/* now do a forward scan */
32276da39aSCy Schubert 	for (scan = src_buf; scan != src_end; ++scan) {
33276da39aSCy Schubert 		ch = scan[0];
34276da39aSCy Schubert 		if ((ch < ' ' || ch >= 128) && ch != '\t')
35276da39aSCy Schubert 			break;
36276da39aSCy Schubert 	}
37276da39aSCy Schubert 	return (size_t)(scan - src_buf);
38276da39aSCy Schubert }
39