sbase/basename.c

43 lines
541 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>
2013-03-05 20:35:55 +00:00
2011-05-23 01:36:34 +00:00
#include "util.h"
2012-05-15 12:32:56 +00:00
static void usage(void);
2012-04-23 13:50:47 +00:00
2013-03-05 20:35:55 +00:00
void
usage(void)
{
eprintf("usage: %s name [suffix]\n", argv0);
}
2011-05-23 01:36:34 +00:00
int
main(int argc, char *argv[])
{
2012-04-23 14:32:41 +00:00
char *s;
size_t n;
2012-04-23 13:50:47 +00:00
ARGBEGIN {
default:
2012-05-15 12:32:56 +00:00
usage();
2012-04-23 13:50:47 +00:00
} ARGEND;
2012-05-14 20:28:41 +00:00
if(argc < 1)
2012-05-15 12:32:56 +00:00
usage();
2012-04-23 13:50:47 +00:00
2012-04-23 14:32:41 +00:00
s = basename(argv[0]);
2012-05-14 20:28:41 +00:00
if(argc == 2 && argv[1]) {
n = strlen(s) - strlen(argv[1]);
2012-05-14 20:28:41 +00:00
if(!strcmp(&s[n], argv[1]))
2012-04-23 14:32:41 +00:00
s[n] = '\0';
2011-05-23 01:36:34 +00:00
}
2013-03-05 20:35:55 +00:00
2012-04-23 14:32:41 +00:00
puts(s);
2012-04-23 13:50:47 +00:00
2013-03-05 20:35:55 +00:00
return 0;
2011-05-23 01:36:34 +00:00
}
2012-05-15 12:32:56 +00:00