dup2.c 717 B

1234567891011121314151617181920212223242526272829303132
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * dup2() for uClibc
  4. *
  5. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  6. *
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. #include <sys/syscall.h>
  10. #include <unistd.h>
  11. #if defined __NR_dup3 && !defined __NR_dup2
  12. # include <fcntl.h>
  13. extern int __libc_fcntl (int fd, int cmd, ...);
  14. libc_hidden_proto(__libc_fcntl);
  15. int dup2(int old, int newfd)
  16. {
  17. /*
  18. * Check if old fd is valid before we try
  19. * to ducplicate it. Return it if valid
  20. * or EBADF otherwise
  21. */
  22. if (old == newfd)
  23. return fcntl(old, F_GETFL, 0) < 0 ? -1 : newfd;
  24. return dup3(old, newfd, 0);
  25. }
  26. #else
  27. _syscall2(int, dup2, int, oldfd, int, newfd)
  28. #endif
  29. libc_hidden_def(dup2)