Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some cross-platform portability changes #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ include(CheckFunctionExists)
include(GNUInstallDirs)

# Check if system has strtonum
check_function_exists(strndup HAVE_STRNDUP)
check_function_exists(strtonum HAVE_STRTONUM)
check_function_exists(reallocarray HAVE_REALLOCARRAY)

Expand All @@ -29,6 +30,10 @@ include_directories(${GD_INCLUDE_DIRS})
set(SRC src/clean.c src/drawchar.c src/fonts.c src/error.c src/loadfile.c src/init.c src/output.c src/savefile.c)
set(LOADERS src/loaders/ansi.c src/loaders/artworx.c src/loaders/binary.c src/loaders/icedraw.c src/loaders/pcboard.c src/loaders/tundra.c src/loaders/xbin.c)

if(NOT HAVE_STRNDUP)
set(SRC ${SRC} compat/strndup.c)
endif()

if(NOT HAVE_STRTONUM)
set(SRC ${SRC} compat/strtonum.c)
endif()
Expand Down
22 changes: 22 additions & 0 deletions compat/strndup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2024 Martin J. Fiedler <[email protected]>
// SPDX-License-Identifier: MIT

#include <stdio.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "strndup.h"

char *strndup(const char *str, size_t size) {
char *buffer;
size_t srclen = strlen(str);
if (srclen < size) { size = srclen; }
buffer = (char*) malloc(size + 1);
if (buffer) {
strncpy(buffer, str, size);
}
buffer[size] = '\0';
return buffer;
}
1 change: 1 addition & 0 deletions compat/strndup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
char *strndup(const char *str, size_t size);
2 changes: 2 additions & 0 deletions include/ansilove.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
extern "C" {
#endif

#ifndef ANSILOVE_EXTERN
#define ANSILOVE_EXTERN __attribute__((visibility("default")))
#endif

/* Version number */
#define ANSILOVE_VERSION "1.4.1"
Expand Down
4 changes: 4 additions & 0 deletions src/loaders/ansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include "fonts.h"
#include "output.h"

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

#ifndef HAVE_STRTONUM
#include "strtonum.h"
#endif
Expand Down