From fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 8 Jul 2015 20:46:52 -0400 Subject: Initial commit --- sys/libc/fdopen.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 sys/libc/fdopen.c (limited to 'sys/libc/fdopen.c') diff --git a/sys/libc/fdopen.c b/sys/libc/fdopen.c new file mode 100644 index 00000000..c9e2bc9f --- /dev/null +++ b/sys/libc/fdopen.c @@ -0,0 +1,76 @@ +/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc. +*/ + +#define import_spp +#define import_libc +#define import_stdio +#define import_fset +#include + + +extern int c_fstati(); + + +/* FDOPEN -- Reopen a file for i/o with the STDIO package, after the file +** as already been opened by FIO. It is an error if the access modes are +** incompatible. +*/ +FILE * +fdopen ( + XINT fd, /* FIO file descriptor */ + char *mode /* STDIO access mode */ +) +{ + register int fio_mode = c_fstati (fd, F_MODE); + register int fio_type = c_fstati (fd, F_TYPE); + + + /* Verify file access mode. No mode checking is performed for the + * special file types. + */ + if (fio_type == TEXT_FILE || fio_type == BINARY_FILE) + switch (mode[0]) { + case 'r': + if (fio_mode != READ_ONLY && fio_mode != READ_WRITE) + return (NULL); + break; + + case 'w': + switch (fio_mode) { + case NEW_FILE: + case READ_WRITE: + case WRITE_ONLY: + break; + default: + return (NULL); + } + break; + + case 'a': + if (fio_mode != APPEND && fio_mode != NEW_FILE) + return (NULL); + break; + + default: + return (NULL); + } + + /* Verify file type. No checking is performed if no type is given. + */ + switch (mode[1]) { + case EOS: + break; + case 't': + if (fio_type != TEXT_FILE) + return (NULL); + break; + case 'b': + if (fio_type != BINARY_FILE) + return (NULL); + break; + default: + return (NULL); + } + + return (FDTOFP(fd)); +} -- cgit