userspace: Implement `dirname` utility

This commit is contained in:
Bananymous 2024-12-05 08:30:30 +02:00
parent 097ab82529
commit cb7d5c9d09
3 changed files with 27 additions and 0 deletions

View File

@ -7,6 +7,7 @@ set(USERSPACE_PROGRAMS
cp cp
dd dd
dhcp-client dhcp-client
dirname
DynamicLoader DynamicLoader
echo echo
env env

View File

@ -0,0 +1,8 @@
set(SOURCES
main.cpp
)
add_executable(dirname ${SOURCES})
banan_link_library(dirname libc)
install(TARGETS dirname OPTIONAL)

View File

@ -0,0 +1,18 @@
#include <libgen.h>
#include <stdio.h>
int usage(const char* argv0, int ret)
{
FILE* fout = ret ? stderr : stdout;
fprintf(fout, "usage: %s STRING\n", argv0);
return ret;
}
int main(int argc, const char* argv[])
{
if (argc != 2)
return usage(argv[0], 1);
const char* result = dirname(const_cast<char*>(argv[1]));
printf("%s\n", result);
return 0;
}