dup.c 339 B

123456789101112131415161718192021222324
  1. #include "lib.h"
  2. #include <unistd.h>
  3. #include <errno.h>
  4. int
  5. dup(int oldd)
  6. {
  7. return fcntl(oldd, F_DUPFD, 0);
  8. }
  9. int
  10. dup2(int oldd, int newd)
  11. {
  12. int n;
  13. if(newd < 0 || newd >= OPEN_MAX){
  14. errno = EBADF;
  15. return -1;
  16. }
  17. if(oldd == newd && _fdinfo[newd].flags&FD_ISOPEN)
  18. return newd;
  19. close(newd);
  20. return fcntl(oldd, F_DUPFD, newd);
  21. }