morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base.git
Log | Files | Refs

basename.c (623B)


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