2012-09-28 18:36:46 -04:00
|
|
|
/*******************************************************************************
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
|
2013-06-08 11:31:11 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of the Sortix C Library.
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
The Sortix C Library is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
The Sortix C Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
fdio.c
|
|
|
|
Handles the file descriptor backend for the FILE* API.
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
|
2012-09-28 18:36:46 -04:00
|
|
|
*******************************************************************************/
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
|
2013-06-08 11:31:11 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2013-06-08 11:31:11 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-02-12 06:14:27 -05:00
|
|
|
#include "fdio.h"
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
|
|
|
|
const int FDIO_WRITING = (1<<0);
|
|
|
|
const int FDIO_READING = (1<<1);
|
|
|
|
const int FDIO_APPEND = (1<<2);
|
|
|
|
const int FDIO_ERROR = (1<<3);
|
|
|
|
const int FDIO_EOF = (1<<4);
|
|
|
|
|
|
|
|
typedef struct fdio_struct
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
int fd;
|
|
|
|
} fdio_t;
|
|
|
|
|
|
|
|
static size_t fdio_read(void* ptr, size_t size, size_t nmemb, void* user)
|
|
|
|
{
|
|
|
|
uint8_t* buf = (uint8_t*) ptr;
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
|
|
|
if ( !(fdio->flags & FDIO_READING) ) { errno = EBADF; return 0; }
|
|
|
|
size_t sofar = 0;
|
|
|
|
size_t total = size * nmemb;
|
|
|
|
while ( sofar < total )
|
|
|
|
{
|
|
|
|
ssize_t numbytes = read(fdio->fd, buf + sofar, total - sofar);
|
2012-09-03 04:19:09 -04:00
|
|
|
if ( numbytes < 0 ) { fdio->flags |= FDIO_ERROR; break; }
|
|
|
|
if ( numbytes == 0 ) { fdio->flags |= FDIO_EOF; break; }
|
2012-12-08 15:14:29 -05:00
|
|
|
return numbytes / size;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
sofar += numbytes;
|
|
|
|
}
|
2012-09-03 04:19:09 -04:00
|
|
|
return sofar / size;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t fdio_write(const void* ptr, size_t size, size_t nmemb, void* user)
|
|
|
|
{
|
|
|
|
const uint8_t* buf = (const uint8_t*) ptr;
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
|
|
|
if ( !(fdio->flags & FDIO_WRITING) ) { errno = EBADF; return 0; }
|
|
|
|
size_t sofar = 0;
|
|
|
|
size_t total = size * nmemb;
|
|
|
|
while ( sofar < total )
|
|
|
|
{
|
|
|
|
ssize_t numbytes = write(fdio->fd, buf + sofar, total - sofar);
|
2012-09-03 04:19:09 -04:00
|
|
|
if ( numbytes < 0 ) { fdio->flags |= FDIO_ERROR; break; }
|
|
|
|
if ( numbytes == 0 ) { fdio->flags |= FDIO_EOF; break; }
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
sofar += numbytes;
|
|
|
|
}
|
2012-09-03 04:19:09 -04:00
|
|
|
return sofar / size;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
}
|
|
|
|
|
2011-12-29 18:36:39 -05:00
|
|
|
static int fdio_seek(void* user, off_t offset, int whence)
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
{
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
2012-09-07 06:39:13 -04:00
|
|
|
return 0 <= lseek(fdio->fd, offset, whence) ? 0 : -1;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
}
|
|
|
|
|
2011-12-29 18:36:39 -05:00
|
|
|
static off_t fdio_tell(void* user)
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
{
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
2012-09-07 06:39:13 -04:00
|
|
|
return lseek(fdio->fd, 0, SEEK_CUR);;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
}
|
|
|
|
|
2012-03-11 19:53:14 -04:00
|
|
|
static void fdio_seterr(void* user)
|
|
|
|
{
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
|
|
|
fdio->flags |= FDIO_ERROR;
|
|
|
|
}
|
|
|
|
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
static void fdio_clearerr(void* user)
|
|
|
|
{
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
|
|
|
fdio->flags &= ~FDIO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdio_eof(void* user)
|
|
|
|
{
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
|
|
|
return fdio->flags & FDIO_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdio_error(void* user)
|
|
|
|
{
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
|
|
|
return fdio->flags & FDIO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdio_fileno(void* user)
|
|
|
|
{
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
|
|
|
return fdio->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdio_close(void* user)
|
|
|
|
{
|
|
|
|
fdio_t* fdio = (fdio_t*) user;
|
|
|
|
int result = close(fdio->fd);
|
|
|
|
free(fdio);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fdio_install(FILE* fp, const char* mode, int fd)
|
|
|
|
{
|
|
|
|
fdio_t* fdio = (fdio_t*) calloc(1, sizeof(fdio_t));
|
2013-06-08 11:31:11 -04:00
|
|
|
if ( !fdio )
|
|
|
|
return 0;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
fdio->fd = fd;
|
|
|
|
char c;
|
|
|
|
// TODO: This is too hacky and a little buggy.
|
|
|
|
while ( ( c = *mode++ ) )
|
|
|
|
{
|
|
|
|
switch ( c )
|
|
|
|
{
|
|
|
|
case 'r': fdio->flags |= FDIO_READING; break;
|
|
|
|
case 'w': fdio->flags |= FDIO_WRITING; break;
|
|
|
|
case '+': fdio->flags |= FDIO_READING | FDIO_WRITING; break;
|
|
|
|
case 'a': fdio->flags |= FDIO_WRITING | FDIO_APPEND; break;
|
|
|
|
case 'b': break;
|
|
|
|
default: errno = EINVAL; free(fdio); return 0;
|
|
|
|
}
|
|
|
|
}
|
2013-06-08 11:31:11 -04:00
|
|
|
struct stat st;
|
|
|
|
if ( !fstat(fd, &st) && fdio->flags & FDIO_WRITING && S_ISDIR(st.st_mode) )
|
|
|
|
return free(fdio), errno = EISDIR, 0;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
fp->user = fdio;
|
|
|
|
fp->read_func = fdio_read;
|
|
|
|
fp->write_func = fdio_write;
|
|
|
|
fp->seek_func = fdio_seek;
|
|
|
|
fp->tell_func = fdio_tell;
|
2012-03-11 19:53:14 -04:00
|
|
|
fp->seterr_func = fdio_seterr;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
fp->clearerr_func = fdio_clearerr;
|
|
|
|
fp->eof_func = fdio_eof;
|
|
|
|
fp->error_func = fdio_error;
|
|
|
|
fp->fileno_func = fdio_fileno;
|
|
|
|
fp->close_func = fdio_close;
|
2012-12-08 15:14:29 -05:00
|
|
|
if ( lseek(fd, 0, SEEK_CUR) < 0 && errno == ESPIPE )
|
|
|
|
fp->flags |= _FILE_STREAM;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* fdio_newfile(int fd, const char* mode)
|
|
|
|
{
|
|
|
|
FILE* fp = fnewfile();
|
|
|
|
if ( !fp ) { return NULL; }
|
|
|
|
if ( !fdio_install(fp, mode, fd) ) { fclose(fp); return NULL; }
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* fdopen(int fd, const char* mode)
|
|
|
|
{
|
|
|
|
return fdio_newfile(fd, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* fopen(const char* path, const char* mode)
|
|
|
|
{
|
|
|
|
int omode = 0;
|
|
|
|
int oflags = 0;
|
|
|
|
char c;
|
|
|
|
// TODO: This is too hacky and a little buggy.
|
2012-03-01 08:30:20 -05:00
|
|
|
const char* origmode = mode;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
while ( ( c = *mode++ ) )
|
|
|
|
{
|
|
|
|
switch ( c )
|
|
|
|
{
|
|
|
|
case 'r': omode = O_RDONLY; break;
|
|
|
|
case 'a': oflags |= O_APPEND; /* fall-through */
|
2013-01-08 09:07:38 -05:00
|
|
|
case 'w': omode = O_WRONLY; oflags |= O_CREAT | O_TRUNC; break;
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
case '+':
|
|
|
|
omode = O_RDWR;
|
|
|
|
break;
|
|
|
|
case 'b': break;
|
|
|
|
default: errno = EINVAL; return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-03-01 08:30:20 -05:00
|
|
|
mode = origmode;
|
2012-01-18 06:40:31 -05:00
|
|
|
int fd = open(path, omode | oflags, 0666);
|
Implemented large parts of the stdio(3), including fprintf.
Made FILE an interface to various backends. This allows application writers
to override the standard FILE API functions with their own backends. This
is highly unportable - it'd be nice if a real standard existed for this.
glibc already does something like this internally, but AFAIK you can't hook
into it.
Added fdopen(3), fopen(3), fregister(3), funregister(3), fread(3),
fwrite(3), fseek(3), clearerr(3), ferror(3), feof(3), rewind(3), ftell(3),
fflush(3), fclose(3), fileno(3), fnewline(3), fcloseall(3), memset(3),
stdio(3), vfprintf(3), fprintf(3), and vprintf(3).
Added a file-descriptor backend to the FILE API.
fd's {0, 1, 2} are now initialized as stdin, stdout, and stderr when the
standard library initializes.
fcloseall(3) is now called on exit(3).
decl/intn_t_.h now @include(size_t.h) instead of declaring it itself.
Added <stdint.h>.
The following programs now flush stdout: cat(1), clear(1), editor(1),
init(1), mxsh(1).
printf(3) is now hooked up against vprintf(3), while Maxsi::PrintF
remains using the system call, for now.
2011-12-23 22:08:10 -05:00
|
|
|
if ( fd < 0 ) { return NULL; }
|
|
|
|
FILE* fp = fdopen(fd, mode);
|
|
|
|
if ( !fp ) { close(fd); return NULL; }
|
|
|
|
return fp;
|
|
|
|
}
|