2018-10-31 01:09:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2018-10-31 16:50:43 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2018-11-11 09:11:09 +00:00
|
|
|
#include <unistd.h>
|
2018-11-17 14:56:09 +00:00
|
|
|
#include <fcntl.h>
|
2018-10-27 14:43:03 +00:00
|
|
|
#include <AK/printf.cpp>
|
2019-04-23 11:00:53 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-01-14 13:21:51 +00:00
|
|
|
#include <Kernel/Syscall.h>
|
2018-10-22 11:57:25 +00:00
|
|
|
|
2018-10-27 14:43:03 +00:00
|
|
|
extern "C" {
|
2018-10-22 11:57:25 +00:00
|
|
|
|
2019-04-18 14:08:52 +00:00
|
|
|
static FILE __default_streams[4];
|
2018-11-11 09:11:09 +00:00
|
|
|
FILE* stdin;
|
|
|
|
FILE* stdout;
|
|
|
|
FILE* stderr;
|
2019-04-18 14:08:52 +00:00
|
|
|
FILE* stddbg;
|
2018-11-11 09:11:09 +00:00
|
|
|
|
|
|
|
void init_FILE(FILE& fp, int fd, int mode)
|
|
|
|
{
|
|
|
|
fp.fd = fd;
|
|
|
|
fp.buffer = fp.default_buffer;
|
|
|
|
fp.buffer_size = BUFSIZ;
|
|
|
|
fp.mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE* make_FILE(int fd)
|
|
|
|
{
|
|
|
|
auto* fp = (FILE*)malloc(sizeof(FILE));
|
|
|
|
memset(fp, 0, sizeof(FILE));
|
|
|
|
init_FILE(*fp, fd, isatty(fd));
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __stdio_init()
|
|
|
|
{
|
|
|
|
stdin = &__default_streams[0];
|
|
|
|
stdout = &__default_streams[1];
|
|
|
|
stderr = &__default_streams[2];
|
2019-04-18 14:08:52 +00:00
|
|
|
stddbg = &__default_streams[3];
|
2018-11-11 09:11:09 +00:00
|
|
|
init_FILE(*stdin, 0, isatty(0) ? _IOLBF : _IOFBF);
|
|
|
|
init_FILE(*stdout, 1, isatty(1) ? _IOLBF : _IOFBF);
|
|
|
|
init_FILE(*stderr, 2, _IONBF);
|
2019-04-18 20:20:01 +00:00
|
|
|
int fd = open("/dev/debuglog", O_WRONLY | O_CLOEXEC);
|
2019-04-18 14:08:52 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("open /dev/debuglog");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
init_FILE(*stddbg, fd, _IOLBF);
|
2018-11-11 09:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int setvbuf(FILE* stream, char* buf, int mode, size_t size)
|
|
|
|
{
|
|
|
|
if (mode != _IONBF && mode != _IOLBF && mode != _IOFBF) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
stream->mode = mode;
|
|
|
|
if (buf) {
|
|
|
|
stream->buffer = buf;
|
|
|
|
stream->buffer_size = size;
|
|
|
|
} else {
|
|
|
|
stream->buffer = stream->default_buffer;
|
|
|
|
stream->buffer_size = BUFSIZ;
|
|
|
|
}
|
2018-11-11 14:36:40 +00:00
|
|
|
stream->buffer_index = 0;
|
2018-11-11 09:11:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setbuf(FILE* stream, char* buf)
|
|
|
|
{
|
|
|
|
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
|
|
|
|
}
|
|
|
|
|
2018-11-11 14:36:40 +00:00
|
|
|
void setlinebuf(FILE* stream)
|
2018-11-11 09:11:09 +00:00
|
|
|
{
|
2018-11-11 14:36:40 +00:00
|
|
|
setvbuf(stream, nullptr, _IOLBF, 0);
|
2018-11-11 09:11:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
int fileno(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
return stream->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int feof(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
return stream->eof;
|
|
|
|
}
|
|
|
|
|
2018-11-05 13:56:05 +00:00
|
|
|
int fflush(FILE* stream)
|
|
|
|
{
|
|
|
|
// FIXME: Implement buffered streams, duh.
|
|
|
|
if (!stream)
|
|
|
|
return -EBADF;
|
2018-11-11 09:11:09 +00:00
|
|
|
if (!stream->buffer_index)
|
2018-11-08 00:23:23 +00:00
|
|
|
return 0;
|
2018-11-11 09:11:09 +00:00
|
|
|
int rc = write(stream->fd, stream->buffer, stream->buffer_index);
|
|
|
|
stream->buffer_index = 0;
|
2018-11-08 00:23:23 +00:00
|
|
|
return rc;
|
2018-11-05 13:56:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
char* fgets(char* buffer, int size, FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
ssize_t nread = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (nread >= size)
|
|
|
|
break;
|
2019-02-08 16:49:54 +00:00
|
|
|
int ch = fgetc(stream);
|
2019-02-26 21:32:51 +00:00
|
|
|
if (ch == EOF) {
|
|
|
|
if (nread == 0)
|
|
|
|
return nullptr;
|
2018-10-31 16:50:43 +00:00
|
|
|
break;
|
2019-02-26 21:32:51 +00:00
|
|
|
}
|
2018-10-31 16:50:43 +00:00
|
|
|
buffer[nread++] = ch;
|
|
|
|
if (!ch || ch == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nread < size)
|
|
|
|
buffer[nread] = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fgetc(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
char ch;
|
2019-02-08 16:49:54 +00:00
|
|
|
size_t nread = fread(&ch, sizeof(char), 1, stream);
|
|
|
|
if (nread <= 0) {
|
|
|
|
stream->eof = nread == 0;
|
|
|
|
stream->error = -nread;
|
|
|
|
return EOF;
|
|
|
|
}
|
2018-10-31 16:50:43 +00:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getc(FILE* stream)
|
2018-10-22 11:57:25 +00:00
|
|
|
{
|
2018-10-31 16:50:43 +00:00
|
|
|
return fgetc(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getchar()
|
|
|
|
{
|
|
|
|
return getc(stdin);
|
|
|
|
}
|
|
|
|
|
2019-03-27 00:40:55 +00:00
|
|
|
int ungetc(int c, FILE* stream)
|
2019-02-03 03:32:31 +00:00
|
|
|
{
|
2019-03-27 00:40:55 +00:00
|
|
|
ASSERT(stream);
|
|
|
|
stream->have_ungotten = true;
|
|
|
|
stream->ungotten = c;
|
2019-03-27 04:13:28 +00:00
|
|
|
stream->eof = false;
|
2019-03-27 00:40:55 +00:00
|
|
|
return c;
|
2019-02-03 03:32:31 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
int fputc(int ch, FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-11-11 09:11:09 +00:00
|
|
|
assert(stream->buffer_index < stream->buffer_size);
|
|
|
|
stream->buffer[stream->buffer_index++] = ch;
|
|
|
|
if (stream->buffer_index >= stream->buffer_size)
|
|
|
|
fflush(stream);
|
2018-11-11 14:36:40 +00:00
|
|
|
else if (stream->mode == _IONBF || (stream->mode == _IOLBF && ch == '\n'))
|
2018-11-08 00:23:23 +00:00
|
|
|
fflush(stream);
|
2018-11-07 09:23:16 +00:00
|
|
|
if (stream->eof)
|
|
|
|
return EOF;
|
2018-10-27 14:43:03 +00:00
|
|
|
return (byte)ch;
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
int putc(int ch, FILE* stream)
|
|
|
|
{
|
|
|
|
return fputc(ch, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
int putchar(int ch)
|
|
|
|
{
|
|
|
|
return putc(ch, stdout);
|
|
|
|
}
|
|
|
|
|
2018-11-07 09:23:16 +00:00
|
|
|
int fputs(const char* s, FILE* stream)
|
|
|
|
{
|
|
|
|
for (; *s; ++s) {
|
|
|
|
int rc = putc(*s, stream);
|
|
|
|
if (rc == EOF)
|
|
|
|
return EOF;
|
|
|
|
}
|
2019-01-23 15:24:39 +00:00
|
|
|
return 0;
|
2018-11-07 09:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int puts(const char* s)
|
|
|
|
{
|
2019-01-23 15:24:39 +00:00
|
|
|
int rc = fputs(s, stdout);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
return fputc('\n', stdout);
|
2018-11-07 09:23:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
void clearerr(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
stream->eof = false;
|
2018-11-07 09:23:16 +00:00
|
|
|
stream->error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ferror(FILE* stream)
|
|
|
|
{
|
|
|
|
return stream->error;
|
2018-10-31 16:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2019-03-27 00:40:55 +00:00
|
|
|
if (!size)
|
|
|
|
return 0;
|
|
|
|
|
2019-03-27 04:13:28 +00:00
|
|
|
ssize_t nread = 0;
|
|
|
|
|
2019-03-27 00:40:55 +00:00
|
|
|
if (stream->have_ungotten) {
|
|
|
|
// FIXME: Support ungotten character even if size != 1.
|
|
|
|
ASSERT(size == 1);
|
|
|
|
((char*)ptr)[0] = stream->ungotten;
|
|
|
|
stream->have_ungotten = false;
|
|
|
|
--nmemb;
|
2019-03-27 04:13:28 +00:00
|
|
|
if (!nmemb)
|
|
|
|
return 1;
|
2019-03-27 00:40:55 +00:00
|
|
|
ptr = &((char*)ptr)[1];
|
2019-03-27 04:13:28 +00:00
|
|
|
++nread;
|
2019-03-27 00:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 04:13:28 +00:00
|
|
|
ssize_t rc = read(stream->fd, ptr, nmemb * size);
|
|
|
|
if (rc < 0)
|
2018-10-31 16:50:43 +00:00
|
|
|
return 0;
|
2019-03-27 04:13:28 +00:00
|
|
|
if (rc == 0)
|
2018-10-31 16:50:43 +00:00
|
|
|
stream->eof = true;
|
2019-03-27 04:13:28 +00:00
|
|
|
nread += rc;
|
2019-03-25 23:10:58 +00:00
|
|
|
return nread / size;
|
2018-10-31 16:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-11-08 00:23:23 +00:00
|
|
|
fflush(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
ssize_t nwritten = write(stream->fd, ptr, nmemb * size);
|
|
|
|
if (nwritten < 0)
|
|
|
|
return 0;
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fseek(FILE* stream, long offset, int whence)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
off_t off = lseek(stream->fd, offset, whence);
|
|
|
|
if (off < 0)
|
|
|
|
return off;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long ftell(FILE* stream)
|
|
|
|
{
|
2018-10-31 20:31:56 +00:00
|
|
|
assert(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
return lseek(stream->fd, 0, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewind(FILE* stream)
|
|
|
|
{
|
|
|
|
fseek(stream, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2019-01-14 19:00:42 +00:00
|
|
|
int dbgprintf(const char* fmt, ...)
|
2019-01-14 13:21:51 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2019-04-18 14:08:52 +00:00
|
|
|
int ret = vfprintf(stddbg, fmt, ap);
|
2019-01-14 13:21:51 +00:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stdout_putch(char*&, char ch)
|
2018-10-22 11:57:25 +00:00
|
|
|
{
|
2018-10-30 14:33:37 +00:00
|
|
|
putchar(ch);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 09:14:56 +00:00
|
|
|
static FILE* __current_stream = nullptr;
|
|
|
|
static void stream_putch(char*&, char ch)
|
|
|
|
{
|
2018-11-08 00:23:23 +00:00
|
|
|
fputc(ch, __current_stream);
|
2018-10-31 09:14:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 23:11:08 +00:00
|
|
|
int vfprintf(FILE* stream, const char* fmt, va_list ap)
|
|
|
|
{
|
|
|
|
__current_stream = stream;
|
2019-01-31 16:31:23 +00:00
|
|
|
return printf_internal(stream_putch, nullptr, fmt, ap);
|
2018-11-16 23:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fprintf(FILE* stream, const char* fmt, ...)
|
2018-10-31 09:14:56 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2018-11-16 23:11:08 +00:00
|
|
|
int ret = vfprintf(stream, fmt, ap);
|
2018-10-31 09:14:56 +00:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-01 15:03:21 +00:00
|
|
|
int vprintf(const char* fmt, va_list ap)
|
|
|
|
{
|
|
|
|
return printf_internal(stdout_putch, nullptr, fmt, ap);
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:57:25 +00:00
|
|
|
int printf(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2019-02-01 15:03:21 +00:00
|
|
|
int ret = vprintf(fmt, ap);
|
2018-10-22 11:57:25 +00:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_putch(char*& bufptr, char ch)
|
|
|
|
{
|
|
|
|
*bufptr++ = ch;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:11:08 +00:00
|
|
|
int vsprintf(char* buffer, const char* fmt, va_list ap)
|
|
|
|
{
|
2019-01-31 16:31:23 +00:00
|
|
|
int ret = printf_internal(buffer_putch, buffer, fmt, ap);
|
2018-11-16 23:11:08 +00:00
|
|
|
buffer[ret] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:57:25 +00:00
|
|
|
int sprintf(char* buffer, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2018-11-16 23:11:08 +00:00
|
|
|
int ret = vsprintf(buffer, fmt, ap);
|
2018-10-22 11:57:25 +00:00
|
|
|
buffer[ret] = '\0';
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-23 05:35:34 +00:00
|
|
|
static size_t __vsnprintf_space_remaining;
|
|
|
|
static void sized_buffer_putch(char*& bufptr, char ch)
|
|
|
|
{
|
|
|
|
if (__vsnprintf_space_remaining) {
|
|
|
|
*bufptr++ = ch;
|
|
|
|
--__vsnprintf_space_remaining;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap)
|
|
|
|
{
|
|
|
|
__vsnprintf_space_remaining = size;
|
2019-01-31 16:31:23 +00:00
|
|
|
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
|
2019-01-23 05:35:34 +00:00
|
|
|
buffer[ret] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int snprintf(char* buffer, size_t size, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int ret = vsnprintf(buffer, size, fmt, ap);
|
|
|
|
buffer[ret] = '\0';
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:56:21 +00:00
|
|
|
void perror(const char* s)
|
|
|
|
{
|
2018-10-31 20:37:09 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", s, strerror(errno));
|
2018-10-26 12:56:21 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
FILE* fopen(const char* pathname, const char* mode)
|
|
|
|
{
|
2019-03-14 14:18:15 +00:00
|
|
|
int flags = 0;
|
|
|
|
if (!strcmp(mode, "r") || !strcmp(mode, "rb"))
|
|
|
|
flags = O_RDONLY;
|
|
|
|
else if (!strcmp(mode, "r+") || !strcmp(mode, "rb+"))
|
|
|
|
flags = O_RDWR;
|
|
|
|
else if (!strcmp(mode, "w") || !strcmp(mode, "wb"))
|
|
|
|
flags = O_WRONLY | O_CREAT | O_TRUNC;
|
|
|
|
else if (!strcmp(mode, "w+") || !strcmp(mode, "wb+"))
|
|
|
|
flags = O_RDWR | O_CREAT | O_TRUNC;
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "FIXME(LibC): fopen('%s', '%s')\n", pathname, mode);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
int fd = open(pathname, flags, 0666);
|
2018-10-31 16:50:43 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return nullptr;
|
2018-11-11 09:11:09 +00:00
|
|
|
return make_FILE(fd);
|
2018-10-31 16:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:19:32 +00:00
|
|
|
FILE* freopen(const char* pathname, const char* mode, FILE* stream)
|
|
|
|
{
|
|
|
|
(void)pathname;
|
|
|
|
(void)mode;
|
|
|
|
(void)stream;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:01:22 +00:00
|
|
|
FILE* fdopen(int fd, const char* mode)
|
|
|
|
{
|
2019-04-23 11:00:53 +00:00
|
|
|
UNUSED_PARAM(mode);
|
2019-03-14 14:18:15 +00:00
|
|
|
// FIXME: Verify that the mode matches how fd is already open.
|
2018-11-05 18:01:22 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return nullptr;
|
2018-11-11 09:11:09 +00:00
|
|
|
return make_FILE(fd);
|
2018-11-05 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
int fclose(FILE* stream)
|
|
|
|
{
|
2019-03-27 00:40:55 +00:00
|
|
|
fflush(stream);
|
2018-10-31 16:50:43 +00:00
|
|
|
int rc = close(stream->fd);
|
|
|
|
free(stream);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-02-08 01:38:21 +00:00
|
|
|
int rename(const char* oldpath, const char* newpath)
|
|
|
|
{
|
2019-04-07 21:35:26 +00:00
|
|
|
int rc = syscall(SC_rename, oldpath, newpath);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-02-08 01:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 11:57:02 +00:00
|
|
|
char* tmpnam(char*)
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* popen(const char* command, const char* type)
|
|
|
|
{
|
|
|
|
(void)command;
|
|
|
|
(void)type;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pclose(FILE*)
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:18:15 +00:00
|
|
|
int remove(const char* pathname)
|
|
|
|
{
|
|
|
|
int rc = unlink(pathname);
|
|
|
|
if (rc < 0 && errno != EISDIR)
|
|
|
|
return -1;
|
|
|
|
return rmdir(pathname);
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:29:04 +00:00
|
|
|
int scanf(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int count = vfscanf(stdin, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fscanf(FILE* stream, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int count = vfscanf(stream, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sscanf(const char* buffer, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int count = vsscanf(buffer, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int vfscanf(FILE* stream, const char* fmt, va_list ap)
|
|
|
|
{
|
|
|
|
char buffer[BUFSIZ];
|
|
|
|
if (!fgets(buffer, sizeof(buffer) - 1, stream))
|
|
|
|
return -1;
|
|
|
|
return vsscanf(buffer, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|