From 381a56b5ef3fe1ed63ee18ffa49b16ef0ab04528 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Tue, 16 May 2023 12:33:11 +0530 Subject: 8.2: initial commit --- 8.2/Makefile | 13 +++++++++ 8.2/main.c | 6 +++++ 8.2/stdio.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8.2/stdio.h | 48 +++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 8.2/Makefile create mode 100644 8.2/main.c create mode 100644 8.2/stdio.c create mode 100644 8.2/stdio.h (limited to '8.2') diff --git a/8.2/Makefile b/8.2/Makefile new file mode 100644 index 0000000..e77912f --- /dev/null +++ b/8.2/Makefile @@ -0,0 +1,13 @@ +OBJECTS = main.o stdio.o +CC = gcc +CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address + +exe : $(OBJECTS) + $(CC) $(CFLAGS) -o exe $(OBJECTS) + +main.o : stdio.h + +.PHONY : clean + +clean : + rm -f exe $(OBJECTS) diff --git a/8.2/main.c b/8.2/main.c new file mode 100644 index 0000000..34d30a6 --- /dev/null +++ b/8.2/main.c @@ -0,0 +1,6 @@ +#include "stdio.h" + +int main(void) +{ + return 0; +} diff --git a/8.2/stdio.c b/8.2/stdio.c new file mode 100644 index 0000000..dd6cbef --- /dev/null +++ b/8.2/stdio.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include "stdio.h" + +#define PERMS 0666 + +FILE _iob[OPN_MAX] = { + {0, (char *) NULL, (char *) NULL, {1, 0, 0, 1, 0, 0}, 0}, + {0, (char *) NULL, (char *) NULL, {0, 1, 0, 1, 0, 0}, 1}, + {0, (char *) NULL, (char *) NULL, {0, 1, 1, 0, 0, 0}, 2}, +}; + +FILE *fopen(char *name, char *mode) +{ + int fd; + FILE *fp; + + if (*mode != 'r' && *mode != 'w' && *mode != 'a') + return NULL; + + for (fp = _iob; fp < _iob + OPN_MAX; ++fp) + if (fp->flag.is_read == 0 && fp->flag.is_write == 0) + break; + if (fp == _iob + OPN_MAX) + return NULL; + + if (*mode == 'w') { + fd = creat(name, PERMS); + } else if (*mode == 'a') { + if ((fd = open(name, O_WRONLY)) == -1) + fd = creat(name, PERMS); + lseek(fd, 0L, 2); + } else { + fd = open(name, O_RDONLY); + } + + if (fd == -1) + return NULL; + + fp->fd = fd; + fp->cnt = 0; + fp->base = NULL; + fp->flag.is_unbuf = 0; + fp->flag.is_buf = 1; + fp->flag.is_eof = 0; + fp->flag.is_err = 0; + if (*mode == 'r') { + fp->flag.is_read = 1; + fp->flag.is_write = 0; + } else { + fp->flag.is_write = 1; + fp->flag.is_read = 0; + } + + return fp; +} + +int _fillbuf(FILE *fp) +{ + int bufsize; + + if (fp->flag.is_read == 0 || + fp->flag.is_err == 1 || + fp->flag.is_eof == 1) + return EOF; + + bufsize = (fp->flag.is_unbuf == 1) ? 1 : BUFSIZ; + if (fp->base == NULL) + if ((fp->base = (char *) 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.is_eof = 1; + else + fp->flag.is_err = 1; + fp->cnt = 0; + return EOF; + } + + return (unsigned char) *fp->ptr++; +} diff --git a/8.2/stdio.h b/8.2/stdio.h new file mode 100644 index 0000000..f7520de --- /dev/null +++ b/8.2/stdio.h @@ -0,0 +1,48 @@ +#include +#include +#include + +#undef NULL +#define NULL 0 +#define EOF (-1) +#define BUFSIZ 8192 +#define OPN_MAX 20 + +struct flag_field { + unsigned int is_read : 1; + unsigned int is_write : 1; + unsigned int is_unbuf : 1; + unsigned int is_buf : 1; + unsigned int is_eof : 1; + unsigned int is_err : 1; +}; + +typedef struct _iobuff { + int cnt; + char *ptr; + char *base; + struct flag_field flag; + int fd; +} FILE; + +extern FILE _iob[OPN_MAX]; + +#define stdin (&_iob[0]); +#define stdout (&_iob[1]); +#define stderr (&_iob[2]); + +int _fillbuf(FILE *); +int _flushbuf(int, FILE *); +FILE *fopen(char *name, char *mode); + +#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, stdin) -- cgit v1.2.3