Skip to content

Commit

Permalink
Add a strndup fallback implementation for Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
FtZPetruska committed Jun 7, 2023
1 parent cad6d32 commit 0bfe10c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ else()
set(runtime_destination lib)
endif()

# Check if strndup is available, Windows toolchains typically don't provide it
include(CheckCSourceCompiles)
check_c_source_compiles("
#include <string.h>
#include <stdlib.h>
int main() {
free(strndup(\"foo\", 4));
}"
HAVE_STRNDUP)

if(HAVE_STRNDUP)
set(STRNDUP_DEF "HAVE_STRNDUP=1")
else()
set(STRNDUP_DEF "HAVE_STRNDUP=0")
endif()

set_source_files_properties(dupstring.c PROPERTIES
COMPILE_DEFINITIONS ${STRNDUP_DEF})

add_library(vita-yaml yamltree.c yamltreeutil.c)
add_library(vita-export vita-export-parse.c sha256.c)
add_library(vita-import vita-import.c vita-import-parse.c)
Expand All @@ -53,7 +72,7 @@ target_include_directories(vita-import PUBLIC
$<INSTALL_INTERFACE:include>)

add_executable(vita-libs-gen vita-libs-gen.c)
add_executable(vita-elf-create vita-elf-create.c elf-create-argp.c vita-elf.c elf-defs.c sce-elf.c varray.c elf-utils.c yamlemitter.c)
add_executable(vita-elf-create vita-elf-create.c elf-create-argp.c vita-elf.c elf-defs.c sce-elf.c varray.c elf-utils.c yamlemitter.c dupstring.c)
add_executable(vita-mksfoex vita-mksfoex.c getopt_long.c)
add_executable(vita-make-fself vita-make-fself.c)
add_executable(vita-pack-vpk vita-pack-vpk.c)
Expand Down
24 changes: 24 additions & 0 deletions src/dupstring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "dupstring.h"

#include <string.h>

#if defined(HAVE_STRNDUP) && HAVE_STRNDUP

char *dupstring(const char *source, size_t maxlen) {
return strndup(source, maxlen);
}

#else /* Fallback implementation */
#include <stdlib.h>

char *dupstring(const char *source, size_t maxlen) {
size_t dest_size = strnlen(source, maxlen);
char *dest = malloc(dest_size + 1);
if (dest == NULL) {
return NULL;
}
memcmp(dest, source, dest_size);
dest[dest_size] = '\0';
return dest;
}
#endif
9 changes: 9 additions & 0 deletions src/dupstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef DUPSTRING_H
#define DUPSTRING_H

#include <stddef.h>

/* Wrapper around strndup */
char *dupstring(const char *source, size_t maxlen);

#endif /* DUPSTRING_H */
4 changes: 3 additions & 1 deletion src/elf-create-argp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <string.h>
#include <unistd.h>

#include "dupstring.h"

int parse_arguments(int argc, char *argv[], elf_create_args *arguments)
{
int c;
Expand Down Expand Up @@ -76,7 +78,7 @@ int parse_arguments(int argc, char *argv[], elf_create_args *arguments)
}
size_t entrypoint_length = (size_t)(entrypoint_end - entrypoint);
if (entrypoint_length > 0)
arguments->entrypoint_funcs[i] = strndup(entrypoint, entrypoint_length);
arguments->entrypoint_funcs[i] = dupstring(entrypoint, entrypoint_length);

i++;
entrypoint = entrypoint_end + 1;
Expand Down

0 comments on commit 0bfe10c

Please sign in to comment.