rumors & oracles & data.base vs "%lx" format (trunk only)

For text data processed by makedefs at install time, change all
printf and scanf calls that use %lx format to deal with unsigned long
variables, replacing the makedefs hack of a few days ago.  It's not as
clean as I would have liked (quite a few casts), because the values
involved are derived from ftell and/or passed to fseek, which deal in
signed longs.  But it clears up a few format check warnings by gcc in
rumors.c and pager.c in addition to the previous one in makedefs.c and
uses the right data type even in the places where no warning was issued.
This commit is contained in:
nethack.rankin
2012-01-15 09:27:06 +00:00
parent 09efc215a8
commit 141653625d
3 changed files with 71 additions and 67 deletions

View File

@@ -219,7 +219,7 @@ lookat(x, y, buf, monbuf)
/* there might be a mimic here posing as an object */ /* there might be a mimic here posing as an object */
mtmp = m_at(x, y); mtmp = m_at(x, y);
if (mtmp && mtmp->m_ap_type == M_AP_OBJECT && if (mtmp && mtmp->m_ap_type == M_AP_OBJECT &&
mtmp->mappearance == glyphotyp) otmp = 0; mtmp->mappearance == (unsigned)glyphotyp) otmp = 0;
else mtmp = 0; else mtmp = 0;
if (!otmp || otmp->otyp != glyphotyp) { if (!otmp || otmp->otyp != glyphotyp) {
@@ -300,7 +300,7 @@ checkfile(inp, pm, user_typed_name, without_asking)
dlb *fp; dlb *fp;
char buf[BUFSZ], newstr[BUFSZ]; char buf[BUFSZ], newstr[BUFSZ];
char *ep, *dbase_str; char *ep, *dbase_str;
long txt_offset; unsigned long txt_offset;
int chk_skip; int chk_skip;
boolean found_in_file = FALSE, skipping_entry = FALSE; boolean found_in_file = FALSE, skipping_entry = FALSE;
@@ -370,7 +370,7 @@ checkfile(inp, pm, user_typed_name, without_asking)
impossible("can't read 'data' file"); impossible("can't read 'data' file");
(void) dlb_fclose(fp); (void) dlb_fclose(fp);
return; return;
} else if (sscanf(buf, "%8lx\n", &txt_offset) < 1 || txt_offset <= 0) } else if (sscanf(buf, "%8lx\n", &txt_offset) < 1 || txt_offset == 0L)
goto bad_data_file; goto bad_data_file;
/* look for the appropriate entry */ /* look for the appropriate entry */
@@ -417,7 +417,7 @@ bad_data_file: impossible("'data' file in wrong format");
if (user_typed_name || without_asking || yn("More info?") == 'y') { if (user_typed_name || without_asking || yn("More info?") == 'y') {
winid datawin; winid datawin;
if (dlb_fseek(fp, txt_offset + entry_offset, SEEK_SET) < 0) { if (dlb_fseek(fp, (long)txt_offset + entry_offset, SEEK_SET) < 0) {
pline("? Seek error on 'data' file!"); pline("? Seek error on 'data' file!");
(void) dlb_fclose(fp); (void) dlb_fclose(fp);
return; return;

View File

@@ -43,11 +43,16 @@
STATIC_DCL void FDECL(init_rumors, (dlb *)); STATIC_DCL void FDECL(init_rumors, (dlb *));
STATIC_DCL void FDECL(init_oracles, (dlb *)); STATIC_DCL void FDECL(init_oracles, (dlb *));
static long true_rumor_start, true_rumor_size, true_rumor_end, /* rumor size variables are signed so that value -1 can be used as a flag */
false_rumor_start, false_rumor_size, false_rumor_end; static long true_rumor_size = 0L, false_rumor_size;
/* rumor start offsets are unsigned because they're handled via %lx format */
static unsigned long true_rumor_start, false_rumor_start;
/* rumor end offsets are signed because they're compared with [dlb_]ftell() */
static long true_rumor_end, false_rumor_end;
/* oracles are handled differently from rumors... */
static int oracle_flg = 0; /* -1=>don't use, 0=>need init, 1=>init done */ static int oracle_flg = 0; /* -1=>don't use, 0=>need init, 1=>init done */
static unsigned oracle_cnt = 0; static unsigned oracle_cnt = 0;
static long *oracle_loc = 0; static unsigned long *oracle_loc = 0;
STATIC_OVL void STATIC_OVL void
init_rumors(fp) init_rumors(fp)
@@ -55,7 +60,7 @@ dlb *fp;
{ {
static const char rumors_header[] = "%d,%ld,%lx;%d,%ld,%lx;0,0,%lx\n"; static const char rumors_header[] = "%d,%ld,%lx;%d,%ld,%lx;0,0,%lx\n";
int true_count, false_count; /* in file but not used here */ int true_count, false_count; /* in file but not used here */
long eof_offset; unsigned long eof_offset;
char line[BUFSZ]; char line[BUFSZ];
(void) dlb_fgets(line, sizeof line, fp); /* skip "don't edit" comment */ (void) dlb_fgets(line, sizeof line, fp); /* skip "don't edit" comment */
@@ -65,9 +70,9 @@ dlb *fp;
&false_count, &false_rumor_size, &false_rumor_start, &false_count, &false_rumor_size, &false_rumor_start,
&eof_offset) == 7 && &eof_offset) == 7 &&
true_rumor_size > 0L && false_rumor_size > 0L) { true_rumor_size > 0L && false_rumor_size > 0L) {
true_rumor_end = true_rumor_start + true_rumor_size; true_rumor_end = (long)true_rumor_start + true_rumor_size;
/* assert( true_rumor_end == false_rumor_start ); */ /* assert( true_rumor_end == false_rumor_start ); */
false_rumor_end = false_rumor_start + false_rumor_size; false_rumor_end = (long)false_rumor_start + false_rumor_size;
/* assert( false_rumor_end == eof_offset ); */ /* assert( false_rumor_end == eof_offset ); */
} else { } else {
true_rumor_size = -1L; /* init failed */ true_rumor_size = -1L; /* init failed */
@@ -87,7 +92,7 @@ char *rumor_buf;
boolean exclude_cookie; boolean exclude_cookie;
{ {
dlb *rumors; dlb *rumors;
long tidbit, beginning; long tidbit, beginning;
char *endp, line[BUFSZ], xbuf[BUFSZ]; char *endp, line[BUFSZ], xbuf[BUFSZ];
rumor_buf[0] = '\0'; rumor_buf[0] = '\0';
@@ -117,11 +122,11 @@ boolean exclude_cookie;
*/ */
switch (adjtruth = truth + rn2(2)) { switch (adjtruth = truth + rn2(2)) {
case 2: /*(might let a bogus input arg sneak thru)*/ case 2: /*(might let a bogus input arg sneak thru)*/
case 1: beginning = true_rumor_start; case 1: beginning = (long)true_rumor_start;
tidbit = Rand() % true_rumor_size; tidbit = Rand() % true_rumor_size;
break; break;
case 0: /* once here, 0 => false rather than "either"*/ case 0: /* once here, 0 => false rather than "either"*/
case -1: beginning = false_rumor_start; case -1: beginning = (long)false_rumor_start;
tidbit = Rand() % false_rumor_size; tidbit = Rand() % false_rumor_size;
break; break;
default: default:
@@ -157,6 +162,7 @@ boolean exclude_cookie;
/* remove padding */ /* remove padding */
{ {
char *x = eos(rumor_buf) - 1; char *x = eos(rumor_buf) - 1;
while(x > rumor_buf && *x=='_') x--; while(x > rumor_buf && *x=='_') x--;
*++x = '\n'; *++x = '\n';
*x = '\0'; *x = '\0';
@@ -186,6 +192,7 @@ rumor_check()
if (rumors) { if (rumors) {
long ftell_rumor_start = 0L; long ftell_rumor_start = 0L;
rumor_buf[0] = '\0'; rumor_buf[0] = '\0';
if (true_rumor_size == 0L) { /* if this is 1st outrumor() */ if (true_rumor_size == 0L) { /* if this is 1st outrumor() */
init_rumors(rumors); init_rumors(rumors);
@@ -199,16 +206,16 @@ rumor_check()
Sprintf(rumor_buf, Sprintf(rumor_buf,
"T start=%06ld (%06lx), end=%06ld (%06lx), size=%06ld (%06lx)", "T start=%06ld (%06lx), end=%06ld (%06lx), size=%06ld (%06lx)",
true_rumor_start, true_rumor_start, (long)true_rumor_start, true_rumor_start,
true_rumor_end, true_rumor_end, true_rumor_end, (unsigned long)true_rumor_end,
true_rumor_size, true_rumor_size); true_rumor_size, (unsigned long)true_rumor_size);
putstr(tmpwin, 0, rumor_buf); putstr(tmpwin, 0, rumor_buf);
Sprintf(rumor_buf, Sprintf(rumor_buf,
"F start=%06ld (%06lx), end=%06ld (%06lx), size=%06ld (%06lx)", "F start=%06ld (%06lx), end=%06ld (%06lx), size=%06ld (%06lx)",
false_rumor_start, false_rumor_start, (long)false_rumor_start, false_rumor_start,
false_rumor_end, false_rumor_end, false_rumor_end, (unsigned long)false_rumor_end,
false_rumor_size, false_rumor_size); false_rumor_size, (unsigned long)false_rumor_size);
putstr(tmpwin, 0, rumor_buf); putstr(tmpwin, 0, rumor_buf);
/* /*
@@ -219,7 +226,7 @@ rumor_check()
* the value read in rumors, and display it. * the value read in rumors, and display it.
*/ */
rumor_buf[0] = '\0'; rumor_buf[0] = '\0';
(void) dlb_fseek(rumors, true_rumor_start, SEEK_SET); (void) dlb_fseek(rumors, (long)true_rumor_start, SEEK_SET);
ftell_rumor_start = dlb_ftell(rumors); ftell_rumor_start = dlb_ftell(rumors);
(void) dlb_fgets(line, sizeof line, rumors); (void) dlb_fgets(line, sizeof line, rumors);
if ((endp = index(line, '\n')) != 0) *endp = 0; if ((endp = index(line, '\n')) != 0) *endp = 0;
@@ -235,7 +242,7 @@ rumor_check()
putstr(tmpwin, 0, rumor_buf); putstr(tmpwin, 0, rumor_buf);
rumor_buf[0] = '\0'; rumor_buf[0] = '\0';
(void) dlb_fseek(rumors, false_rumor_start, SEEK_SET); (void) dlb_fseek(rumors, (long)false_rumor_start, SEEK_SET);
ftell_rumor_start = dlb_ftell(rumors); ftell_rumor_start = dlb_ftell(rumors);
(void) dlb_fgets(line, sizeof line, rumors); (void) dlb_fgets(line, sizeof line, rumors);
if ((endp = index(line, '\n')) != 0) *endp = 0; if ((endp = index(line, '\n')) != 0) *endp = 0;
@@ -318,7 +325,7 @@ dlb *fp;
(void) dlb_fgets(line, sizeof line, fp); (void) dlb_fgets(line, sizeof line, fp);
if (sscanf(line, "%5d\n", &cnt) == 1 && cnt > 0) { if (sscanf(line, "%5d\n", &cnt) == 1 && cnt > 0) {
oracle_cnt = (unsigned) cnt; oracle_cnt = (unsigned) cnt;
oracle_loc = (long *) alloc((unsigned)cnt * sizeof (long)); oracle_loc = (unsigned long *)alloc((unsigned)cnt * sizeof (long));
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
(void) dlb_fgets(line, sizeof line, fp); (void) dlb_fgets(line, sizeof line, fp);
(void) sscanf(line, "%5lx\n", &oracle_loc[i]); (void) sscanf(line, "%5lx\n", &oracle_loc[i]);
@@ -350,7 +357,7 @@ int fd;
{ {
mread(fd, (genericptr_t) &oracle_cnt, sizeof oracle_cnt); mread(fd, (genericptr_t) &oracle_cnt, sizeof oracle_cnt);
if (oracle_cnt) { if (oracle_cnt) {
oracle_loc = (long *) alloc(oracle_cnt * sizeof (long)); oracle_loc = (unsigned long *)alloc(oracle_cnt * sizeof (long));
mread(fd, (genericptr_t) oracle_loc, oracle_cnt * sizeof (long)); mread(fd, (genericptr_t) oracle_loc, oracle_cnt * sizeof (long));
oracle_flg = 1; /* no need to call init_oracles() */ oracle_flg = 1; /* no need to call init_oracles() */
} }
@@ -384,8 +391,10 @@ boolean delphi;
/* oracle_loc[1..oracle_cnt-1] are normal ones */ /* oracle_loc[1..oracle_cnt-1] are normal ones */
if (oracle_cnt <= 1 && !special) return; /*(shouldn't happen)*/ if (oracle_cnt <= 1 && !special) return; /*(shouldn't happen)*/
oracle_idx = special ? 0 : rnd((int) oracle_cnt - 1); oracle_idx = special ? 0 : rnd((int) oracle_cnt - 1);
(void) dlb_fseek(oracles, oracle_loc[oracle_idx], SEEK_SET); (void) dlb_fseek(oracles, (long)oracle_loc[oracle_idx],
if (!special) oracle_loc[oracle_idx] = oracle_loc[--oracle_cnt]; SEEK_SET);
if (!special) /* move offset of very last one into this slot */
oracle_loc[oracle_idx] = oracle_loc[--oracle_cnt];
tmpwin = create_nhwindow(NHW_TEXT); tmpwin = create_nhwindow(NHW_TEXT);
if (delphi) if (delphi)

View File

@@ -167,6 +167,8 @@ static char *FDECL(version_string, (char *, const char *));
static char *FDECL(version_id_string, (char *,const char *)); static char *FDECL(version_id_string, (char *,const char *));
static char *FDECL(bannerc_string, (char *,const char *)); static char *FDECL(bannerc_string, (char *,const char *));
static char *FDECL(xcrypt, (const char *)); static char *FDECL(xcrypt, (const char *));
static unsigned long FDECL(read_rumors_file,
(const char *,int *,long *,unsigned long));
static int FDECL(check_control, (char *)); static int FDECL(check_control, (char *));
static char *FDECL(without_control, (char *)); static char *FDECL(without_control, (char *));
static boolean FDECL(d_filter, (char *)); static boolean FDECL(d_filter, (char *));
@@ -810,20 +812,21 @@ const char *str;
#define PAD_RUMORS_TO 60 #define PAD_RUMORS_TO 60
/* common code for do_rumors(). Return 0 on error. */ /* common code for do_rumors(). Return 0 on error. */
static long static unsigned long
read_rumors_file( read_rumors_file(file_ext, rumor_count, rumor_size, old_rumor_offset)
const char *file_ext, const char *file_ext;
int *rumor_count, int *rumor_count;
long *rumor_size, long *rumor_size;
long old_rumor_offset unsigned long old_rumor_offset;
){ {
char infile[600]; char infile[600];
long rumor_offset; unsigned long rumor_offset;
Sprintf(infile, DATA_IN_TEMPLATE, RUMOR_FILE); Sprintf(infile, DATA_IN_TEMPLATE, RUMOR_FILE);
Strcat(infile, file_ext); Strcat(infile, file_ext);
if (!(ifp = fopen(infile, RDTMODE))) { if (!(ifp = fopen(infile, RDTMODE))) {
perror(infile); perror(infile);
return 0; return 0L;
} }
/* copy the rumors */ /* copy the rumors */
@@ -839,8 +842,7 @@ read_rumors_file(
*base = '\0'; *base = '\0';
} }
#endif #endif
(*rumor_count)++;
(*rumor_count)++;
#if 0 #if 0
/*[if we forced binary output, this would be sufficient]*/ /*[if we forced binary output, this would be sufficient]*/
*rumor_size += strlen(in_line); /* includes newline */ *rumor_size += strlen(in_line); /* includes newline */
@@ -848,7 +850,7 @@ read_rumors_file(
(void) fputs(xcrypt(in_line), tfp); (void) fputs(xcrypt(in_line), tfp);
} }
/* record the current position; next rumors section will start here */ /* record the current position; next rumors section will start here */
rumor_offset = ftell(tfp); rumor_offset = (unsigned long)ftell(tfp);
Fclose(ifp); /* all done with rumors.file_ext */ Fclose(ifp); /* all done with rumors.file_ext */
/* the calculated value for *_rumor_count assumes that /* the calculated value for *_rumor_count assumes that
@@ -856,8 +858,7 @@ read_rumors_file(
which use two byte CR+LF, we need to override that value which use two byte CR+LF, we need to override that value
[it's much simpler to do so unconditionally, rendering [it's much simpler to do so unconditionally, rendering
the loop's accumulation above obsolete] */ the loop's accumulation above obsolete] */
*rumor_size = (long)(rumor_offset - old_rumor_offset);
*rumor_size = rumor_offset - old_rumor_offset;
return rumor_offset; return rumor_offset;
} }
@@ -868,8 +869,8 @@ do_rumors()
"%s%04d,%06ld,%06lx;%04d,%06ld,%06lx;0,0,%06lx\n"; "%s%04d,%06ld,%06lx;%04d,%06ld,%06lx;0,0,%06lx\n";
char tempfile[600]; char tempfile[600];
int true_rumor_count, false_rumor_count; int true_rumor_count, false_rumor_count;
long true_rumor_size, false_rumor_size, long true_rumor_size, false_rumor_size;
true_rumor_offset, false_rumor_offset, eof_offset; unsigned long true_rumor_offset, false_rumor_offset, eof_offset;
Sprintf(tempfile, DATA_TEMPLATE, "rumors.tmp"); Sprintf(tempfile, DATA_TEMPLATE, "rumors.tmp");
filename[0]='\0'; filename[0]='\0';
@@ -899,14 +900,12 @@ do_rumors()
/* record the current position; true rumors will start here */ /* record the current position; true rumors will start here */
true_rumor_offset = ftell(tfp); true_rumor_offset = ftell(tfp);
false_rumor_offset = read_rumors_file( false_rumor_offset = read_rumors_file(".tru", &true_rumor_count,
".tru", &true_rumor_count, &true_rumor_size, true_rumor_offset);
&true_rumor_size, true_rumor_offset);
if(!false_rumor_offset) goto rumors_failure; if(!false_rumor_offset) goto rumors_failure;
eof_offset = read_rumors_file( eof_offset = read_rumors_file(".fal", &false_rumor_count,
".fal", &false_rumor_count, &false_rumor_size, false_rumor_offset);
&false_rumor_size, false_rumor_offset);
if(!eof_offset) goto rumors_failure; if(!eof_offset) goto rumors_failure;
/* get ready to transfer the contents of temp file to output file */ /* get ready to transfer the contents of temp file to output file */
@@ -1652,7 +1651,8 @@ do_data()
ok = (rewind(ofp) == 0); ok = (rewind(ofp) == 0);
if (ok) { if (ok) {
Sprintf(in_line, "header rewrite of \"%s\"", filename); Sprintf(in_line, "header rewrite of \"%s\"", filename);
ok = (fprintf(ofp, "%s%08lx\n", Dont_Edit_Data, txt_offset) >= 0); ok = (fprintf(ofp, "%s%08lx\n", Dont_Edit_Data,
(unsigned long)txt_offset) >= 0);
} }
if (!ok) { if (!ok) {
dead_data: perror(in_line); /* report the problem */ dead_data: perror(in_line); /* report the problem */
@@ -1718,7 +1718,8 @@ do_oracles()
{ {
char infile[60], tempfile[60]; char infile[60], tempfile[60];
boolean in_oracle, ok; boolean in_oracle, ok;
long txt_offset, offset, fpos; long fpos;
unsigned long txt_offset, offset;
int oracle_cnt; int oracle_cnt;
register int i; register int i;
@@ -1752,7 +1753,8 @@ do_oracles()
/* handle special oracle; it must come first */ /* handle special oracle; it must come first */
(void) fputs("---\n", tfp); (void) fputs("---\n", tfp);
Fprintf(ofp, "%05lx\n", ftell(tfp)); /* start pos of special oracle */ offset = (unsigned long)ftell(tfp);
Fprintf(ofp, "%05lx\n", offset); /* start pos of special oracle */
for (i = 0; i < SIZE(special_oracle); i++) { for (i = 0; i < SIZE(special_oracle); i++) {
(void) fputs(xcrypt(special_oracle[i]), tfp); (void) fputs(xcrypt(special_oracle[i]), tfp);
(void) fputc('\n', tfp); (void) fputc('\n', tfp);
@@ -1761,7 +1763,8 @@ do_oracles()
oracle_cnt = 1; oracle_cnt = 1;
(void) fputs("---\n", tfp); (void) fputs("---\n", tfp);
Fprintf(ofp, "%05lx\n", ftell(tfp)); /* start pos of first oracle */ offset = (unsigned long)ftell(tfp);
Fprintf(ofp, "%05lx\n", offset); /* start pos of first oracle */
in_oracle = FALSE; in_oracle = FALSE;
while (fgets(in_line, sizeof in_line, ifp)) { while (fgets(in_line, sizeof in_line, ifp)) {
@@ -1773,8 +1776,8 @@ do_oracles()
in_oracle = FALSE; in_oracle = FALSE;
oracle_cnt++; oracle_cnt++;
(void) fputs("---\n", tfp); (void) fputs("---\n", tfp);
Fprintf(ofp, "%05lx\n", ftell(tfp)); offset = (unsigned long)ftell(tfp);
/* start pos of this oracle */ Fprintf(ofp, "%05lx\n", offset); /* start pos of this oracle */
} else { } else {
in_oracle = TRUE; in_oracle = TRUE;
(void) fputs(xcrypt(in_line), tfp); (void) fputs(xcrypt(in_line), tfp);
@@ -1784,11 +1787,12 @@ do_oracles()
if (in_oracle) { /* need to terminate last oracle */ if (in_oracle) { /* need to terminate last oracle */
oracle_cnt++; oracle_cnt++;
(void) fputs("---\n", tfp); (void) fputs("---\n", tfp);
Fprintf(ofp, "%05lx\n", ftell(tfp)); /* eof position */ offset = (unsigned long)ftell(tfp);
Fprintf(ofp, "%05lx\n", offset); /* eof position */
} }
/* record the current position */ /* record the current position */
txt_offset = ftell(ofp); txt_offset = (unsigned long)ftell(ofp);
Fclose(ifp); /* all done with original input file */ Fclose(ifp); /* all done with original input file */
/* reprocess the scratch file; 1st format an error msg, just in case */ /* reprocess the scratch file; 1st format an error msg, just in case */
@@ -1817,16 +1821,7 @@ do_oracles()
#endif #endif
if (!(ok = (fpos = ftell(ofp)) >= 0)) break; if (!(ok = (fpos = ftell(ofp)) >= 0)) break;
if (!(ok = (fseek(ofp, fpos, SEEK_SET) >= 0))) break; if (!(ok = (fseek(ofp, fpos, SEEK_SET) >= 0))) break;
{ if (!(ok = (fscanf(ofp, "%5lx", &offset) == 1))) break;
/* gcc's format checking issues a warning when using
%lx to read into a signed long, so force unsigned;
casting &offset to unsigned long * works but is iffy */
unsigned long uloffset;
int itmp = fscanf(ofp, "%5lx", &uloffset);
offset = (long)uloffset;
if (!(ok = (itmp == 1))) break;
}
#ifdef MAC #ifdef MAC
# ifdef __MWERKS__ # ifdef __MWERKS__
/* /*
@@ -1839,8 +1834,8 @@ do_oracles()
# endif # endif
#endif #endif
if (!(ok = (fseek(ofp, fpos, SEEK_SET) >= 0))) break; if (!(ok = (fseek(ofp, fpos, SEEK_SET) >= 0))) break;
if (!(ok = (fprintf(ofp, "%05lx\n", offset + txt_offset) >= 0))) offset += txt_offset;
break; if (!(ok = (fprintf(ofp, "%05lx\n", offset) >= 0))) break;
} }
} }
if (!ok) { if (!ok) {