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