home *** CD-ROM | disk | FTP | other *** search
- /*
- * fake_stdio.c
- *
- * By Ross Ridge
- * Public Domain
- * 92/02/01 07:29:52
- *
- * fake some stdio functions
- *
- */
-
- #include "defs.h"
-
- #include <fcntl.h>
-
- #ifdef USE_FAKE_STDIO
-
- #ifdef USE_SCCS_IDS
- static const char SCCSid[] = "@(#) mytinfo fake_stdio.c 3.2 92/02/01 public domain, By Ross Ridge";
- #endif
- #include "fake_stdio.h"
-
- static FILE _fake_files[] = {
- {NULL, NULL, -1, {'\0'}},
- {NULL, NULL, -1, {'\0'}},
- {NULL, NULL, -1, {'\0'}},
- {NULL, NULL, -1, {'\0'}},
- {NULL, NULL, -1, {'\0'}}
- };
-
- int
- _fillbuf(f)
- register FILE *f; {
- register int r;
-
- r = read(f->fd, f->buf, FAKE_BUF_SIZE);
- if (r == -1 || r == 0) {
- f->pos = f->end = f->buf;
- return EOF;
- }
- f->pos = f->buf + 1;
- f->end = f->buf + r;
-
- return f->buf[0];
- }
-
- int
- fgetc(f)
- register FILE *f; {
- return getc(f);
- }
-
- char *
- fgets(s, n, f)
- char *s;
- int n;
- register FILE *f; {
- register char *d;
- register int l;
- register int c;
-
- d = s;
- l = 1;
- while(l < n) {
- if ((c = getc(f)) == EOF) {
- if (l == 0)
- return NULL;
- break;
- }
- *d++ = c;
- if (c == '\n')
- break;
- l++;
- }
- *d = '\0';
- return s;
- }
-
- static FILE *
- _fdopen(fd)
- int fd; {
- register FILE *f;
- int i, r;
-
- for(f = _fake_files, i = 0; i < FILES; i++, f++) {
- if (f->fd == -1) {
- f->fd = fd;
- r = read(fd, f->buf, FAKE_BUF_SIZE);
- if (r == -1) {
- f->pos = f->end = f->buf;
- return NULL;
- }
- f->pos = f->buf;
- f->end = f->buf + r;
- return f;
- }
- }
- return NULL;
- }
-
- FILE *
- fopen(name, type)
- char *name;
- char *type; {
- FILE *f;
- int fd;
-
- if (strcmp(type, "r") != 0)
- return NULL;
- fd = open(name, O_RDONLY);
- if (fd == -1)
- return NULL;
- f = _fdopen(fd);
- if (f == NULL)
- close(fd);
- return f;
- }
-
- FILE *
- fdopen(fd, type)
- int fd;
- char *type; {
- if (strcmp(type, "r") != 0)
- return NULL;
- return _fdopen(fd);
- }
-
- #endif /* USE_FAKE_STDIO */
-