Skip to content

Commit 8eb13be

Browse files
committed
pp_chdir: simplify control flow in fchdir case
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
1 parent a518f71 commit 8eb13be

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

pp_sys.c

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3950,23 +3950,17 @@ PP_wrapped(pp_chdir, MAXARG, 0)
39503950
if (gv) {
39513951
#ifdef HAS_FCHDIR
39523952
IO* const io = GvIO(gv);
3953-
if (io) {
3954-
if (IoDIRP(io)) {
3955-
PUSHs(boolSV(fchdir(my_dirfd(IoDIRP(io))) >= 0));
3956-
} else if (IoIFP(io)) {
3957-
int fd = PerlIO_fileno(IoIFP(io));
3958-
if (fd < 0) {
3959-
goto nuts;
3960-
}
3961-
PUSHs(boolSV(fchdir(fd) >= 0));
3962-
}
3963-
else {
3964-
goto nuts;
3965-
}
3966-
} else {
3967-
goto nuts;
3953+
const int fd =
3954+
!io ? -1 :
3955+
IoDIRP(io) ? my_dirfd(IoDIRP(io)) :
3956+
IoIFP(io) ? PerlIO_fileno(IoIFP(io)) :
3957+
-1;
3958+
if (fd < 0) {
3959+
report_evil_fh(gv);
3960+
SETERRNO(EBADF,RMS_IFI);
3961+
RETPUSHNO;
39683962
}
3969-
3963+
PUSHs(boolSV(fchdir(fd) >= 0));
39703964
#else
39713965
DIE(aTHX_ PL_no_func, "fchdir");
39723966
#endif
@@ -3979,13 +3973,6 @@ PP_wrapped(pp_chdir, MAXARG, 0)
39793973
hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
39803974
#endif
39813975
RETURN;
3982-
3983-
#ifdef HAS_FCHDIR
3984-
nuts:
3985-
report_evil_fh(gv);
3986-
SETERRNO(EBADF,RMS_IFI);
3987-
RETPUSHNO;
3988-
#endif
39893976
}
39903977

39913978

0 commit comments

Comments
 (0)