[9] | 1 | /* zio.c (simulation of non-standard low-level i/o functions) */ |
---|
| 2 | |
---|
| 3 | /* Written by Andrew Makhorin <mao@gnu.org>, April 2011 |
---|
| 4 | * For conditions of distribution and use, see copyright notice in |
---|
| 5 | * zlib.h */ |
---|
| 6 | |
---|
| 7 | /* (reserved for copyright notice) */ |
---|
| 8 | |
---|
| 9 | #include <assert.h> |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include "zio.h" |
---|
| 12 | |
---|
| 13 | static FILE *file[FOPEN_MAX]; |
---|
| 14 | static int initialized = 0; |
---|
| 15 | |
---|
| 16 | static void initialize(void) |
---|
| 17 | { int fd; |
---|
| 18 | assert(!initialized); |
---|
| 19 | file[0] = stdin; |
---|
| 20 | file[1] = stdout; |
---|
| 21 | file[2] = stderr; |
---|
| 22 | for (fd = 3; fd < FOPEN_MAX; fd++) |
---|
| 23 | file[fd] = NULL; |
---|
| 24 | initialized = 1; |
---|
| 25 | return; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | int open(const char *path, int oflag, ...) |
---|
| 29 | { FILE *fp; |
---|
| 30 | int fd; |
---|
| 31 | if (!initialized) initialize(); |
---|
| 32 | /* see file gzlib.c, function gz_open */ |
---|
| 33 | if (oflag == O_RDONLY) |
---|
| 34 | fp = fopen(path, "rb"); |
---|
| 35 | else if (oflag == (O_WRONLY | O_CREAT | O_TRUNC)) |
---|
| 36 | fp = fopen(path, "wb"); |
---|
| 37 | else if (oflag == (O_WRONLY | O_CREAT | O_APPEND)) |
---|
| 38 | fp = fopen(path, "ab"); |
---|
| 39 | else |
---|
| 40 | assert(oflag != oflag); |
---|
| 41 | if (fp == NULL) |
---|
| 42 | return -1; |
---|
| 43 | for (fd = 0; fd < FOPEN_MAX; fd++) |
---|
| 44 | if (file[fd] == NULL) break; |
---|
| 45 | assert(fd < FOPEN_MAX); |
---|
| 46 | file[fd] = fp; |
---|
| 47 | return fd; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | long read(int fd, void *buf, unsigned long nbyte) |
---|
| 51 | { unsigned long count; |
---|
| 52 | if (!initialized) initialize(); |
---|
| 53 | assert(0 <= fd && fd < FOPEN_MAX); |
---|
| 54 | assert(file[fd] != NULL); |
---|
| 55 | count = fread(buf, 1, nbyte, file[fd]); |
---|
| 56 | if (ferror(file[fd])) |
---|
| 57 | return -1; |
---|
| 58 | return count; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | long write(int fd, const void *buf, unsigned long nbyte) |
---|
| 62 | { unsigned long count; |
---|
| 63 | if (!initialized) initialize(); |
---|
| 64 | assert(0 <= fd && fd < FOPEN_MAX); |
---|
| 65 | assert(file[fd] != NULL); |
---|
| 66 | count = fwrite(buf, 1, nbyte, file[fd]); |
---|
| 67 | if (count != nbyte) |
---|
| 68 | return -1; |
---|
| 69 | if (fflush(file[fd]) != 0) |
---|
| 70 | return -1; |
---|
| 71 | return count; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | long lseek(int fd, long offset, int whence) |
---|
| 75 | { if (!initialized) initialize(); |
---|
| 76 | assert(0 <= fd && fd < FOPEN_MAX); |
---|
| 77 | assert(file[fd] != NULL); |
---|
| 78 | if (fseek(file[fd], offset, whence) != 0) |
---|
| 79 | return -1; |
---|
| 80 | return ftell(file[fd]); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | int close(int fd) |
---|
| 84 | { if (!initialized) initialize(); |
---|
| 85 | assert(0 <= fd && fd < FOPEN_MAX); |
---|
| 86 | assert(file[fd] != NULL); |
---|
| 87 | fclose(file[fd]); |
---|
| 88 | file[fd] = NULL; |
---|
| 89 | return 0; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /* eof */ |
---|