sbase/basename.c

45 lines
610 B
C
Raw Normal View History

2011-05-22 21:36:34 -04:00
/* See LICENSE file for copyright and license details. */
2011-05-24 07:56:38 -04:00
#include <libgen.h>
2011-05-22 21:36:34 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2011-05-24 16:39:20 -04:00
#include <unistd.h>
2012-04-23 09:50:47 -04:00
#include <stdbool.h>
#include "arg.h"
2011-05-22 21:36:34 -04:00
#include "util.h"
2012-04-23 09:50:47 -04:00
char *argv0;
void
usage(void)
{
2012-04-23 10:32:41 -04:00
eprintf("usage: %s name [suffix]\n", basename(argv0));
2012-04-23 09:50:47 -04:00
}
2011-05-22 21:36:34 -04:00
int
main(int argc, char *argv[])
{
2012-04-23 10:32:41 -04:00
char *s;
size_t n;
2012-04-23 09:50:47 -04:00
ARGBEGIN {
default:
usage();
} ARGEND;
if (argc < 1)
usage();
2012-04-23 10:32:41 -04:00
s = basename(argv[0]);
if (argc == 2 && argv[1]) {
n = strlen(s) - strlen(argv[1]);
if (!strcmp(&s[n], argv[1]))
2012-04-23 10:32:41 -04:00
s[n] = '\0';
2011-05-22 21:36:34 -04:00
}
2012-04-23 10:32:41 -04:00
puts(s);
2012-04-23 09:50:47 -04:00
2011-05-22 21:36:34 -04:00
return EXIT_SUCCESS;
}
2012-04-23 09:50:47 -04:00