Skip to content

Commit

Permalink
Avoid using a wrapper for strndup.
Browse files Browse the repository at this point in the history
  • Loading branch information
FtZPetruska committed Jun 8, 2023
1 parent a9221c0 commit f37a0c2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
15 changes: 5 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ int main() {
}"
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 @@ -72,12 +63,16 @@ 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 dupstring.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 strndup.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)
add_executable(vita-elf-export vita-elf-export.c yamlemitter.c)

if(HAVE_STRNDUP)
target_compile_definitions(vita-elf-create PRIVATE "HAVE_STRNDUP")
endif()

target_link_libraries(vita-yaml ${libyaml_LIBRARIES})
target_link_libraries(vita-import vita-yaml)
target_link_libraries(vita-export vita-yaml)
Expand Down
9 changes: 0 additions & 9 deletions src/dupstring.h

This file was deleted.

6 changes: 4 additions & 2 deletions src/elf-create-argp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <string.h>
#include <unistd.h>

#include "dupstring.h"
#ifndef HAVE_STRNDUP
#include "strndup.h"
#endif

int parse_arguments(int argc, char *argv[], elf_create_args *arguments)
{
Expand Down Expand Up @@ -78,7 +80,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] = dupstring(entrypoint, entrypoint_length);
arguments->entrypoint_funcs[i] = strndup(entrypoint, entrypoint_length);

i++;
entrypoint = entrypoint_end + 1;
Expand Down
15 changes: 4 additions & 11 deletions src/dupstring.c → src/strndup.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#include "dupstring.h"
#ifndef HAVE_STRNDUP
#include "strndup.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>
#include <string.h>

char *dupstring(const char *source, size_t maxlen) {
char *strndup(const char *source, size_t maxlen) {
size_t dest_size = strnlen(source, maxlen);
char *dest = malloc(dest_size + 1);
if (dest == NULL) {
Expand Down
8 changes: 8 additions & 0 deletions src/strndup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef STRNDUP_H
#define STRNDUP_H

#include <stddef.h>

char *strndup(const char *source, size_t maxlen);

#endif /* STRNDUP_H */

0 comments on commit f37a0c2

Please sign in to comment.