SameBoy/Windows/stdio.h

92 lines
1.7 KiB
C
Raw Permalink Normal View History

2016-08-20 14:51:17 +00:00
#pragma once
#include_next <stdio.h>
#include <stdlib.h>
2022-05-21 15:18:34 +00:00
#include <stdarg.h>
2016-08-20 14:51:17 +00:00
int access(const char *filename, int mode);
#define R_OK 2
#define W_OK 4
#ifndef __MINGW32__
2017-04-25 01:50:35 +00:00
#ifndef __LIBRETRO__
2016-08-20 14:51:17 +00:00
static inline int vasprintf(char **str, const char *fmt, va_list args)
{
size_t size = _vscprintf(fmt, args) + 1;
2021-06-06 02:04:47 +00:00
*str = (char*)malloc(size);
2016-08-20 14:51:17 +00:00
int ret = vsprintf(*str, fmt, args);
if (ret != size - 1) {
free(*str);
*str = NULL;
return -1;
}
return ret;
}
2022-05-21 15:18:34 +00:00
static inline int asprintf(char **strp, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
int r = vasprintf(strp, fmt, args);
va_end(args);
return r;
}
2017-04-25 01:50:35 +00:00
#endif
#endif
2016-08-20 14:51:17 +00:00
/* This code is public domain -- Will Hartung 4/9/09 */
2021-06-06 02:04:47 +00:00
static inline size_t getline(char **lineptr, size_t *n, FILE *stream)
2020-04-24 17:37:57 +00:00
{
2016-08-20 14:51:17 +00:00
char *bufptr = NULL;
char *p = bufptr;
size_t size;
int c;
if (lineptr == NULL) {
return -1;
}
if (stream == NULL) {
return -1;
}
if (n == NULL) {
return -1;
}
bufptr = *lineptr;
size = *n;
c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (bufptr == NULL) {
2021-06-06 02:04:47 +00:00
bufptr = (char*)malloc(128);
2016-08-20 14:51:17 +00:00
if (bufptr == NULL) {
return -1;
}
size = 128;
}
p = bufptr;
while (c != EOF) {
if ((p - bufptr) > (size - 1)) {
size = size + 128;
2021-06-06 02:04:47 +00:00
bufptr = (char*)realloc(bufptr, size);
2016-08-20 14:51:17 +00:00
if (bufptr == NULL) {
return -1;
}
}
*p++ = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}
*p++ = '\0';
*lineptr = bufptr;
*n = size;
return p - bufptr - 1;
2017-05-24 18:44:43 +00:00
}
#define snprintf _snprintf