home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / unixlib / !UnixLib / src / unix / c / dev < prev    next >
Encoding:
Text File  |  1994-09-30  |  5.8 KB  |  375 lines

  1. static char sccs_id[] = "@(#) dev.c 2.0 " __DATE__ " HJR";
  2.  
  3. /* dev.c (c) Copyright 1990 H.Rogers */
  4.  
  5. #include <errno.h>
  6. #include <stdlib.h>
  7.  
  8. #include "fcntl.h"
  9.  
  10. #include "sys/unix.h"
  11. #include "sys/param.h"
  12. #include "sys/dev.h"
  13. #include "sys/os.h"
  14.  
  15. #define IGNORE(x) x = x
  16.  
  17. struct dev __dev[NDEV] =
  18. {
  19.   {__fsopen, __fsclose, __fsread, __fswrite, __fslseek, __fsioctl},
  20.   {__ttyopen, __ttyclose, __ttyread, __ttywrite, __ttylseek, __ttyioctl},
  21. {__pipeopen, __pipeclose, __piperead, __pipewrite, __pipelseek, __pipeioctl},
  22. {__nullopen, __nullclose, __nullread, __nullwrite, __nulllseek, __nullioctl}
  23. };
  24.  
  25. int
  26. __fsopen (char *file, int mode, struct file *f)
  27. {
  28.   register int oflag = f->oflag;
  29.   register int *r = f->r;
  30.   os_error *e;
  31.   int fd;
  32.  
  33.   file = __uname (file, oflag & (O_CREAT | O_OMASK));
  34.  
  35.   if (e = os_file (0x05, file, r))
  36.     {
  37.       __seterr (e);
  38.       return (-1);
  39.     }
  40.  
  41.   if (r[0])
  42.     {
  43.       if ((oflag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
  44.     {
  45.       errno = EEXIST;
  46.       return (-1);
  47.     }
  48.       if (oflag & O_CREAT)
  49.     oflag &= ~O_CREAT;
  50.       if (oflag & O_OMASK)
  51.     if (e = os_file (0x09, file, r))
  52.       {
  53.         __seterr (e);
  54.         return (-1);
  55.       }
  56.     }
  57.   else
  58.     {
  59.       if ((oflag & (O_RDONLY | O_CREAT)) == O_RDONLY)
  60.     {
  61.       errno = ENOENT;
  62.       return (-1);
  63.     }
  64.       if (oflag & O_OMASK)
  65.     oflag |= O_CREAT;
  66.     }
  67.  
  68.   if (oflag & O_CREAT)
  69.     {
  70.       r[2] = (oflag & O_BINARY) ? 0xffd : 0xfff;
  71.       r[4] = r[5] = 0;
  72.       if (e = os_file (0x0b, file, r))
  73.     {
  74.       __seterr (e);
  75.       return (-1);
  76.     }
  77.  
  78.       mode &= ~(__u->umask & 0777);
  79.  
  80.       r[5] = ((mode & 0400) >> 8) | ((mode & 0200) >> 6) |
  81.     ((mode & 0004) << 2) | ((mode & 0002) << 4);
  82.  
  83.       if (e = os_file (0x04, file, r))
  84.     {
  85.       __seterr (e);
  86.       return (-1);
  87.     }
  88.     }
  89.  
  90.   if (e = os_file (0x05, file, r))
  91.     {
  92.       __seterr (e);
  93.       return (-1);
  94.     }
  95.  
  96.   if (oflag & O_TRUNC)
  97.     e = os_fopen (0x80, file, &fd);
  98.   else
  99.     switch (oflag & O_OMASK)
  100.       {
  101.       case O_RDONLY:
  102.     e = os_fopen (0x40, file, &fd);
  103.     break;
  104.       case O_WRONLY:
  105.       case O_RDWR:
  106.     e = os_fopen (0xc0, file, &fd);
  107.     break;
  108.       default:
  109.     errno = EINVAL;
  110.     return (-1);
  111.     break;
  112.       }
  113.  
  114.   if (e)
  115.     {
  116.       __seterr (e);
  117.       return (-1);
  118.     }
  119.  
  120.   return (fd);
  121. }
  122.  
  123. int
  124. __fsclose (int fd, struct file *f)
  125. {
  126.   os_error *e;
  127.  
  128.   IGNORE (f);
  129.  
  130.   if (e = os_fclose (fd))
  131.     {
  132.       __seterr (e);
  133.       return (-1);
  134.     }
  135.  
  136.   return (0);
  137. }
  138.  
  139. int
  140. __fsread (int fd, register void *data, register int nbyte, struct file *f)
  141. {
  142.   int r[5];
  143.   os_error *e;
  144.  
  145.   IGNORE (f);
  146.  
  147.   if (e = os_fread (fd, data, nbyte, r))
  148.     {
  149.       __seterr (e);
  150.       return (-1);
  151.     }
  152.  
  153.   return (nbyte - r[3]);
  154. }
  155.  
  156. int
  157. __fswrite (int fd, register void *data, register int nbyte, struct file *f)
  158. {
  159.   int r[5];
  160.   os_error *e;
  161.  
  162.   IGNORE (f);
  163.  
  164.   if (e = os_fwrite (fd, data, nbyte, r))
  165.     {
  166.       __seterr (e);
  167.       return (-1);
  168.     }
  169.  
  170.   return (nbyte - r[3]);
  171. }
  172.  
  173. long
  174. __fslseek (int fd, long lpos, int whence, struct file *f)
  175. {
  176.   int r[3];
  177.   os_error *e;
  178.  
  179.   IGNORE (f);
  180.  
  181.   switch (whence)
  182.     {
  183.     case 0:
  184.       if (e = os_args (1, fd, (int) lpos, r))
  185.     {
  186.       __seterr (e);
  187.       return (-1);
  188.     }
  189.       break;
  190.     case 1:
  191.       if (e = os_args (0, fd, 0, r))
  192.     {
  193.       __seterr (e);
  194.       return (-1);
  195.     }
  196.       if (e = os_args (1, fd, r[2] + (int) lpos, r))
  197.     {
  198.       __seterr (e);
  199.       return (-1);
  200.     }
  201.       break;
  202.     case 2:
  203.       if (e = os_args (2, fd, 0, r))
  204.     {
  205.       __seterr (e);
  206.       return (-1);
  207.     }
  208.       if (e = os_args (1, fd, r[2] + (int) lpos, r))
  209.     {
  210.       __seterr (e);
  211.       return (-1);
  212.     }
  213.       break;
  214.     default:
  215.       errno = EINVAL;
  216.       return (-1);
  217.       break;
  218.     }
  219.  
  220.   return ((long) r[2]);
  221. }
  222.  
  223. int
  224. __fsioctl (int fd, register int request, void *arg, struct file *f)
  225. {
  226.   IGNORE (fd);
  227.   IGNORE (request);
  228.   IGNORE (arg);
  229.   IGNORE (f);
  230.   errno = EINVAL;
  231.   return (-1);
  232. }
  233.  
  234. struct pipe *__pipe;
  235.  
  236. int
  237. __pipeopen (char *file, int mode, struct file *f)
  238. {
  239.   return (__fsopen (file, mode, f));
  240. }
  241.  
  242. int
  243. __pipeclose (int fd, struct file *f)
  244. {
  245.   struct pipe *pi = __pipe, *pi_ = 0;
  246.  
  247.   while (pi)
  248.     {
  249.       if (pi->p[0] == f || pi->p[1] == f)
  250.     break;
  251.       pi_ = pi;
  252.       pi = pi->next;
  253.     }
  254.   if (!pi)
  255.     {
  256.       errno = EBADF;
  257.       return (-1);
  258.     }
  259.   if (pi_)
  260.     pi_->next = pi->next;
  261.   else
  262.     __pipe = pi->next;
  263.   __fsclose (fd, f);
  264.   {
  265.     os_error *e;
  266.  
  267.     if (e = os_fsctrl (27, __uname (pi->file, 0), 0, 0642))
  268.       {
  269.     __seterr (e);
  270.     return (-1);
  271.       }
  272.   }
  273.   free (pi->file);
  274.   free (pi);
  275.   return (0);
  276. }
  277.  
  278. int
  279. __piperead (int fd, void *data, int nbyte, struct file *f)
  280. {
  281.   if (f->oflag & O_PIPE)
  282.     {
  283.       f->oflag &= ~O_PIPE;
  284.       __fslseek (fd, 0, 0, f);
  285.     }
  286.   return (__fsread (fd, data, nbyte, f));
  287. }
  288.  
  289. int
  290. __pipewrite (int fd, void *data, int nbyte, struct file *f)
  291. {
  292.   if (f->oflag & O_PIPE)
  293.     return (__fswrite (fd, data, nbyte, f));
  294.   else
  295.     {
  296.       errno = EPIPE;
  297.       return (-1);
  298.     }
  299. }
  300.  
  301. long
  302. __pipelseek (int fd, long lpos, int whence, struct file *f)
  303. {
  304.   IGNORE (fd);
  305.   IGNORE (lpos);
  306.   IGNORE (whence);
  307.   IGNORE (f);
  308.   errno = ESPIPE;
  309.   return (-1);
  310. }
  311.  
  312. int
  313. __pipeioctl (int fd, register int request, void *arg, struct file *f)
  314. {
  315.   IGNORE (fd);
  316.   IGNORE (request);
  317.   IGNORE (arg);
  318.   IGNORE (f);
  319.   errno = EINVAL;
  320.   return (-1);
  321. }
  322.  
  323. int
  324. __nullopen (char *file, int mode, struct file *f)
  325. {
  326.   IGNORE (file);
  327.   IGNORE (mode);
  328.   IGNORE (f);
  329.   return (0);
  330. }
  331. int
  332. __nullclose (int fd, struct file *f)
  333. {
  334.   IGNORE (fd);
  335.   IGNORE (f);
  336.   return (0);
  337. }
  338. int
  339. __nullread (int fd, void *data, int nbyte, struct file *f)
  340. {
  341.   IGNORE (fd);
  342.   IGNORE (data);
  343.   IGNORE (nbyte);
  344.   IGNORE (f);
  345.   return (0);
  346. }
  347. int
  348. __nullwrite (int fd, void *data, int nbyte, struct file *f)
  349. {
  350.   IGNORE (fd);
  351.   IGNORE (data);
  352.   IGNORE (f);
  353.   return (nbyte);
  354. }
  355. long
  356. __nulllseek (int fd, long lpos, int whence, struct file *f)
  357. {
  358.   IGNORE (fd);
  359.   IGNORE (lpos);
  360.   IGNORE (whence);
  361.   IGNORE (f);
  362.   errno = ESPIPE;
  363.   return (-1);
  364. }
  365. int
  366. __nullioctl (int fd, int request, void *arg, struct file *f)
  367. {
  368.   IGNORE (fd);
  369.   IGNORE (request);
  370.   IGNORE (arg);
  371.   IGNORE (f);
  372.   errno = EINVAL;
  373.   return (-1);
  374. }
  375.