sbase/basename.c
Laslo Hunhold e92a1aef54 Handle { NULL } argv[] properly in manual arg-reduction
Thanks izabera for reporting this!
2017-08-05 23:50:39 +02:00

35 lines
560 B
C

/* See LICENSE file for copyright and license details. */
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s path [suffix]\n", argv0);
}
int
main(int argc, char *argv[])
{
ssize_t off;
char *p;
argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
if (argc != 1 && argc != 2)
usage();
p = basename(argv[0]);
if (argc == 2) {
off = strlen(p) - strlen(argv[1]);
if (off > 0 && !strcmp(p + off, argv[1]))
p[off] = '\0';
}
puts(p);
return fshut(stdout, "<stdout>");
}