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

  1. static char sccs_id[] = "@(#) dup.c 1.2 " __DATE__ " HJR";
  2.  
  3. /* dup.c (c) Copyright 1990 H.Rogers */
  4.  
  5. #include <string.h>
  6. #include <errno.h>
  7.  
  8. #include "sys/types.h"
  9. #include "sys/unix.h"
  10. #include "sys/os.h"
  11.  
  12. int
  13. dup (int fd)
  14. {
  15.   register struct file *f1, *f2;
  16.   register int i;
  17.  
  18.   if (BADF (fd))
  19.     {
  20.       errno = EBADF;
  21.       return (-1);
  22.     }
  23.  
  24.   if ((i = __fdalloc ()) < 0)
  25.     return (-1);
  26.  
  27.   f1 = __u->file + i;
  28.   f2 = __u->file + fd;
  29.  
  30.   memcpy (f1, f2, sizeof (struct file));
  31.  
  32.   f1->dup = f2;
  33.  
  34.   while (f2->dup != f1->dup)
  35.     f2 = f2->dup;
  36.  
  37.   f2->dup = f1;
  38.  
  39.   return (i);
  40. }
  41.