dup2.c 692 B

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