implement the gnu asprintf/vasprintf extensions.

This commit is contained in:
Kalamatee 2023-04-10 23:11:16 +01:00 committed by deadwood
parent aff6c19e3b
commit 8ee9cf0833
5 changed files with 150 additions and 1 deletions

60
compiler/stdc/asprintf.c Normal file
View File

@ -0,0 +1,60 @@
/*
Copyright (C) 2023, The AROS Development Team. All rights reserved.
GNU extension asprintf().
*/
#include <libraries/stdcio.h>
#include <aros/debug.h>
#if DEBUG
#include <aros/libcall.h>
#endif
#include "debug.h"
#define _GNU_SOURCE
/*****************************************************************************
NAME */
#include <stdio.h>
#include <stdarg.h>
int asprintf (
/* SYNOPSIS */
char **restrict str, const char *restrict format, ...)
/* FUNCTION
Analog of sprintf, except that sotrage is allocated for a string
large enough to hold the output including the terminating null
byte
INPUTS
str - Where to store the pointer for the allocated string.
format - A printf() format string.
... - Arguments for the format string
RESULT
The number of characters written, or EOF on error.
NOTES
EXAMPLE
BUGS
SEE ALSO
vasprintf(), sprintf(), vsprintf()
INTERNALS
******************************************************************************/
{
va_list args;
int size = 0;
va_start(args, format);
size = vasprintf(str, format, args);
va_end(args);
return size;
}

View File

@ -91,6 +91,9 @@ int fprintf(FILE * restrict stream, const char * restrict format, ...);
int fscanf(FILE * restrict stream, const char * restrict format, ...);
int printf(const char * restrict format, ...);
int scanf(const char * restrict format, ...);
#if defined(_GNU_SOURCE)
int asprintf(char ** restrict str, const char * restrict format, ...);
#endif
int snprintf(char * restrict s, size_t n, const char * restrict format, ...);
int sprintf(char * restrict s, const char * restrict format, ...);
int sscanf(const char * restrict s, const char * restrict format, ...);
@ -100,6 +103,10 @@ int vfscanf(FILE * restrict stream, const char * restrict format,
va_list arg);
int vprintf(const char * restrict format, va_list arg);
int vscanf(const char * restrict format, va_list arg);
#if defined(_GNU_SOURCE)
int vasprintf(char **restrict str, const char *restrict format,
va_list args);
#endif
int vsnprintf(char * restrict s, size_t n, const char * restrict format,
va_list arg);
int vsprintf(char * restrict s, const char * restrict format,

View File

@ -366,6 +366,7 @@ STDC := \
abort \
abs labs llabs \
asctime asctime_r \
asprintf \
atexit \
atof \
atoi \
@ -461,6 +462,7 @@ STDC := \
strupr \
strxfrm \
time \
vasprintf \
vsnprintf \
vsprintf \
vsscanf
@ -585,6 +587,7 @@ STDC_STATIC := \
__vcformat \
__vcscan \
abs labs llabs \
asprintf \
atoi \
atol \
atoll \
@ -633,6 +636,7 @@ STDC_STATIC := \
strtof \
strtol \
strtoul \
vasprintf \
vsnprintf \
vsprintf \
vsscanf

View File

@ -1,5 +1,5 @@
##begin config
version 0.15
version 0.16
basename StdC
libbasetypeextern struct StdCBase
libbasetype struct StdCIntBase
@ -637,6 +637,9 @@ char *asctime_r(const struct tm *, char *)
char *ctime_r(const time_t *, char *)
struct tm *gmtime_r(const time_t *, struct tm *)
struct tm *localtime_r(const time_t *, struct tm *)
.skip 1
int asprintf(char **restrict str, const char *restrict format, ...)
int vasprintf(char * restrict s, const char * restrict format, ...)
#
# * wchar.h: mostly unimplemented in stdc.library
#

75
compiler/stdc/vasprintf.c Normal file
View File

@ -0,0 +1,75 @@
/*
Copyright (C) 2023, The AROS Development Team. All rights reserved.
GNU function vasprintf().
*/
#include <libraries/stdcio.h>
#include <aros/debug.h>
#if DEBUG
#include <aros/libcall.h>
#endif
#include "debug.h"
/*****************************************************************************
NAME */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
int vasprintf (
/* SYNOPSIS */
char **restrict str, const char *restrict format, va_list args)
/* FUNCTION
Analog of vsprintf, except that sotrage is allocated for a string
large enough to hold the output including the terminating null
byte
INPUTS
str - Where to store the pointer for the allocated string.
format - A printf() format string.
args - A list of arguments for the format string.
RESULT
The number of characters written, or EOF on error.
NOTES
EXAMPLE
BUGS
SEE ALSO
asprintf(), sprintf(), vsprintf()
INTERNALS
******************************************************************************/
{
va_list argtmp;
int size = 0;
va_copy(argtmp, args);
size = vsnprintf(NULL, 0, format, argtmp);
va_end(argtmp);
if (size < 0)
{
return -1;
}
*str = (char *) malloc(size + 1);
if (!*str)
{
return -1;
}
size = vsprintf(*str, format, args);
return size;
}