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