view toys/basename.c @ 239:4f1ca01db000

Fluff out hello.c to supply more example code as a skeleton for new commands, and update a chunk of code.html (much more to do there).
author Rob Landley <rob@landley.net>
date Sun, 20 Jan 2008 19:00:16 -0600
parents 163498bf547b
children
line wrap: on
line source

/* vi: set sw=4 ts=4:
 *
 * basename.c - print non-directory portion of path
 *
 * Copyright 2007 Charlie Shepherd <masterdriverz@gentoo.org>
 *
 * See http://www.opengroup.org/onlinepubs/009695399/utilities/basename.html

USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_BIN))

config BASENAME
	bool "basename"
	default y
	help
	  usage: basename path [suffix]

	  Print the part of path after the last slash, optionally minus suffix.
*/

#include "toys.h"

void basename_main(void)
{
	char *name = basename(*toys.optargs);
	char *suffix = toys.optargs[1];
	if (suffix) {
		char *end = name+strlen(name)-strlen(suffix);
		if (end>name && !strcmp(end,suffix)) *end=0;
	}
	puts(name);
}