From 16c85099f944a698429ac48a8d22f9680525f136 Mon Sep 17 00:00:00 2001 From: Jessica Hamilton Date: Fri, 19 Aug 2022 09:49:49 +0000 Subject: [PATCH] libroot: Replace asctime[_r] with musl's. This broke anyway in hrev56361. Change-Id: I1e7e02b27d6fee4845c81cf6f229bca0048a0e61 --- src/system/libroot/posix/musl/time/Jamfile | 3 + src/system/libroot/posix/musl/time/asctime.c | 7 +++ .../libroot/posix/musl/time/asctime_r.c | 30 ++++++++++ src/system/libroot/posix/time/Jamfile | 1 - src/system/libroot/posix/time/asctime.cpp | 59 ------------------- 5 files changed, 40 insertions(+), 60 deletions(-) create mode 100644 src/system/libroot/posix/musl/time/asctime.c create mode 100644 src/system/libroot/posix/musl/time/asctime_r.c delete mode 100644 src/system/libroot/posix/time/asctime.cpp diff --git a/src/system/libroot/posix/musl/time/Jamfile b/src/system/libroot/posix/musl/time/Jamfile index b8d967a4bc..23ca1f65ac 100644 --- a/src/system/libroot/posix/musl/time/Jamfile +++ b/src/system/libroot/posix/musl/time/Jamfile @@ -2,6 +2,7 @@ SubDir HAIKU_TOP src system libroot posix musl time ; SubDirSysHdrs [ FDirName $(SUBDIR) .. include ] ; UseHeaders [ FDirName $(SUBDIR) .. internal ] ; +UseHeaders [ FDirName $(SUBDIR) .. arch $(TARGET_ARCH) ] ; local architectureObject ; for architectureObject in [ MultiArchSubDirSetup ] { @@ -9,6 +10,8 @@ for architectureObject in [ MultiArchSubDirSetup ] { local architecture = $(TARGET_PACKAGING_ARCH) ; MergeObject <$(architecture)>posix_musl_time.o : + asctime.c + asctime_r.c difftime.c strftime.c strptime.c diff --git a/src/system/libroot/posix/musl/time/asctime.c b/src/system/libroot/posix/musl/time/asctime.c new file mode 100644 index 0000000000..0a7ac91790 --- /dev/null +++ b/src/system/libroot/posix/musl/time/asctime.c @@ -0,0 +1,7 @@ +#include + +char *asctime(const struct tm *tm) +{ + static char buf[26]; + return asctime_r(tm, buf); +} diff --git a/src/system/libroot/posix/musl/time/asctime_r.c b/src/system/libroot/posix/musl/time/asctime_r.c new file mode 100644 index 0000000000..5414c20652 --- /dev/null +++ b/src/system/libroot/posix/musl/time/asctime_r.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include "locale_impl.h" +#include "time_impl.h" +#include "atomic.h" + +char *__asctime_r(const struct tm *restrict tm, char *restrict buf) +{ + if (snprintf(buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", + __nl_langinfo_l(ABDAY_1+tm->tm_wday, C_LOCALE), + __nl_langinfo_l(ABMON_1+tm->tm_mon, C_LOCALE), + tm->tm_mday, tm->tm_hour, + tm->tm_min, tm->tm_sec, + 1900 + tm->tm_year) >= 26) + { + /* ISO C requires us to use the above format string, + * even if it will not fit in the buffer. Thus asctime_r + * is _supposed_ to crash if the fields in tm are too large. + * We follow this behavior and crash "gracefully" to warn + * application developers that they may not be so lucky + * on other implementations (e.g. stack smashing..). + */ + a_crash(); + } + return buf; +} + +weak_alias(__asctime_r, asctime_r); + diff --git a/src/system/libroot/posix/time/Jamfile b/src/system/libroot/posix/time/Jamfile index 0b2f9312d4..ae70c1a9c7 100644 --- a/src/system/libroot/posix/time/Jamfile +++ b/src/system/libroot/posix/time/Jamfile @@ -19,7 +19,6 @@ for architectureObject in [ MultiArchSubDirSetup ] { UsePrivateSystemHeaders ; MergeObject <$(architecture)>posix_time.o : - asctime.cpp clock.cpp clock_support.cpp ctime.c diff --git a/src/system/libroot/posix/time/asctime.cpp b/src/system/libroot/posix/time/asctime.cpp deleted file mode 100644 index c4c8ce69a4..0000000000 --- a/src/system/libroot/posix/time/asctime.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2004-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved. - * Copyright 2010, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved. - * Distributed under the terms of the MIT License. - */ - - -#include -#include - -#include - -#include "PosixLCTimeInfo.h" - - -using BPrivate::Libroot::gPosixLCTimeInfo; - - -static char* -print_time(char* buffer, size_t bufferSize, const struct tm* tm) -{ - snprintf(buffer, bufferSize, "%.3s %.3s%3d %02d:%02d:%02d %d\n", - tm->tm_wday < 0 ? "???" : gPosixLCTimeInfo.wday[tm->tm_wday % 7], - tm->tm_mon < 0 ? "???" : gPosixLCTimeInfo.mon[tm->tm_mon % 12], - tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, - 1900 + tm->tm_year); - - return buffer; -} - - -extern "C" char* -asctime(const struct tm* tm) -{ - if (tm == NULL) { - __set_errno(EINVAL); - return NULL; - } - - static char buffer[26]; - // That's enough to hold normal dates (i.e. with 4-digit years), for any - // other dates the behaviour of asctime() is undefined according to the - // POSIX Base Specifications Issue 7. - - return print_time(buffer, sizeof(buffer), tm); -} - - -extern "C" char* -asctime_r(const struct tm* tm, char* buffer) -{ - if (tm == NULL) { - __set_errno(EINVAL); - return NULL; - } - - return print_time(buffer, 26, tm); - // 26 bytes seems to be required by the standard, so we can't write more -}