Skip to content

Commit 29692c1

Browse files
authored
[libc] Implement basename and dirname in libgen.h (#204554)
Added the POSIX standard functions basename and dirname under a new libgen.h header. The implementations modify the input path in-place using cpp::string_view to determine boundaries safely. Added find_last_not_of to cpp::string_view to support trailing slash removal. Implemented: * libc/include/libgen.yaml, libgen.h.def: Public API definitions. * libc/src/libgen/basename.cpp, dirname.cpp: Generic implementations. * libc/test/src/libgen/: Unit and hermetic tests. Registered the new entrypoints for all active Linux targets (x86_64, aarch64, arm, riscv) and added docgen configuration. Assisted-by: Automated tooling, human reviewed.
1 parent 22dce64 commit 29692c1

22 files changed

Lines changed: 458 additions & 0 deletions

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ set(TARGET_LIBC_ENTRYPOINTS
109109
libc.src.inttypes.strtoimax
110110
libc.src.inttypes.strtoumax
111111

112+
# libgen.h entrypoints
113+
libc.src.libgen.basename
114+
libc.src.libgen.dirname
115+
112116
# stdbit.h entrypoints
113117
libc.src.stdbit.stdc_bit_ceil_uc
114118
libc.src.stdbit.stdc_bit_ceil_ui

libc/config/linux/arm/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ set(TARGET_LIBC_ENTRYPOINTS
7373
libc.src.inttypes.strtoimax
7474
libc.src.inttypes.strtoumax
7575

76+
# libgen.h entrypoints
77+
libc.src.libgen.basename
78+
libc.src.libgen.dirname
79+
7680
# stdbit.h entrypoints
7781
libc.src.stdbit.stdc_bit_ceil_uc
7882
libc.src.stdbit.stdc_bit_ceil_ui

libc/config/linux/riscv/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ set(TARGET_LIBC_ENTRYPOINTS
109109
libc.src.inttypes.strtoimax
110110
libc.src.inttypes.strtoumax
111111

112+
# libgen.h entrypoints
113+
libc.src.libgen.basename
114+
libc.src.libgen.dirname
115+
112116
# stdbit.h entrypoints
113117
libc.src.stdbit.stdc_bit_ceil_uc
114118
libc.src.stdbit.stdc_bit_ceil_ui

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ set(TARGET_LIBC_ENTRYPOINTS
125125
libc.src.inttypes.wcstoimax
126126
libc.src.inttypes.wcstoumax
127127

128+
# libgen.h entrypoints
129+
libc.src.libgen.basename
130+
libc.src.libgen.dirname
131+
128132
# stdbit.h entrypoints
129133
libc.src.stdbit.stdc_bit_ceil_uc
130134
libc.src.stdbit.stdc_bit_ceil_ui

libc/docs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ if (SPHINX_FOUND)
5454
float
5555
glob
5656
inttypes
57+
libgen
5758
locale
5859
nl_types
5960
net/if

libc/docs/headers/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Implementation Status
2020
float
2121
glob
2222
inttypes
23+
libgen
2324
locale
2425
math/index.rst
2526
net/if

libc/include/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ add_header_macro(
130130
.llvm-libc-macros.float_macros
131131
)
132132

133+
add_header_macro(
134+
libgen
135+
../libc/include/libgen.yaml
136+
libgen.h
137+
DEPENDS
138+
.llvm_libc_common_h
139+
)
140+
133141
add_header_macro(
134142
limits
135143
../libc/include/limits.yaml

libc/include/libgen.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
header: libgen.h
2+
standards:
3+
- posix
4+
macros: []
5+
types: []
6+
enums: []
7+
objects: []
8+
functions:
9+
- name: basename
10+
standards:
11+
- posix
12+
return_type: char *
13+
arguments:
14+
- type: char *
15+
- name: dirname
16+
standards:
17+
- posix
18+
return_type: char *
19+
arguments:
20+
- type: char *

libc/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_subdirectory(dlfcn)
77
add_subdirectory(errno)
88
add_subdirectory(fenv)
99
add_subdirectory(inttypes)
10+
add_subdirectory(libgen)
1011
add_subdirectory(link)
1112
add_subdirectory(math)
1213
add_subdirectory(netinet)

libc/src/__support/CPP/string_view.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ class string_view {
205205
return npos;
206206
}
207207

208+
LIBC_INLINE constexpr size_t find_last_not_of(const char c,
209+
size_t end = npos) const {
210+
end = end >= size() ? size() : end + 1;
211+
for (; end > 0; --end)
212+
if ((*this)[end - 1] != c)
213+
return end - 1;
214+
return npos;
215+
}
216+
208217
// Finds the first character not equal to c in this view, starting at
209218
// position From.
210219
LIBC_INLINE constexpr size_t find_first_not_of(const char c,

0 commit comments

Comments
 (0)