sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

basename.c (549B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <libgen.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 
      6 #include "util.h"
      7 
      8 static void
      9 usage(void)
     10 {
     11 	eprintf("usage: %s path [suffix]\n", argv0);
     12 }
     13 
     14 int
     15 main(int argc, char *argv[])
     16 {
     17 	ssize_t off;
     18 	char *p;
     19 
     20 	ARGBEGIN {
     21 	default:
     22 		usage();
     23 	} ARGEND
     24 
     25 	if (argc != 1 && argc != 2)
     26 		usage();
     27 
     28 	p = basename(argv[0]);
     29 	if (argc == 2) {
     30 		off = strlen(p) - strlen(argv[1]);
     31 		if (off > 0 && !strcmp(p + off, argv[1]))
     32 			p[off] = '\0';
     33 	}
     34 	puts(p);
     35 
     36 	return fshut(stdout, "<stdout>");
     37 }