Skip to content

Commit 4ee3916

Browse files
Chip SalzenbergSteve Hay
Chip Salzenberg
authored and
Steve Hay
committed
standardize save/restore of errno & vaxc$errno
Message-ID: <[email protected]> p4raw-id: //depot/perl@35018
1 parent 33839f2 commit 4ee3916

File tree

8 files changed

+43
-40
lines changed

8 files changed

+43
-40
lines changed

doio.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,9 @@ Perl_do_openn(pTHX_ GV *gv, register const char *oname, I32 len, int as_raw,
663663
}
664664
#if defined(HAS_FCNTL) && defined(F_SETFD)
665665
if (fd >= 0) {
666-
const int save_errno = errno;
666+
dSAVE_ERRNO;
667667
fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
668-
errno = save_errno;
668+
RESTORE_ERRNO;
669669
}
670670
#endif
671671
IoIFP(io) = fp;
@@ -1014,14 +1014,14 @@ Perl_do_eof(pTHX_ GV *gv)
10141014

10151015
{
10161016
/* getc and ungetc can stomp on errno */
1017-
const int saverrno = errno;
1017+
dSAVE_ERRNO;
10181018
const int ch = PerlIO_getc(IoIFP(io));
10191019
if (ch != EOF) {
10201020
(void)PerlIO_ungetc(IoIFP(io),ch);
1021-
errno = saverrno;
1021+
RESTORE_ERRNO;
10221022
return FALSE;
10231023
}
1024-
errno = saverrno;
1024+
RESTORE_ERRNO;
10251025
}
10261026

10271027
if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {

mg.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,10 +817,10 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
817817
}
818818
#else
819819
{
820-
const int saveerrno = errno;
820+
dSAVE_ERRNO;
821821
sv_setnv(sv, (NV)errno);
822822
sv_setpv(sv, errno ? Strerror(errno) : "");
823-
errno = saveerrno;
823+
RESTORE_ERRNO;
824824
}
825825
#endif
826826
SvRTRIM(sv);
@@ -1036,15 +1036,15 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
10361036
sv_setpv(sv, errno ? Strerror(errno) : "");
10371037
#else
10381038
{
1039-
const int saveerrno = errno;
1039+
dSAVE_ERRNO;
10401040
sv_setnv(sv, (NV)errno);
10411041
#ifdef OS2
10421042
if (errno == errno_isOS2 || errno == errno_isOS2_set)
10431043
sv_setpv(sv, os2error(Perl_rc));
10441044
else
10451045
#endif
10461046
sv_setpv(sv, errno ? Strerror(errno) : "");
1047-
errno = saveerrno;
1047+
RESTORE_ERRNO;
10481048
}
10491049
#endif
10501050
SvRTRIM(sv);

perl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,11 @@ EXTERN_C char *crypt(const char *, const char *);
12771277
set_errno(errcode); \
12781278
set_vaxc_errno(vmserrcode); \
12791279
} STMT_END
1280+
# define dSAVEDERRNO int saved_errno; unsigned saved_vms_errno
1281+
# define dSAVE_ERRNO int saved_errno = errno; unsigned saved_vms_errno = vaxc$errno
1282+
# define SAVE_ERRNO ( saved_errno = errno, saved_vms_errno = vaxc$errno )
1283+
# define RESTORE_ERRNO SETERRNO(saved_errno, saved_vms_errno)
1284+
12801285
# define LIB_INVARG LIB$_INVARG
12811286
# define RMS_DIR RMS$_DIR
12821287
# define RMS_FAC RMS$_FAC
@@ -1291,6 +1296,11 @@ EXTERN_C char *crypt(const char *, const char *);
12911296
# define SS_NORMAL SS$_NORMAL
12921297
#else
12931298
# define SETERRNO(errcode,vmserrcode) (errno = (errcode))
1299+
# define dSAVEDERRNO int saved_errno
1300+
# define dSAVE_ERRNO int saved_errno = errno
1301+
# define SAVE_ERRNO (saved_errno = errno)
1302+
# define RESTORE_ERRNO (errno = saved_errno)
1303+
12941304
# define LIB_INVARG 0
12951305
# define RMS_DIR 0
12961306
# define RMS_FAC 0

