bore

basic core utilities (PD)
git clone git://bvnf.space/bore.git
Log | Files | Refs | README

commit bdc41a7cab8a0a5b7910a9008fb486fee81b687d
parent 72b1e3bf54006965fd53dc800515465048f48836
Author: phoebos <ben@bvnf.space>
Date:   Thu,  3 Nov 2022 13:17:34 +0000

basename: add

Diffstat:
M.gitignore | 1+
MMakefile | 1+
MPROGRESS | 1+
MTODO.posix | 2+-
Abasename.c | 25+++++++++++++++++++++++++
5 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ +basename cat echo ed diff --git a/Makefile b/Makefile @@ -1,6 +1,7 @@ .POSIX: BINS = \ + basename \ cat \ echo \ ed \ diff --git a/PROGRESS b/PROGRESS @@ -2,6 +2,7 @@ # Any additional commands come afterwards. # An [x] in the box indicates an essentially completed program. +[x] basename [x] cat [-u] [ ] chmod [ ] chown diff --git a/TODO.posix b/TODO.posix @@ -2,7 +2,7 @@ [ ] ar [ ] asa [ ] awk -[ ] basename +[x] basename [ ] bc [ ] c99 [ ] cal diff --git a/basename.c b/basename.c @@ -0,0 +1,25 @@ +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int +main(int argc, char **argv) { + char *base, *suffix; + + // TODO: handle `basename -- string` + if (argc < 2 || argc > 3) { + fprintf(stderr, "usage: %s string [suffix]\n", argv[0]); + return 1; + } + + base = basename(argv[1]); + if (argc == 3) { + suffix = strstr(base, argv[2]); + if (suffix != NULL) + *suffix = '\0'; + } + + printf("%s\n", base); + return 0; +}