aboutsummaryrefslogtreecommitdiff
path: root/toys/basename.c
blob: ffcb5bbe288b02ee829a351262182cb67eb1a034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* 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

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);
}