perlio.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3131,8 +3131,8 @@ PerlIOStdio_close(pTHX_ PerlIO *f)
31313131
const int fd = fileno(stdio);
31323132
int invalidate = 0;
31333133
IV result = 0;
3134-
int saveerr = 0;
31353134
int dupfd = -1;
3135+
dSAVEDERRNO;
31363136
#ifdef USE_ITHREADS
31373137
dVAR;
31383138
#endif
@@ -3166,7 +3166,7 @@ PerlIOStdio_close(pTHX_ PerlIO *f)
31663166
fileno slot of the FILE *
31673167
*/
31683168
result = PerlIO_flush(f);
3169-
saveerr = errno;
3169+
SAVE_ERRNO;
31703170
invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio);
31713171
if (!invalidate) {
31723172
#ifdef USE_ITHREADS
@@ -3205,7 +3205,7 @@ PerlIOStdio_close(pTHX_ PerlIO *f)
32053205
errno may NOT be expected EBADF
32063206
*/
32073207
if (invalidate && result != 0) {
3208-
errno = saveerr;
3208+
RESTORE_ERRNO;
32093209
result = 0;
32103210
}
32113211
#ifdef SOCKS5_VERSION_NAME
@@ -3367,9 +3367,9 @@ PerlIOStdio_flush(pTHX_ PerlIO *f)
33673367
/*
33683368
* Not writeable - sync by attempting a seek
33693369
*/
3370-
const int err = errno;
3370+
dSAVE_ERRNO;
33713371
if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3372-
errno = err;
3372+
RESTORE_ERRNO;
33733373
#endif
33743374
}
33753375
return 0;

