Skip to content

Commit

Permalink
strchrnul is a GNU libc extension, add a wrapper for other platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
natoscott committed Oct 9, 2023
1 parent 2978af0 commit 4103c23
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
11 changes: 11 additions & 0 deletions XUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ void String_freeArray(char** s);

char* String_readLine(FILE* fd) ATTR_MALLOC;

static inline char* String_strchrnul(const char* s, int c) {
#ifdef HAVE_STRCHRNUL
return strchrnul(s, c);
#else
char* result = strchr(s, c);
if (result)
return result;
return strchr(s, '\0');
#endif
}

/* Always null-terminates dest. Caller must pass a strictly positive size. */
ATTR_ACCESS3_W(1, 3)
ATTR_ACCESS3_R(2, 3)
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ AC_CHECK_FUNCS([ \
readlinkat \
sched_getscheduler \
sched_setscheduler \
strchrnul \
])

if test "$my_htop_platform" = darwin; then
Expand Down
10 changes: 5 additions & 5 deletions linux/CGroupUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static bool CGroup_filterName_internal(const char* cgroup, StrBuf_state* s, StrB
}

const char* labelStart = cgroup;
const char* nextSlash = strchrnul(labelStart, '/');
const char* nextSlash = String_strchrnul(labelStart, '/');
const size_t labelLen = nextSlash - labelStart;

if (Label_checkEqual(labelStart, labelLen, str_system_slice)) {
Expand All @@ -104,7 +104,7 @@ static bool CGroup_filterName_internal(const char* cgroup, StrBuf_state* s, StrB
return false;

if (String_startsWith(cgroup, str_system_slice_prefix)) {
cgroup = strchrnul(cgroup + 1, '/');
cgroup = String_strchrnul(cgroup + 1, '/');
continue;
}

Expand All @@ -129,7 +129,7 @@ static bool CGroup_filterName_internal(const char* cgroup, StrBuf_state* s, StrB
if (!String_startsWith(cgroup, str_user_slice_prefix))
continue;

const char* userSliceSlash = strchrnul(cgroup + strlen(str_user_slice_prefix), '/');
const char* userSliceSlash = String_strchrnul(cgroup + strlen(str_user_slice_prefix), '/');
const char* sliceSpec = userSliceSlash - strlen(str_slice_suffix);

if (!String_startsWith(sliceSpec, str_slice_suffix))
Expand Down Expand Up @@ -212,7 +212,7 @@ static bool CGroup_filterName_internal(const char* cgroup, StrBuf_state* s, StrB
while (*labelStart == '/')
labelStart++;

nextSlash = strchrnul(labelStart, '/');
nextSlash = String_strchrnul(labelStart, '/');
if (nextSlash - labelStart > 0) {
if (!StrBuf_putsz(s, w, isMonitor ? "[LXC:" : "[lxc:"))
return false;
Expand Down Expand Up @@ -276,7 +276,7 @@ static bool CGroup_filterName_internal(const char* cgroup, StrBuf_state* s, StrB

continue;
} else if (Label_checkPrefix(labelStart, scopeNameLen, str_snap_scope_prefix)) {
const char* nextDot = strchrnul(labelStart + strlen(str_snap_scope_prefix), '.');
const char* nextDot = String_strchrnul(labelStart + strlen(str_snap_scope_prefix), '.');

if (!StrBuf_putsz(s, w, "!snap:"))
return false;
Expand Down
4 changes: 2 additions & 2 deletions linux/LinuxProcessTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -856,13 +856,13 @@ static void LinuxProcessTable_readCGroupFile(LinuxProcess* process, openat_arg_t

char* group = buffer;
for (size_t i = 0; i < 2; i++) {
group = strchrnul(group, ':');
group = String_strchrnul(group, ':');
if (!*group)
break;
group++;
}

char* eol = strchrnul(group, '\n');
char* eol = String_strchrnul(group, '\n');
*eol = '\0';

if (at != output) {
Expand Down

0 comments on commit 4103c23

Please sign in to comment.