libbb/trim.c

souf souf_oued at yahoo.fr
Wed Jun 13 22:29:55 UTC 2007


I tried to rewrite libbb/trim.c by adding new functions, a similar
functions are dispersed in busybox:
coreutils/cal.c
miscutils/crond.c
procps/sysctl.c
shell/lash.c
shell/ash.c
applets/applets.c

trim.c =========================================================
#include "libbb.h"

char *bb_strtrim(char *s, int c)
{
	char *t = NULL;

	if (!s) return NULL;

	if (c)
		t = strchr(s, c);
	else
		t = s;

	t = skip_whitespace(t);

	memmove(s, t, strlen(t));
	memset(s+strlen(t), 0, strlen(s)-strlen(t));

	return s;
}

char *bb_strrtrim(char *s, int c)
{
	char *t = NULL;

	if (!s) return NULL;

	if (c)
		t = strrchr(s, c);
	else
		t = s + strlen(s);

	while (t-- > s) {
		if (!isspace(*t)) {
			t[1] = '\0';
			break;
		}
	}

	return s;
}

char *bb_strtrim_index(char *s, unsigned int i)
{
	if (!s || i > strlen(s)) {
		return (s = NULL);
	}

	return bb_strtrim(s, s[i]);
}

char *bb_strrtrim_index(char *s, unsigned int i)
{
	if (!s || i > strlen(s)) {
		return (s = NULL);
	}

	return bb_strrtrim(s, s[i]);
}

char *bb_trimmed_slice(char *s, int l, int t)
{
	bb_strtrim(s, l);
	bb_strrtrim(s, t);

	return s;
}

char *trim(char *s)
{
	return bb_trimmed_slice(s, 0, 0);
}

I do not know if it is interesting or not ?

Malek (Souf).





More information about the busybox mailing list