38 lines
1.1 KiB
C
38 lines
1.1 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#include <stdio.h>
|
|
#include "format-internal.h"
|
|
|
|
// Keep the stream dependency in a separate archive member so snprintf-only
|
|
// programs can link without a Disk BASIC runtime.
|
|
static int storeFile(void *context, const char *source, char fill, unsigned count)
|
|
{
|
|
FILE *stream = (FILE *) context;
|
|
if (source)
|
|
return fwrite(source, 1, count, stream) == count ? 0 : -1;
|
|
char padding[32];
|
|
unsigned chunk = count < sizeof(padding) ? count : sizeof(padding);
|
|
memset(padding, fill, chunk);
|
|
while (count) {
|
|
if (chunk > count) chunk = count;
|
|
if (fwrite(padding, 1, chunk, stream) != chunk) return -1;
|
|
count -= chunk;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int vfprintf(FILE *stream, const char *format, va_list args)
|
|
{
|
|
// Check stream direction and pending errors even for an empty format.
|
|
if (fputs("", stream) == EOF) return -1;
|
|
return vcc_format_to(storeFile, stream, format, args);
|
|
}
|
|
|
|
int fprintf(FILE *stream, const char *format, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, format);
|
|
int result = vfprintf(stream, format, args);
|
|
va_end(args);
|
|
return result;
|
|
}
|