home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / dup / part01 / dup.c next >
Encoding:
C/C++ Source or Header  |  1993-01-29  |  2.7 KB  |  101 lines

  1. /*
  2.  * dup.c - /dev/fd driver
  3.  *
  4.  * open("/dev/fd/N") is (theoretically) equivalent to dup(N).
  5.  *
  6.  *
  7.  * The Ditto version of this driver was ported to SCO XENIX by
  8.  * David J. Fiander, with several enhancements added on the way.
  9.  * This version is _not_ in the public domain.  The following
  10.  * copyright notice now applies.
  11.  *
  12.  * Copyright 1992, David J.  Fiander.  Free distribution of
  13.  * unmodified versions of this source is permitted.
  14.  *
  15.  * Redistribution and use in source and binary forms are permitted
  16.  * provided that the above copyright notice is duplicated in all
  17.  * such forms and that any documentation related to such
  18.  * distribution and use acknowlege that the software was
  19.  * developed by David J. Fiander.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  22.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  24.  * PURPOSE.
  25.  *
  26.  * (Hey, it worked for Mark Colburn, didn't it?)
  27.  *
  28.  * This program was written by me, Mike "Ford" Ditto, and
  29.  * I hereby release it into the public domain in the interest
  30.  * of promoting the development of free, quality software
  31.  * for the hackers and users of the world.
  32.  *
  33.  * Feel free to use, copy, modify, improve, and redistribute
  34.  * this program, but please keep in mind the spirit of this
  35.  * contribution; always provide source, and always allow
  36.  * free redistribution (shareware is fine with me).
  37.  *
  38.  *                -=] Ford [=-
  39.  */
  40.  
  41. #define KERNEL        1
  42.  
  43. #include "sys/types.h"
  44. #include "sys/param.h"
  45. #include "sys/sysmacros.h"
  46. #include "sys/dir.h"
  47. #include "sys/signal.h"
  48. #include "sys/page.h"
  49. #include "sys/seg.h"
  50. #include "sys/user.h"
  51. #include "sys/file.h"
  52. #include "sys/systm.h"
  53. #include "sys/errno.h"
  54.  
  55. void dupopen(dev, flag, id)
  56. register dev_t    dev;
  57. int        flag;
  58. int        id;
  59. {
  60.     register struct file *fp;
  61.     register int fd;
  62.  
  63.     /* First, validate the existing fd to be duped */
  64.     if (!(fp = getf(minor(dev))))
  65.     return;
  66.  
  67.     /* Make sure we are not about to return the same fd that was requested */
  68.     fd = u.u_rval1;
  69.     if (minor(dev) == fd)
  70.     {
  71.     u.u_error = EBADF;
  72.     return;
  73.     }
  74.  
  75.     /*
  76.      * Make sure that the flags requested are a subset of the
  77.      * flags already open. That is make sure that the
  78.      * intersection of the requested flags and the existing flags
  79.      * == the requested flags
  80.      */
  81.     if (((fp->f_flag & (FREAD|FWRITE)) & flag) != (flag & (FREAD|FWRITE)))
  82.     {
  83.     u.u_error = EBADF;
  84.     return;
  85.     }
  86.     
  87.     /* Next, find (and undo) the half-open new fd */
  88.     if (fp = getf(fd)) 
  89.     {
  90.     plock(fp->f_inode);
  91.     iput(fp->f_inode);
  92.     if (--fp->f_count <= 0)
  93.     {
  94.         fp->f_inode = NULL;
  95.     }
  96.     }
  97.  
  98.     /* Finally, dup the existing fd */
  99.     ++(u.u_ofile[fd] = u.u_ofile[minor(dev)])->f_count;
  100. }
  101.