Skip to content

Commit

Permalink
pp_chdir: simplify control flow in fchdir case
Browse files Browse the repository at this point in the history
The result is (in my opinion) easier to read. It also lets us get rid
of:

- two levels of indentation
- three `goto`s and a label
- one duplicate instance of `PUSHs(boolSV(fchdir(...) >= 0))`
- one `#ifdef ... #endif` section
  • Loading branch information
mauke committed Jul 4, 2024
1 parent a518f71 commit 8eb13be
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions pp_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -3950,23 +3950,17 @@ PP_wrapped(pp_chdir, MAXARG, 0)
if (gv) {
#ifdef HAS_FCHDIR
IO* const io = GvIO(gv);
if (io) {
if (IoDIRP(io)) {
PUSHs(boolSV(fchdir(my_dirfd(IoDIRP(io))) >= 0));
} else if (IoIFP(io)) {
int fd = PerlIO_fileno(IoIFP(io));
if (fd < 0) {
goto nuts;
}
PUSHs(boolSV(fchdir(fd) >= 0));
}
else {
goto nuts;
}
} else {
goto nuts;
const int fd =
!io ? -1 :
IoDIRP(io) ? my_dirfd(IoDIRP(io)) :
IoIFP(io) ? PerlIO_fileno(IoIFP(io)) :
-1;
if (fd < 0) {
report_evil_fh(gv);
SETERRNO(EBADF,RMS_IFI);
RETPUSHNO;
}

PUSHs(boolSV(fchdir(fd) >= 0));
#else
DIE(aTHX_ PL_no_func, "fchdir");
#endif
Expand All @@ -3979,13 +3973,6 @@ PP_wrapped(pp_chdir, MAXARG, 0)
hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
#endif
RETURN;

#ifdef HAS_FCHDIR
nuts:
report_evil_fh(gv);
SETERRNO(EBADF,RMS_IFI);
RETPUSHNO;
#endif
}


Expand Down

0 comments on commit 8eb13be

Please sign in to comment.