1
0
mirror of git://git.suckless.org/sbase synced 2024-12-25 16:32:37 +00:00
sbase/basename.c

29 lines
577 B
C
Raw Normal View History

2011-05-23 01:36:34 +00:00
/* See LICENSE file for copyright and license details. */
2011-05-24 11:56:38 +00:00
#include <libgen.h>
2011-05-23 01:36:34 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2011-05-24 20:39:20 +00:00
#include <unistd.h>
2011-05-23 01:36:34 +00:00
#include "util.h"
int
main(int argc, char *argv[])
{
2011-05-24 11:56:38 +00:00
char *s;
size_t n;
2011-05-23 01:36:34 +00:00
2011-05-24 20:39:20 +00:00
if(getopt(argc, argv, "") != -1)
exit(EXIT_FAILURE);
if(optind == argc)
2011-05-23 01:36:34 +00:00
eprintf("usage: %s string [suffix]\n", argv[0]);
2011-05-24 20:39:20 +00:00
s = basename(argv[optind++]);
if(optind < argc && strlen(s) > strlen(argv[optind])) {
n = strlen(s) - strlen(argv[optind]);
if(!strcmp(&s[n], argv[optind]))
2011-05-24 11:56:38 +00:00
s[n] = '\0';
2011-05-23 01:36:34 +00:00
}
2011-05-24 11:56:38 +00:00
puts(s);
2011-05-23 01:36:34 +00:00
return EXIT_SUCCESS;
}