home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include "sys/file.h"
-
- int __file_debug = (0!=0);
- char __file_lastfname[256] = { '\0' };
- FILE *__file_lastfile = NULL;
-
- int fileno(FILE *f) {
- if (f == stdin) return(0);
- if (f == stdout) return(1);
- if (f == stderr) return(2);
- return((int)f);
- }
-
- static int fdtofile(int fd) {
- if (fd == 0) return(stdin);
- if (fd == 1) return(stdout);
- if (fd == 2) return(stderr);
- return((FILE *)fd);
- }
-
- int unlink(char *s) {
- return(remove(s));
- }
-
- int fdopen(int fd, char *mode) {
- if (fd != (int)__file_lastfile) return(-1); /* Can't remember this one */
- fclose((FILE *)fd);
- fd = (int)fopen(__file_lastfname, mode);
- if (fd == 0) return(-1);
- return((int)fd);
- }
-
- int creat(char *fname, int mode) {
- FILE *f;
- if (__file_debug) {
- fprintf(stderr, "open: creat(\"%s\", %d)\n", fname, mode);
- }
- f = fopen(fname, "w");
- if (f == NULL) return(-1);
- strcpy(__file_lastfname, fname);
- __file_lastfile = f;
- return((int)f);
- }
-
- long lseek(int fd, long where, int whence) {
- int rc;
- rc = fseek(fdtofile(fd), (long)where, whence);
- if (rc != 0) return(-1L);
- return(ftell((FILE *)fd));
- }
-
- int write(int fd, char *buf, unsigned int bytes) {
- int rc;
- rc = fwrite(buf, bytes, 1, fdtofile(fd));
- if (rc != 1) return(-1);
- return(bytes);
- }
-
- int read(int fd, char *buf, int bytes) {
- int rc;
- rc = fread(buf, 1, (size_t)bytes, fdtofile(fd));
- if (rc == EOF) return(-1);
- return(rc);
- }
-
-
- int open(char *fname, int mode, ...) {
- char *fmode;
- FILE *f;
-
- if (__file_debug) {
- fprintf(stderr, "open: open(\"%s\", 0x%2x -> ", fname, mode);
- if ((mode & O_RAW) != 0) fprintf(stderr, " O_RAW");
- if ((mode & O_NEW) != 0) fprintf(stderr, " O_NEW");
- if ((mode & O_CREAT) != 0) fprintf(stderr, " O_CREAT");
- if ((mode & O_RDWR) != 0) fprintf(stderr, " O_RDWR");
- if ((mode & O_RDONLY) != 0) fprintf(stderr, " O_RDONLY");
- fprintf(stderr, ")\n");
- }
- if ((mode & O_CREAT) != 0) {
- if (__file_debug) {
- fprintf(stderr, "open: fclose(fopen(\"%s\", \"ab+\"))\n", fname);
- }
- fclose(fopen(fname, "ab+"));
- }
- if ((mode & O_RDONLY) != 0) {
- if ((mode & O_RAW) != 0) {
- fmode = "rb";
- } else {
- fmode = "r";
- }
- } else {
- if ((mode & O_RDWR) != 0) {
- if ((mode & O_NEW) != 0) {
- if ((mode & O_RAW) != 0) {
- fmode = "wb+";
- } else {
- fmode = "w+";
- }
- } else {
- if ((mode & O_RAW) != 0) {
- fmode = "rb+";
- } else {
- fmode = "r+";
- }
- }
- } else {
- if ((mode & O_RAW) != 0) {
- fmode = "wb";
- } else {
- fmode = "w";
- }
- }
- }
- f = fopen(fname, fmode);
- if (__file_debug) {
- fprintf(stderr, "open: fopen(\"%s\",\"%s\") -> %8lx\n",
- fname, fmode, (long)f);
- }
- if (f == NULL) return(-1);
- strcpy(__file_lastfname, fname);
- __file_lastfile = f;
- return((int)f);
- }
-
- int close(int fd) {
- if (fd == (int)__file_lastfile) { /* Should extend this to remember all the others... */
- __file_lastfname[0] = '\0';
- __file_lastfile = NULL;
- }
- if (fclose(fdtofile(fd)) == EOF) return(-1);
- return(0);
- }
-
- extern int chmod(char *f, int mode) {
- return(0); /* This actually has some meaning on
- the archie, so could be emulated */
- }
-
- extern int chown(char *f, int owner, int group) {
- return(0);
- }
-
-
-