From 8eb13beb49495e289b9b7a824baa05d46328aa4f Mon Sep 17 00:00:00 2001 From: Lukas Mai Date: Mon, 1 Jul 2024 12:05:34 +0200 Subject: [PATCH] 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 --- pp_sys.c | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/pp_sys.c b/pp_sys.c index 951aea390f12..ead34bd81726 100644 --- a/pp_sys.c +++ b/pp_sys.c @@ -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 @@ -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 }