#include <stdio.h>
#include <string.h>

void trim_right(char *s) {
	char *space_pos = NULL;
	for(;;++s) {
		char ch = *s;
		if(ch == '\0') {
			break;
		} else if(ch == ' ') {
		    if(!space_pos)
                space_pos = s;
		} else {
			space_pos = NULL;
		}
	}
	if(space_pos) {
		*space_pos = '\0';
	}
}

char buf[1024];
void test(const char *arg, const char *expected) {
	snprintf(buf, sizeof(buf), "%s", arg);
	trim_right(buf);
	if(strcmp(buf, expected) == 0) {
		printf("OK: '%s' -> '%s'\n", arg, expected);
	} else {
		printf("FAIL: arg is '%s', expected '%s', found '%s'.\n", arg, expected, buf);
	}
}

int main(void) {
	test("", "");
	test("word", "word");
	test(" foo ", " foo");
	test(" bar   ", " bar");
	test("     ", "");
	test("this failure is expected", "...");
	return 0;
}
