util updates: lev_main.c and recover.c

Change lev_comp's add_opvars() to be like pline(), where there's a
single visible opening brace and a second one hidden in VA_DECL2
that introduces a nested block, plus a single visible closing brace
with a hidden one in VA_END() to close the nested block.  This
addresses the erroneous report (sent directly to devteam, so no #H
number, subject "missing '{' in util/lev_main.c:634") that the code
for !USE_STDARG/!USE_VARARGS in add_opvars() wouldn't compile.

Also, fix the part of "#H5778: file descriptor leaks" dealing with
util/recover.c -- an open file not being closed after various errors.
I didn't take responsibility for this entry in the bugzilla list
since the report includes similar problems in other code that's not
addressed here.

And a blast from the past:  some reformatting fixups in recover.c.
The most interesting bit is for a block of dead code....
This commit is contained in:
PatR
2017-07-30 17:34:52 -07:00
parent 27f9a83a0b
commit e3c4ac730d
2 changed files with 37 additions and 27 deletions

View File

@@ -1,4 +1,4 @@
/* NetHack 3.6 lev_main.c $NHDT-Date: 1448074107 2015/11/21 02:48:27 $ $NHDT-Branch: master $:$NHDT-Revision: 1.43 $ */
/* NetHack 3.6 lev_main.c $NHDT-Date: 1501461281 2017/07/31 00:34:41 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.46 $ */
/* Copyright (c) 1989 by Jean-Christophe Collet */
/* NetHack may be freely redistributed. See license for details. */
@@ -615,14 +615,14 @@ VA_DECL2(sp_lev *, sp, const char *, fmt)
#ifdef USE_STDARG
static void
vadd_opvars(sp_lev *sp, const char *fmt, va_list the_args)
{
#else
static void
vadd_opvars(sp, fmt, the_args)
sp_lev *sp;
const char *fmt;
va_list the_args;
{
#endif
#else /* USE_STDARG | USE_VARARG */
@@ -632,7 +632,7 @@ va_list the_args;
void add_opvars
VA_DECL2(sp_lev *, sp, const char *, fmt)
#endif /* USE_STDARG | USE_VARARG */
{
const char *p, *lp;
long la;
/* Do NOT use VA_START and VA_END in here... see above */
@@ -710,7 +710,11 @@ VA_DECL2(sp_lev *, sp, const char *, fmt)
break;
}
}
return;
#if !(defined(USE_STDARG) || defined(USE_VARARGS))
/* provide closing brace for USE_OLDARGS nested block from VA_DECL2() */
VA_END();
#endif
}
void

View File

@@ -1,4 +1,4 @@
/* NetHack 3.6 recover.c $NHDT-Date: 1432512785 2015/05/25 00:13:05 $ $NHDT-Branch: master $:$NHDT-Revision: 1.15 $ */
/* NetHack 3.6 recover.c $NHDT-Date: 1501461282 2017/07/31 00:34:42 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.16 $ */
/* Copyright (c) Janet Walz, 1992. */
/* NetHack may be freely redistributed. See license for details. */
@@ -245,7 +245,7 @@ char *basename;
errno);
#endif
Fprintf(stderr, "Cannot open level 0 for %s.\n", basename);
return (-1);
return -1;
}
if (read(gfd, (genericptr_t) &hpid, sizeof hpid) != sizeof hpid) {
Fprintf(
@@ -253,7 +253,7 @@ char *basename;
"Checkpoint data incompletely written or subsequently clobbered;",
"recovery for \"", basename, "\" impossible.");
Close(gfd);
return (-1);
return -1;
}
if (read(gfd, (genericptr_t) &savelev, sizeof(savelev))
!= sizeof(savelev)) {
@@ -261,7 +261,7 @@ char *basename;
"impossible.\n",
basename);
Close(gfd);
return (-1);
return -1;
}
if ((read(gfd, (genericptr_t) savename, sizeof savename)
!= sizeof savename)
@@ -273,7 +273,7 @@ char *basename;
|| (read(gfd, (genericptr_t) &plbuf, pltmpsiz) != pltmpsiz)) {
Fprintf(stderr, "Error reading %s -- can't recover.\n", lock);
Close(gfd);
return (-1);
return -1;
}
/* save file should contain:
@@ -288,7 +288,7 @@ char *basename;
if (sfd < 0) {
Fprintf(stderr, "Cannot create savefile %s.\n", savename);
Close(gfd);
return (-1);
return -1;
}
lfd = open_levelfile(savelev);
@@ -296,7 +296,7 @@ char *basename;
Fprintf(stderr, "Cannot open level of save for %s.\n", basename);
Close(gfd);
Close(sfd);
return (-1);
return -1;
}
if (write(sfd, (genericptr_t) &version_data, sizeof version_data)
@@ -304,7 +304,8 @@ char *basename;
Fprintf(stderr, "Error writing %s; recovery failed.\n", savename);
Close(gfd);
Close(sfd);
return (-1);
Close(lfd);
return -1;
}
if (write(sfd, (genericptr_t) &sfi, sizeof sfi) != sizeof sfi) {
@@ -313,7 +314,8 @@ char *basename;
savename);
Close(gfd);
Close(sfd);
return (-1);
Close(lfd);
return -1;
}
if (write(sfd, (genericptr_t) &pltmpsiz, sizeof pltmpsiz)
@@ -323,7 +325,8 @@ char *basename;
savename);
Close(gfd);
Close(sfd);
return (-1);
Close(lfd);
return -1;
}
if (write(sfd, (genericptr_t) &plbuf, pltmpsiz) != pltmpsiz) {
@@ -331,7 +334,8 @@ char *basename;
savename);
Close(gfd);
Close(sfd);
return (-1);
Close(lfd);
return -1;
}
copy_bytes(lfd, sfd);
@@ -364,25 +368,27 @@ char *basename;
#if 0 /* OBSOLETE, HackWB is no longer in use */
#ifdef AMIGA
/* we need to create an icon for the saved game
* or HackWB won't notice the file.
*/
{
{
/* we need to create an icon for the saved game
* or HackWB won't notice the file.
*/
char iconfile[FILENAME];
int in, out;
(void) sprintf(iconfile, "%s.info", savename);
in = open("NetHack:default.icon", O_RDONLY);
out = open(iconfile, O_WRONLY | O_TRUNC | O_CREAT);
if(in > -1 && out > -1){
copy_bytes(in,out);
}
if(in > -1)close(in);
if(out > -1)close(out);
if (in > -1 && out > -1) {
copy_bytes(in, out);
}
if (in > -1)
close(in);
if (out > -1)
close(out);
}
#endif /*AMIGA*/
#endif
#endif
return (0);
return 0;
}
#ifdef EXEPATH