From 68f15b3002235095d9649ba423069b492cf63746 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Thu, 18 May 2023 12:44:37 +0530 Subject: 8.4: initial commit --- 8.4/Makefile | 12 +++++ 8.4/main.c | 8 +++ 8.4/stdio.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8.4/stdio.h | 50 +++++++++++++++++++ 4 files changed, 230 insertions(+) create mode 100644 8.4/Makefile create mode 100644 8.4/main.c create mode 100644 8.4/stdio.c create mode 100644 8.4/stdio.h diff --git a/8.4/Makefile b/8.4/Makefile new file mode 100644 index 0000000..fb8a9bb --- /dev/null +++ b/8.4/Makefile @@ -0,0 +1,12 @@ +OBJECTS = main.o stdio.o +CC = gcc +CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address + +sneed : $(OBJECTS) + $(CC) $(CFLAGS) -o sneed $(OBJECTS) + +main.o : stdio.h + +.PHONY : clean +clean : + rm -f sneed $(OBJECTS) diff --git a/8.4/main.c b/8.4/main.c new file mode 100644 index 0000000..4c5b7b6 --- /dev/null +++ b/8.4/main.c @@ -0,0 +1,8 @@ +#include "stdio.h" + +int main(void) +{ + puts("sneed from sneedio.h (formerly stdio.h)\n"); + + return 0; +} diff --git a/8.4/stdio.c b/8.4/stdio.c new file mode 100644 index 0000000..57b90ba --- /dev/null +++ b/8.4/stdio.c @@ -0,0 +1,160 @@ +#include +#include +#include +#include "stdio.h" + +#define PERMS 0666 + +FILE _iob[OPN_MAX] = { + {0, NULL, NULL, _READ, 0}, + {0, NULL, NULL, _WRITE, 1}, + {0, NULL, NULL, _WRITE | _UNBUF, 2} +}; + +FILE *fopen(char *name, char *mode) +{ + FILE *fp; + + if (*mode != 'r' && *mode != 'w' && *mode != 'a') + return NULL; + + for(fp = _iob; fp < _iob + OPN_MAX; ++fp) + if (!(fp->flag & (_READ | _WRITE))) + break; + if (fp == _iob + OPN_MAX) + return NULL; + + if (*mode == 'w') { + fp->fd = open(name, O_WRONLY); + } else if (*mode == 'a') { + if ((fp->fd = open(name, O_WRONLY)) == -1) + fp->fd = creat(name, PERMS); + lseek(fp->fd, 0L, 2); + } else { + fp->fd = open(name, O_RDONLY); + } + + if (fp->fd == -1) { + fp->fd = 0; + return NULL; + } + + fp->base = fp->ptr = NULL; + fp->flag = (*mode == 'r') ? _READ : _WRITE; + fp->cnt = 0; + + return fp; +} + +int _fillbuf(FILE *fp) +{ + int bufsize; + + if ((fp->flag & (_READ | _EOF | _ERR)) != _READ) + return EOF; + + bufsize = (fp->flag & _UNBUF) ? 1 : BUFSIZ; + if (fp->base == NULL) + if ((fp->base = malloc(bufsize)) == NULL) + return EOF; + fp->ptr = fp->base; + + fp->cnt = read(fp->fd, fp->base, bufsize); + if (--fp->cnt < 0) { + if (fp->cnt == -1) + fp->flag = _EOF; + else // fp->cnt == -2 + fp->flag = _ERR; + fp->cnt = 0; + return EOF; + } + + return (unsigned char) *fp->ptr++; +} + +int _flushbuf(int c, FILE *fp) +{ + int bufsize, nc; + + if ((fp->flag & (_WRITE | _ERR)) != _WRITE) + return EOF; + + bufsize = (fp->flag & _UNBUF) ? 1 : BUFSIZ; + if (fp->base == NULL) { + if ((fp->base = malloc(bufsize)) == NULL) + return EOF; + } else { + nc = fp->ptr - fp->base; + if (write(fp->fd, fp->base, nc) != nc) { + fp->flag = _ERR; + return EOF; + } + } + + fp->ptr = fp->base; + fp->cnt = bufsize - 1; + *fp->ptr++ = c; + + return (unsigned char) c; +} + +int fflush(FILE *fp) +{ + int rc = 0; + + if (fp < _iob && fp >= _iob + OPN_MAX) + return EOF; + if (fp->flag & _WRITE) + rc = _flushbuf(0, fp); + + fp->ptr = fp->base; + fp->cnt = (fp->flag & _UNBUF) ? 1 : BUFSIZ; + + return rc; +} + + +int fclose(FILE *fp) +{ + int rc; + + if ((rc = fflush(fp)) != EOF) { + free(fp->base); + fp->base = fp->ptr = NULL; + fp->flag &= ~(_READ | _WRITE); + fp->cnt = 0; + close(fp->fd); + } + + return rc; +} + +int fseek(FILE *fp, ssize_t os, int origin) +{ + int rc = 0; + + if (fp < _iob && fp >= _iob + OPN_MAX) + return EOF; + + if (fp->flag & _READ) { + if (os == SEEK_CUR) + os -= fp->cnt; + rc = lseek(fp->fd, os, origin); + fp->cnt = 0; + } else if (fp->flag & _WRITE) { + if ((rc = fflush(fp)) != EOF) + rc = lseek(fp->fd, os, origin); + } + + return (rc == -1) ? -1 : 0; +} + +int puts(const char *s) +{ + while (*s) + if (putchar(*s++) == EOF) + return EOF; + fflush(stdout); + + return 1; +} diff --git a/8.4/stdio.h b/8.4/stdio.h new file mode 100644 index 0000000..37413a2 --- /dev/null +++ b/8.4/stdio.h @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +#define EOF (-1) +#define BUFSIZ 8192 +#define OPN_MAX 20 + +typedef struct { + int cnt; + char *ptr; + char *base; + int flag; + int fd; +} FILE; + +extern FILE _iob[OPN_MAX]; + +#define stdin (_iob+0) +#define stdout (_iob+1) +#define stderr (_iob+2) + +enum flags { + _READ = 1 << 0, + _WRITE = 1 << 2, + _UNBUF = 1 << 3, + _EOF = 1 << 4, + _ERR = 1 << 5 +}; + +int _fillbuf(FILE *); +int _flushbuf(int, FILE *); +FILE *fopen(char *name, char *mode); +int fflush(FILE *fp); +int fseek(FILE *fp, ssize_t os, int whence); +int fclose(FILE *fp); +int puts(const char *); + +#define feof(p) ((p)->flag & _EOF) +#define ferror(p) ((p)->flag & _ERR) +#define fileno(p) ((p)->fd) + +#define getc(p) ((--(p)->cnt >= 0) \ + ? (unsigned char) *(p)->ptr++ : _fillbuf(p)) +#define putc(x, p) ((--(p)->cnt >= 0) \ + ? *(p)->ptr++ = (x) : _flushbuf(x, p)) + +#define getchar() getc(stdin) +#define putchar(x) putc((x), stdout) -- cgit v1.2.3