Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <stdarg.h>
- // The functions available from stdio.h are implemented here.
- //#include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/syscall.h>
- #ifdef NULL
- #undef NULL
- #endif
- #define NULL 0
- #define EOF (-1)
- #define BUFSIZ 1024
- #define OPEN_MAX 20 /* max # files open at once */
- struct flags{
- unsigned int _read : 1;
- unsigned int _write : 1;
- unsigned int _unbuf : 1;
- unsigned int _eof : 1;
- unsigned int _err : 1;
- };
- typedef struct _iobuf {
- int cnt; /* characters left */
- char *ptr; /* next character position */
- char *base; /* location of the buffer */
- struct flags flag; /* mode of the file access */
- int fd; /* file descriptor */
- } FILE;
- extern FILE _iob[OPEN_MAX];
- #define stdin (&_iob[0])
- #define stdout (&_iob[1])
- #define stderr (&_iob[2])
- enum _flags {
- _READ = 01, /* file open for reading */ /* binary 1 */
- _WRITE = 02, /* file open for writing */ /* binary 10 */
- _UNBUF = 03, /* file is unbuffered */ /* binary 11 */
- _EOF = 010, /* EOF has occurred on this file */ /* binary 1000 */
- _ERR = 020, /* error occurred on this file */ /* binary 10000*/
- };
- int _fillbuf(FILE *);
- int _flushbuf(int, FILE *);
- #define feof(p) (((p)->flag._eof) != 0)
- #define ferror(p) (((p)->flag._err) != 0)
- #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)
- #define PERMS 0666 /* RW for owner, group and others */
- /* fopen: open file, return file ptr */
- FILE *fopen(char *name,char *mode){
- int fd;
- FILE *fp;
- if(*mode != 'r' && *mode != 'w' && *mode != 'a')
- return NULL;
- for(fp = _iob; fp<_iob + OPEN_MAX; fp++)
- if(fp->flag._read == 0 && fp->flag._write == 0)
- break;
- if( fp >= _iob + OPEN_MAX)
- return NULL;
- if(*mode == 'w')
- fd = creat(name, PERMS);
- else if(*mode == 'a') {
- if ((fd = open(name, O_WRONLY, 0)) == -1)
- fd = creat(name, PERMS);
- lseek(fd, 0L, 2);
- } else
- fd = open(name, O_RDONLY, 0);
- if(fd == -1)
- return NULL;
- fp->fd = fd;
- fp->cnt = 0;
- fp->base = NULL;
- if (*mode == 'r')
- fp->flag._read = 1,fp->flag._write = 0,fp->flag._unbuf = 0,fp->flag._eof = 0,fp->flag._err = 0;
- else
- fp->flag._read = 0,fp->flag._write = 1,fp->flag._unbuf = 0,fp->flag._eof = 0,fp->flag._err = 0;
- return fp;
- }
- int _fillbuf(FILE *fp){
- int bufsize;
- if(fp->flag._read != 1 || fp->flag._eof == 1 || fp->flag._err == 1)
- return EOF;
- bufsize = (fp->flag._unbuf) ? 1: BUFSIZ;
- if(fp->base == NULL)
- if((fp->base = (char *) malloc(bufsize)) == NULL)
- return EOF; /* cant get buffer */
- fp->ptr = fp->base;
- fp->cnt = read(fp->fd,fp->ptr,bufsize);
- if(--fp->cnt < 0) {
- if (fp->cnt == -1)
- fp->flag._eof = 1;
- else
- fp->flag._err = 1;
- fp->cnt = 0;
- return EOF;
- }
- return (unsigned char) *fp->ptr++;
- }
- int _flushbuf(int c,FILE *fp){
- int num_written, bufsize;
- unsigned char uc = c;
- if(fp->flag._write != 1 || fp->flag._eof == 1 || fp->flag._err == 1)
- return EOF;
- if(fp->base == NULL && (fp->flag._unbuf)==0) {
- if ((fp->base = malloc(BUFSIZ)) == NULL)
- fp->flag._unbuf = 1;
- else{
- fp->ptr = fp->base;
- fp->cnt = BUFSIZ-1;
- }
- }
- if(fp->flag._unbuf){
- fp->ptr=fp->base=NULL;
- fp->cnt=0;
- if(c==EOF)
- return EOF;
- num_written = write(fp->fd,&uc,1);
- bufsize = 1;
- } else {
- bufsize = (int)(fp->ptr - fp->base);
- num_written = write(fp->fd,fp->base,bufsize);
- fp->ptr = fp->base;
- fp->cnt = BUFSIZ-1;
- }
- if(num_written==bufsize)
- return c;
- else
- {
- fp->flag._err = 1;
- return EOF;
- }
- }
- int fflush(FILE *fp){
- int num_written, bufsize;
- unsigned char uc = 's';
- if(fp->flag._write != 1 || fp->flag._eof == 1 || fp->flag._err == 1)
- return EOF;
- if(fp->base == NULL && (fp->flag._unbuf)==0) {
- if ((fp->base = malloc(BUFSIZ)) == NULL)
- fp->flag._unbuf = 1;
- else{
- fp->ptr = fp->base;
- fp->cnt = BUFSIZ-1;
- }
- }
- if(fp->flag._unbuf){
- return EOF;
- } else {
- bufsize = (int)(fp->ptr - fp->base);
- num_written = write(fp->fd,fp->base,bufsize);
- fp->ptr = fp->base;
- fp->cnt = BUFSIZ-1;
- }
- if(num_written==bufsize)
- return uc;
- else
- {
- fp->flag._err = 1;
- return EOF;
- }
- }
- int fclose(FILE *fp){
- fflush(fp);
- fp->cnt = 0;
- fp->ptr = NULL;
- fp->base = NULL;
- fp->flag._read = 0,fp->flag._write = 0,fp->flag._unbuf = 0,fp->flag._eof = 0,fp->flag._err = 0;
- fp->fd = EOF;
- }
- int fseek(FILE *fp, long offset, int origin){
- int c,i = 1;
- FILE *temp = fp;
- c = lseek(fp->fd,offset,origin);
- if(offset<0)
- i *= -1;
- fp->cnt -= (c*i);
- return c;
- }
- FILE _iob[OPEN_MAX] = {
- {0 , (char *) 0, (char *) 0 , (struct flags){1,0,0,0,0}, 0},
- {0 , (char *) 0, (char *) 0 , (struct flags){0,1,0,0,0}, 1},
- {0 , (char *) 0, (char *) 0 , (struct flags){0,1,1,0,0}, 2}
- };
- int main(int argc, char *argv[]) {
- int c , a = 0;
- FILE *fp = fopen(argv[1],"r");
- fseek(fp,3,0);
- while(a++ < 3){
- c = getc(fp);
- putc(c,stdout);
- }
- fclose(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement