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

  1. static char sccs_id[] = "@(#) dup2.c 1.2 " __DATE__ " HJR";
  2.  
  3. /* dup2.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. dup2 (int fd1, int fd2)
  14. {
  15.   register struct file *f1, *f2;
  16.  
  17.   if (BADF (fd1) || (fd2 >= MAXFD) || (fd2 < 0))
  18.     {
  19.       errno = EBADF;
  20.       return (-1);
  21.     }
  22.  
  23.   if (fd1 == fd2)
  24.     return (0);
  25.  
  26.   f1 = __u->file + fd1;
  27.   f2 = __u->file + fd2;
  28.  
  29.   if (f2->dup)
  30.     close (fd2);
  31.  
  32.   memcpy (f2, f1, sizeof (struct file));
  33.  
  34.   f2->dup = f1;
  35.  
  36.   while (f1->dup != f2->dup)
  37.     f1 = f1->dup;
  38.  
  39.   f1->dup = f2;
  40.  
  41.   return (0);
  42. }
  43.