libroot: Replace asctime[_r] with musl's.

This broke anyway in hrev56361.

Change-Id: I1e7e02b27d6fee4845c81cf6f229bca0048a0e61
This commit is contained in:
Jessica Hamilton 2022-08-19 09:49:49 +00:00 committed by Augustin Cavalier
parent b09c82377a
commit 16c85099f9
5 changed files with 40 additions and 60 deletions

View File

@ -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

View File

@ -0,0 +1,7 @@
#include <time.h>
char *asctime(const struct tm *tm)
{
static char buf[26];
return asctime_r(tm, buf);
}

View File

@ -0,0 +1,30 @@
#include <time.h>
#include <stdio.h>
#include <langinfo.h>
#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);

View File

@ -19,7 +19,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
UsePrivateSystemHeaders ;
MergeObject <$(architecture)>posix_time.o :
asctime.cpp
clock.cpp
clock_support.cpp
ctime.c

View File

@ -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 <time.h>
#include <stdio.h>
#include <errno_private.h>
#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
}