libbb: make trim() return pointer to terminating NUL
function old new delta trim 80 90 +10 angle_address 56 50 -6 sysctl_main 282 273 -9 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/3 up/down: +10/-15) Total: -5 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
		| @@ -347,7 +347,7 @@ unsigned long long monotonic_ms(void) FAST_FUNC; | |||||||
| unsigned monotonic_sec(void) FAST_FUNC; | unsigned monotonic_sec(void) FAST_FUNC; | ||||||
|  |  | ||||||
| extern void chomp(char *s) FAST_FUNC; | extern void chomp(char *s) FAST_FUNC; | ||||||
| extern void trim(char *s) FAST_FUNC; | extern char *trim(char *s) FAST_FUNC; | ||||||
| extern char *skip_whitespace(const char *) FAST_FUNC; | extern char *skip_whitespace(const char *) FAST_FUNC; | ||||||
| extern char *skip_non_whitespace(const char *) FAST_FUNC; | extern char *skip_non_whitespace(const char *) FAST_FUNC; | ||||||
| extern char *skip_dev_pfx(const char *tty_name) FAST_FUNC; | extern char *skip_dev_pfx(const char *tty_name) FAST_FUNC; | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								libbb/trim.c
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								libbb/trim.c
									
									
									
									
									
								
							| @@ -10,9 +10,10 @@ | |||||||
|  |  | ||||||
| #include "libbb.h" | #include "libbb.h" | ||||||
|  |  | ||||||
| void FAST_FUNC trim(char *s) | char* FAST_FUNC trim(char *s) | ||||||
| { | { | ||||||
| 	size_t len = strlen(s); | 	size_t len = strlen(s); | ||||||
|  | 	size_t old = len; | ||||||
|  |  | ||||||
| 	/* trim trailing whitespace */ | 	/* trim trailing whitespace */ | ||||||
| 	while (len && isspace(s[len-1])) | 	while (len && isspace(s[len-1])) | ||||||
| @@ -26,5 +27,12 @@ void FAST_FUNC trim(char *s) | |||||||
| 			memmove(s, nws, len); | 			memmove(s, nws, len); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	s[len] = '\0'; |  | ||||||
|  | 	s += len; | ||||||
|  | 	/* If it was a "const char*" which does not need trimming, | ||||||
|  | 	 * avoid superfluous store */ | ||||||
|  | 	if (old != len) | ||||||
|  | 		*s = '\0'; | ||||||
|  |  | ||||||
|  | 	return s; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -166,9 +166,8 @@ static char *angle_address(char *str) | |||||||
| { | { | ||||||
| 	char *s, *e; | 	char *s, *e; | ||||||
|  |  | ||||||
| 	trim(str); | 	e = trim(str); | ||||||
| 	e = last_char_is(str, '>'); | 	if (e != str && e[-1] == '>') { | ||||||
| 	if (e) { |  | ||||||
| 		s = strrchr(str, '<'); | 		s = strrchr(str, '<'); | ||||||
| 		if (s) { | 		if (s) { | ||||||
| 			*e = '\0'; | 			*e = '\0'; | ||||||
|   | |||||||
| @@ -37,9 +37,8 @@ static char *get_line(const char *filename, char *buf, unsigned *bufsize_p) | |||||||
| 	if (sz < 0) | 	if (sz < 0) | ||||||
| 		sz = 0; | 		sz = 0; | ||||||
| 	buf[sz] = '\0'; | 	buf[sz] = '\0'; | ||||||
| 	trim(buf); |  | ||||||
|  |  | ||||||
| 	sz = strlen(buf) + 1; | 	sz = (trim(buf) - buf) + 1; | ||||||
| 	bufsize -= sz; | 	bufsize -= sz; | ||||||
| 	buf += sz; | 	buf += sz; | ||||||
| 	buf[0] = '\0'; | 	buf[0] = '\0'; | ||||||
|   | |||||||
| @@ -257,12 +257,16 @@ static int sysctl_handle_preload_file(const char *filename) | |||||||
| 	parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char | 	parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char | ||||||
| 	while (config_read(parser, token, 2, 2, "#=", parse_flags)) { | 	while (config_read(parser, token, 2, 2, "#=", parse_flags)) { | ||||||
| 		char *tp; | 		char *tp; | ||||||
| 		trim(token[0]); |  | ||||||
| 		trim(token[1]); | 		trim(token[1]); | ||||||
|  | 		tp = trim(token[0]); | ||||||
| 		sysctl_dots_to_slashes(token[0]); | 		sysctl_dots_to_slashes(token[0]); | ||||||
| 		tp = xasprintf("%s=%s", token[0], token[1]); | 		/* ^^^converted in-place. tp still points to NUL */ | ||||||
| 		sysctl_act_on_setting(tp); | 		/* now, add "=TOKEN1" */ | ||||||
| 		free(tp); | 		*tp++ = '='; | ||||||
|  | 		overlapping_strcpy(tp, token[1]); | ||||||
|  |  | ||||||
|  | 		sysctl_act_on_setting(token[0]); | ||||||
| 	} | 	} | ||||||
| 	if (ENABLE_FEATURE_CLEAN_UP) | 	if (ENABLE_FEATURE_CLEAN_UP) | ||||||
| 		config_close(parser); | 		config_close(parser); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user