pp_sys.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5178,13 +5178,13 @@ PP(pp_gpwent)
51785178
* has a different API than the Solaris/IRIX one. */
51795179
# if defined(HAS_GETSPNAM) && !defined(_AIX)
51805180
{
5181-
const int saverrno = errno;
5181+
dSAVE_ERRNO;
51825182
const struct spwd * const spwent = getspnam(pwent->pw_name);
51835183
/* Save and restore errno so that
51845184
* underprivileged attempts seem
51855185
* to have never made the unsccessful
51865186
* attempt to retrieve the shadow password. */
5187-
errno = saverrno;
5187+
RESTORE_ERRNO;
51885188
if (spwent && spwent->sp_pwdp)
51895189
sv_setpv(sv, spwent->sp_pwdp);
51905190
}
@@ -5562,15 +5562,15 @@ static int
55625562
lockf_emulate_flock(int fd, int operation)
55635563
{
55645564
int i;
5565-
const int save_errno = errno;
55665565
Off_t pos;
5566+
dSAVE_ERRNO;
55675567

55685568
/* flock locks entire file so for lockf we need to do the same */
55695569
pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
55705570
if (pos > 0) /* is seekable and needs to be repositioned */
55715571
if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
55725572
pos = -1; /* seek failed, so don't seek back afterwards */
5573-
errno = save_errno;
5573+
RESTORE_ERRNO;
55745574

55755575
switch (operation) {
55765576

sv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,7 +2958,7 @@ Perl_sv_2pv_flags(pTHX_ register SV *const sv, STRLEN *const lp, const I32 flags
29582958
*s = '\0';
29592959
}
29602960
else if (SvNOKp(sv)) {
2961-
const int olderrno = errno;
2961+
dSAVE_ERRNO;
29622962
if (SvTYPE(sv) < SVt_PVNV)
29632963
sv_upgrade(sv, SVt_PVNV);
29642964
/* The +20 is pure guesswork. Configure test needed. --jhi */
@@ -2972,7 +2972,7 @@ Perl_sv_2pv_flags(pTHX_ register SV *const sv, STRLEN *const lp, const I32 flags
29722972
{
29732973
Gconvert(SvNVX(sv), NV_DIG, 0, s);
29742974
}
2975-
errno = olderrno;
2975+
RESTORE_ERRNO;
29762976
#ifdef FIXNEGATIVEZERO
29772977
if (*s == '-' && s[1] == '0' && !s[2]) {
29782978
s[0] = '0';

util.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,14 +1271,14 @@ Perl_write_to_stderr(pTHX_ const char* message, int msglen)
12711271
else {
12721272
#ifdef USE_SFIO
12731273
/* SFIO can really mess with your errno */
1274-
const int e = errno;
1274+
dSAVED_ERRNO;
12751275
#endif
12761276
PerlIO * const serr = Perl_error_log;
12771277

12781278
PERL_WRITE_MSG_TO_CONSOLE(serr, message, msglen);
12791279
(void)PerlIO_flush(serr);
12801280
#ifdef USE_SFIO
1281-
errno = e;
1281+
RESTORE_ERRNO;
12821282
#endif
12831283
}
12841284
}
@@ -2862,10 +2862,7 @@ Perl_my_pclose(pTHX_ PerlIO *ptr)
28622862
Pid_t pid;
28632863
Pid_t pid2;
28642864
bool close_failed;
2865-
int saved_errno = 0;
2866-
#ifdef WIN32
2867-
int saved_win32_errno;
2868-
#endif
2865+
dSAVEDERRNO;
28692866

28702867
LOCK_FDPID_MUTEX;
28712868
svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE);
@@ -2878,12 +2875,8 @@ Perl_my_pclose(pTHX_ PerlIO *ptr)
28782875
return my_syspclose(ptr);
28792876
}
28802877
#endif
2881-
if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2882-
saved_errno = errno;
2883-
#ifdef WIN32
2884-
saved_win32_errno = GetLastError();
2885-
#endif
2886-
}
2878+
if ((close_failed = (PerlIO_close(ptr) == EOF)))
2879+
SAVE_ERRNO;
28872880
#ifdef UTS
28882881
if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
28892882
#endif
@@ -2901,7 +2894,7 @@ Perl_my_pclose(pTHX_ PerlIO *ptr)
29012894
rsignal_restore(SIGQUIT, &qstat);
29022895
#endif
29032896
if (close_failed) {
2904-
SETERRNO(saved_errno, 0);
2897+
RESTORE_ERRNO;
29052898
return -1;
29062899
}
29072900
return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status));
@@ -5052,12 +5045,12 @@ S_socketpair_udp (int fd[2]) {
50525045
errno = ECONNABORTED;
50535046
tidy_up_and_fail:
50545047
{
5055-
const int save_errno = errno;
5048+
dSAVE_ERRNO;
50565049
if (sockets[0] != -1)
50575050
PerlLIO_close(sockets[0]);
50585051
if (sockets[1] != -1)
50595052
PerlLIO_close(sockets[1]);
5060-
errno = save_errno;
5053+
RESTORE_ERRNO;
50615054
return -1;
50625055
}
50635056
}
@@ -5156,14 +5149,14 @@ Perl_my_socketpair (int family, int type, int protocol, int fd[2]) {
51565149
#endif
51575150
tidy_up_and_fail:
51585151
{
5159-
const int save_errno = errno;
5152+
dSAVE_ERRNO;
51605153
if (listener != -1)
51615154
PerlLIO_close(listener);
51625155
if (connector != -1)
51635156
PerlLIO_close(connector);
51645157
if (acceptor != -1)
51655158
PerlLIO_close(acceptor);
5166-
errno = save_errno;
5159+
RESTORE_ERRNO;
51675160
return -1;
51685161
}
51695162
}

vms/vms.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11933,10 +11933,10 @@ Perl_flex_stat_int(pTHX_ const char *fspec, Stat_t *statbufp, int lstat_flag)
1193311933
char temp_fspec[VMS_MAXRSS];
1193411934
char *save_spec;
1193511935
int retval = -1;
11936-
int saved_errno, saved_vaxc_errno;
11936+
dSAVEDERRNO;
1193711937

1193811938
if (!fspec) return retval;
11939-
saved_errno = errno; saved_vaxc_errno = vaxc$errno;
11939+
SAVE_ERRNO;
1194011940
strcpy(temp_fspec, fspec);
1194111941

1194211942
if (decc_bug_devnull != 0) {
@@ -12063,7 +12063,7 @@ Perl_flex_stat_int(pTHX_ const char *fspec, Stat_t *statbufp, int lstat_flag)
1206312063
# endif
1206412064
}
1206512065
/* If we were successful, leave errno where we found it */
12066-
if (retval == 0) { errno = saved_errno; vaxc$errno = saved_vaxc_errno; }
12066+
if (retval == 0) RESTORE_ERRNO;
1206712067
return retval;
1206812068

1206912069
} /* end of flex_stat_int() */

0 commit comments

Comments
 (0)