Skip to content

Commit

Permalink
EXODUS: Improve portability
Browse files Browse the repository at this point in the history
  • Loading branch information
gdsjaar committed May 15, 2024
1 parent 1d9e701 commit f8178bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/seacas/libraries/exodus/src/ex_field_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@

#define SIZE(X) sizeof(X) / sizeof(X[0])

char *my_strdup(const char *s)
{
size_t slen = strlen(s);
char *result = malloc(slen + 1);
if (result == NULL) {
return NULL;
}

memcpy(result, s, slen + 1);
return result;
}

char *my_strsep(char **stringp, const char *delim)
{
char *rv = *stringp;
Expand All @@ -29,7 +41,7 @@ char *my_strsep(char **stringp, const char *delim)
size_t my_strlcat(char *restrict dst, const char *restrict src, size_t maxlen)
{
const size_t srclen = strlen(src);
const size_t dstlen = strnlen(dst, maxlen);
const size_t dstlen = strlen(dst);
if (dstlen == maxlen)
return maxlen + srclen;
if (srclen < maxlen - dstlen) {
Expand Down Expand Up @@ -63,7 +75,7 @@ const char *ex_component_field_name(ex_field *field, int component[EX_MAX_FIELD_
// For thread-safety, it is up to calling code.

// Return the name of the field corresponding to the specified 1-based component(s)
static char field_name[EX_MAX_NAME];
static char field_name[EX_MAX_NAME + 1];
const char *suffices[EX_MAX_FIELD_NESTING] = {NULL};
for (int i = 0; i < field->nesting; i++) {
suffices[i] = ex_field_component_suffix(field, i, component[i]);
Expand Down Expand Up @@ -303,7 +315,7 @@ const char *ex_field_component_suffix(ex_field *field, int nest_level, int compo
case EX_FIELD_TYPE_USER_DEFINED: {
if (field->suffices[0] != '\0') {
// `user_suffices` is a comma-separated string. Assume component is valid.
char *string = strdup(field->suffices);
char *string = my_strdup(field->suffices);
char *tofree = string;
char *token = my_strsep(&string, ",");
for (int i = 0; i < component - 1; i++) {
Expand Down
3 changes: 3 additions & 0 deletions packages/seacas/libraries/exodus/test/testrd-field-metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

char *my_strsep(char **stringp, const char *delim)
{
assert(delim != NULL);
char *rv = *stringp;
if (rv) {
assert(stringp != NULL);
assert(*stringp != NULL);
*stringp += strcspn(*stringp, delim);
if (**stringp)
*(*stringp)++ = '\0';
Expand Down

0 comments on commit f8178bd

Please sign in to comment.