home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/dev,v $
- * $Date: 1996/10/30 21:59:01 $
- * $Revision: 1.2 $
- * $State: Exp $
- * $Author: unixlib $
- *
- * $Log: dev,v $
- * Revision 1.2 1996/10/30 21:59:01 unixlib
- * Massive changes made by Nick Burret and Peter Burwood.
- *
- * Revision 1.1 1996/04/19 21:35:27 simon
- * Initial revision
- *
- ***************************************************************************/
-
- static const char rcs_id[] = "$Id: dev,v 1.2 1996/10/30 21:59:01 unixlib Exp $";
-
- #include <errno.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/unix.h>
- #include <sys/param.h>
- #include <sys/dev.h>
- #include <sys/os.h>
- #include <sys/netdb.h>
-
- #define IGNORE(x) x = x
-
- struct dev __dev[NDEV] =
- {
- {__fsopen, __fsclose, __fsread, __fswrite, __fslseek, __fsioctl},
- {__ttyopen, __ttyclose, __ttyread, __ttywrite, __ttylseek, __ttyioctl},
- {__pipeopen, __pipeclose, __piperead, __pipewrite, __pipelseek, __pipeioctl},
- {__nullopen, __nullclose, __nullread, __nullwrite, __nulllseek, __nullioctl},
- #ifdef DEV_SOCKET
- {__sockopen, __sockclose, __sockread, __sockwrite, __socklseek, __sockioctl}
- #endif
- };
-
- int
- __fsopen (char *file, int mode, struct file *f)
-
- {
- register int oflag = f->oflag;
- register int *r = f->r;
- _kernel_oserror *e;
- int fd;
-
- file = __uname (file, oflag & (O_CREAT | O_OMASK));
-
- if (e = os_file (0x05, file, r))
- {
- __seterr (e);
- return (-1);
- }
-
- if (r[0])
- {
- if ((oflag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
- {
- errno = EEXIST;
- return (-1);
- }
- if (oflag & O_CREAT)
- oflag &= ~O_CREAT;
- if (oflag & O_OMASK)
- if (e = os_file (0x09, file, r))
- {
- __seterr (e);
- return (-1);
- }
- }
- else
- {
- if ((oflag & (O_RDONLY | O_CREAT)) == O_RDONLY)
- {
- errno = ENOENT;
- return (-1);
- }
- if (oflag & O_OMASK)
- oflag |= O_CREAT;
- }
-
- if (oflag & O_CREAT)
- {
- r[2] = (oflag & O_BINARY) ? 0xffd : 0xfff;
- r[4] = r[5] = 0;
- if (e = os_file (0x0b, file, r))
- {
- __seterr (e);
- return (-1);
- }
-
- mode &= ~(__u->umask & 0777);
-
- r[5] = ((mode & 0400) >> 8) | ((mode & 0200) >> 6) |
- ((mode & 0004) << 2) | ((mode & 0002) << 4);
-
- if (e = os_file (0x04, file, r))
- {
- __seterr (e);
- return (-1);
- }
- }
-
- if (e = os_file (0x05, file, r))
- {
- __seterr (e);
- return (-1);
- }
-
- if (oflag & O_TRUNC)
- e = os_fopen (0x80, file, &fd);
- else
- switch (oflag & O_OMASK)
- {
- case O_RDONLY:
- e = os_fopen (0x40, file, &fd);
- break;
- case O_WRONLY:
- case O_RDWR:
- e = os_fopen (0xc0, file, &fd);
- break;
- default:
- errno = EINVAL;
- return (-1);
- break;
- }
-
- if (e)
- {
- __seterr (e);
- return (-1);
- }
-
- return (fd);
- }
-
- int
- __fsclose (int fd, struct file *f)
-
- {
- _kernel_oserror *e;
-
- IGNORE (f);
-
- if (e = os_fclose (fd))
- {
- __seterr (e);
- return (-1);
- }
-
- return (0);
- }
-
- int
- __fsread (int fd, register void *data, register int nbyte, struct file *f)
-
- {
- int r[5];
- _kernel_oserror *e;
-
- IGNORE (f);
-
- if (e = os_fread (fd, data, nbyte, r))
- {
- __seterr (e);
- return (-1);
- }
-
- return (nbyte - r[3]);
- }
-
- int
- __fswrite (int fd, register void *data, register int nbyte, struct file *f)
-
- {
- int r[5];
- _kernel_oserror *e;
-
- IGNORE (f);
-
- if (e = os_fwrite (fd, data, nbyte, r))
- {
- __seterr (e);
- return (-1);
- }
-
- return (nbyte - r[3]);
- }
-
- __off_t
- __fslseek (int fd, __off_t lpos, int whence, struct file *f)
-
- {
- int r[3];
- _kernel_oserror *e;
-
- IGNORE (f);
-
- switch (whence)
- {
- case 0:
- if (e = os_args (1, fd, (int) lpos, r))
- {
- __seterr (e);
- return (-1);
- }
- break;
- case 1:
- if (e = os_args (0, fd, 0, r))
- {
- __seterr (e);
- return (-1);
- }
- if (e = os_args (1, fd, r[2] + (int) lpos, r))
- {
- __seterr (e);
- return (-1);
- }
- break;
- case 2:
- if (e = os_args (2, fd, 0, r))
- {
- __seterr (e);
- return (-1);
- }
- if (e = os_args (1, fd, r[2] + (int) lpos, r))
- {
- __seterr (e);
- return (-1);
- }
- break;
- default:
- errno = EINVAL;
- return (-1);
- break;
- }
-
- return ((long) r[2]);
- }
-
- int
- __fsioctl (int fd, register int request, void *arg, struct file *f)
-
- {
- IGNORE (fd);
- IGNORE (request);
- IGNORE (arg);
- IGNORE (f);
- errno = EINVAL;
- return (-1);
- }
-
- struct pipe *__pipe = NULL;
-
- int
- __pipeopen (char *file, int mode, struct file *f)
-
- {
- return (__fsopen (file, mode, f));
- }
-
- int
- __pipeclose (int fd, struct file *f)
-
- {
- struct pipe *pi = __pipe, *pi_ = 0;
-
- while (pi)
- {
- if (pi->p[0] == f || pi->p[1] == f)
- break;
- pi_ = pi;
- pi = pi->next;
- }
- if (!pi)
- {
- errno = EBADF;
- return (-1);
- }
- if (pi_)
- pi_->next = pi->next;
- else
- __pipe = pi->next;
- __fsclose (fd, f);
- {
- _kernel_oserror *e;
-
- if (e = os_fsctrl (27, __uname (pi->file, 0), 0, 0642))
- {
- __seterr (e);
- return (-1);
- }
- }
- free (pi->file);
- free (pi);
- return (0);
- }
-
- int
- __piperead (int fd, void *data, int nbyte, struct file *f)
-
- {
- if (f->oflag & O_PIPE)
- {
- f->oflag &= ~O_PIPE;
- __fslseek (fd, 0, 0, f);
- }
- return (__fsread (fd, data, nbyte, f));
- }
-
- int
- __pipewrite (int fd, void *data, int nbyte, struct file *f)
-
- {
- if (f->oflag & O_PIPE)
- return (__fswrite (fd, data, nbyte, f));
- else
- {
- errno = EPIPE;
- return (-1);
- }
- }
-
- __off_t
- __pipelseek (int fd, __off_t lpos, int whence, struct file *f)
-
- {
- IGNORE (fd);
- IGNORE (lpos);
- IGNORE (whence);
- IGNORE (f);
- errno = ESPIPE;
- return (-1);
- }
-
- int
- __pipeioctl (int fd, register int request, void *arg, struct file *f)
-
- {
- IGNORE (fd);
- IGNORE (request);
- IGNORE (arg);
- IGNORE (f);
- errno = EINVAL;
- return (-1);
- }
-
- int
- __nullopen (char *file, int mode, struct file *f)
-
- {
- IGNORE (file);
- IGNORE (mode);
- IGNORE (f);
- return (0);
- }
-
- int
- __nullclose (int fd, struct file *f)
-
- {
- IGNORE (fd);
- IGNORE (f);
- return (0);
- }
-
- int
- __nullread (int fd, void *data, int nbyte, struct file *f)
-
- {
- IGNORE (fd);
- IGNORE (data);
- IGNORE (nbyte);
- IGNORE (f);
- return (0);
- }
-
- int
- __nullwrite (int fd, void *data, int nbyte, struct file *f)
-
- {
- IGNORE (fd);
- IGNORE (data);
- IGNORE (f);
- return (nbyte);
- }
-
- __off_t
- __nulllseek (int fd, __off_t lpos, int whence, struct file *f)
-
- {
- IGNORE (fd);
- IGNORE (lpos);
- IGNORE (whence);
- IGNORE (f);
- errno = ESPIPE;
- return (-1);
- }
-
- int
- __nullioctl (int fd, int request, void *arg, struct file *f)
-
- {
- IGNORE (fd);
- IGNORE (request);
- IGNORE (arg);
- IGNORE (f);
- errno = EINVAL;
- return (-1);
- }
-
- #ifdef DEV_SOCKET
-
- /* We optionally include the network support code - mainly because I cannot
- * be bothered to recompile it all the time.
- */
-
- int
- __sockopen (char *file, int mode, struct file *f)
- {
- IGNORE (file);
- IGNORE (mode);
- IGNORE (f);
- errno = EOPNOTSUPP;
- return (-1);
- }
-
- int
- __sockclose (int fd, struct file *f)
- {
- IGNORE (f);
- return (_sclose (fd));
- }
-
- int
- __sockread (int fd, void *data, int nbyte, struct file *f)
- {
- IGNORE (f);
- return (_sread (fd, data, nbyte));
- }
-
- int
- __sockwrite (int fd, void *data, int nbyte, struct file *f)
- {
- IGNORE (f);
- return (_swrite (fd, data, nbyte));
- }
-
- __off_t
- __socklseek (int fd, __off_t lpos, int whence, struct file *f)
- {
- IGNORE (fd);
- IGNORE (lpos);
- IGNORE (whence);
- IGNORE (f);
- errno = EOPNOTSUPP;
- return (-1);
- }
-
- int
- __sockioctl (int fd, int request, void *arg, struct file *f)
- {
- IGNORE (f);
- return (_sioctl (fd, request, arg));
- }
- #endif
-