On Wed, Aug 26, 2015 at 10:18:33AM -0700, Isaac Dunham wrote:
> By my test (Debian Jessie, GCC 4.9.2, glibc shared build), the results are:
> function                                             old     new   delta
> is_suffixed_with                                      61      45     -16
> ------------------------------------------------------------------------------
> (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-16)             Total: -16 bytes
>    text          data     bss     dec     hex filename
>  760460          2092    9080  771632   bc630 busybox_old
>  760444          2092    9080  771616   bc620 busybox_unstripped
> ---
>  libbb/compare_string_array.c | 15 +++++----------
>  1 file changed, 5 insertions(+), 10 deletions(-)
> 
> diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c
> @@ -35,17 +35,12 @@ char* FAST_FUNC is_prefixed_with(const char *string, 
> const char *key)
>   */
>  char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
>  {
> +     char *ret;
>  
> +     ret = strstr(string, key);
> +     if (ret && strcmp(ret, key))
> +             ret = NULL;
> +     return ret;
>  }

I forgot to check/update the documentation; the behavior is slightly
different in the case of an empty key.
As a side effect of the implementation, it returns NULL if key is "",
unless string is also "".
It's probably not going to be smaller if this change is not kept.

Regardless which behavior is used, is_suffixed_with() can be used to check
that config files in a directory are named *.conf or for similar purposes;
I think standard modprobe has this behavior, so  for example the file
/etc/modprobe.d/file.conf.ignore
will be ignored. If there's anywhere that we check this, I can't see it.

To prevent the function from being optimized out, I wrote a small applet
which I'm attaching.

Thanks,
Isaac Dunham
/* vi: set sw=4 ts=4: */
/*
 * suftest - dummy tool to test is_suffixed_with() changes
 *
 * Copyright 2015 A.D., Isaac Dunham
 * Licensed under GPLv2, see file LICENSE in this source tree.
 */

//config:config SUFTEST
//config:	bool "suftest"
//config:	default n
//config:	help
//config:	  suftest is a dummy program to drag in is_suffixed_with()

//applet:IF_SUFTEST(APPLET(suftest, BB_DIR_USR_BIN, BB_SUID_DROP))

//kbuild:lib-$(CONFIG_SUFTEST) += suftest.o

//usage:#define suftest_trivial_usage
//usage:	"STRING SUFFIX"
//usage:#define suftest_full_usage "\n"
//usage:	"Test if STRING ends with SUFFIX"

#include "libbb.h"

int suftest_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int suftest_main(int argc, char **argv)
{
	if (argc < 3)
		bb_show_usage();
	
	if (is_suffixed_with(argv[1], argv[2]))
		return 0;
	return 1;
}
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to