From: nethack.rankin Date: Sun, 15 Jan 2012 09:27:06 +0000 (+0000) Subject: rumors & oracles & data.base vs "%lx" format (trunk only) X-Git-Tag: MOVE2GIT~85 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=141653625ddf299bf76bc44caac683b8ff303e4c;p=nethack 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. --- diff --git a/src/pager.c b/src/pager.c index 95a4e007c..62e744fd6 100644 --- a/src/pager.c +++ b/src/pager.c @@ -219,7 +219,7 @@ lookat(x, y, buf, monbuf) /* there might be a mimic here posing as an object */ mtmp = m_at(x, y); if (mtmp && mtmp->m_ap_type == M_AP_OBJECT && - mtmp->mappearance == glyphotyp) otmp = 0; + mtmp->mappearance == (unsigned)glyphotyp) otmp = 0; else mtmp = 0; if (!otmp || otmp->otyp != glyphotyp) { @@ -300,7 +300,7 @@ checkfile(inp, pm, user_typed_name, without_asking) dlb *fp; char buf[BUFSZ], newstr[BUFSZ]; char *ep, *dbase_str; - long txt_offset; + unsigned long txt_offset; int chk_skip; 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"); (void) dlb_fclose(fp); 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; /* 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') { 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!"); (void) dlb_fclose(fp); return; diff --git a/src/rumors.c b/src/rumors.c index ea5f4a5bc..62ff2c8b3 100644 --- a/src/rumors.c +++ b/src/rumors.c @@ -43,11 +43,16 @@ STATIC_DCL void FDECL(init_rumors, (dlb *)); STATIC_DCL void FDECL(init_oracles, (dlb *)); -static long true_rumor_start, true_rumor_size, true_rumor_end, - false_rumor_start, false_rumor_size, false_rumor_end; +/* rumor size variables are signed so that value -1 can be used as a flag */ +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 unsigned oracle_cnt = 0; -static long *oracle_loc = 0; +static unsigned long *oracle_loc = 0; STATIC_OVL void init_rumors(fp) @@ -55,7 +60,7 @@ dlb *fp; { 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 */ - long eof_offset; + unsigned long eof_offset; char line[BUFSZ]; (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, &eof_offset) == 7 && 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 ); */ - 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 ); */ } else { true_rumor_size = -1L; /* init failed */ @@ -87,7 +92,7 @@ char *rumor_buf; boolean exclude_cookie; { dlb *rumors; - long tidbit, beginning; + long tidbit, beginning; char *endp, line[BUFSZ], xbuf[BUFSZ]; rumor_buf[0] = '\0'; @@ -117,11 +122,11 @@ boolean exclude_cookie; */ switch (adjtruth = truth + rn2(2)) { 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; break; 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; break; default: @@ -157,6 +162,7 @@ boolean exclude_cookie; /* remove padding */ { char *x = eos(rumor_buf) - 1; + while(x > rumor_buf && *x=='_') x--; *++x = '\n'; *x = '\0'; @@ -186,6 +192,7 @@ rumor_check() if (rumors) { long ftell_rumor_start = 0L; + rumor_buf[0] = '\0'; if (true_rumor_size == 0L) { /* if this is 1st outrumor() */ init_rumors(rumors); @@ -199,16 +206,16 @@ rumor_check() Sprintf(rumor_buf, "T start=%06ld (%06lx), end=%06ld (%06lx), size=%06ld (%06lx)", - true_rumor_start, true_rumor_start, - true_rumor_end, true_rumor_end, - true_rumor_size, true_rumor_size); + (long)true_rumor_start, true_rumor_start, + true_rumor_end, (unsigned long)true_rumor_end, + true_rumor_size, (unsigned long)true_rumor_size); putstr(tmpwin, 0, rumor_buf); Sprintf(rumor_buf, "F start=%06ld (%06lx), end=%06ld (%06lx), size=%06ld (%06lx)", - false_rumor_start, false_rumor_start, - false_rumor_end, false_rumor_end, - false_rumor_size, false_rumor_size); + (long)false_rumor_start, false_rumor_start, + false_rumor_end, (unsigned long)false_rumor_end, + false_rumor_size, (unsigned long)false_rumor_size); putstr(tmpwin, 0, rumor_buf); /* @@ -219,7 +226,7 @@ rumor_check() * the value read in rumors, and display it. */ 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); (void) dlb_fgets(line, sizeof line, rumors); if ((endp = index(line, '\n')) != 0) *endp = 0; @@ -235,7 +242,7 @@ rumor_check() putstr(tmpwin, 0, rumor_buf); 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); (void) dlb_fgets(line, sizeof line, rumors); if ((endp = index(line, '\n')) != 0) *endp = 0; @@ -318,7 +325,7 @@ dlb *fp; (void) dlb_fgets(line, sizeof line, fp); if (sscanf(line, "%5d\n", &cnt) == 1 && cnt > 0) { 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++) { (void) dlb_fgets(line, sizeof line, fp); (void) sscanf(line, "%5lx\n", &oracle_loc[i]); @@ -350,7 +357,7 @@ int fd; { mread(fd, (genericptr_t) &oracle_cnt, sizeof 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)); oracle_flg = 1; /* no need to call init_oracles() */ } @@ -384,8 +391,10 @@ boolean delphi; /* oracle_loc[1..oracle_cnt-1] are normal ones */ if (oracle_cnt <= 1 && !special) return; /*(shouldn't happen)*/ oracle_idx = special ? 0 : rnd((int) oracle_cnt - 1); - (void) dlb_fseek(oracles, oracle_loc[oracle_idx], SEEK_SET); - if (!special) oracle_loc[oracle_idx] = oracle_loc[--oracle_cnt]; + (void) dlb_fseek(oracles, (long)oracle_loc[oracle_idx], + 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); if (delphi) diff --git a/util/makedefs.c b/util/makedefs.c index e3d652b7e..1f8752893 100644 --- a/util/makedefs.c +++ b/util/makedefs.c @@ -167,6 +167,8 @@ static char *FDECL(version_string, (char *, const char *)); static char *FDECL(version_id_string, (char *,const char *)); static char *FDECL(bannerc_string, (char *,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 char *FDECL(without_control, (char *)); static boolean FDECL(d_filter, (char *)); @@ -810,20 +812,21 @@ const char *str; #define PAD_RUMORS_TO 60 /* common code for do_rumors(). Return 0 on error. */ -static long -read_rumors_file( - const char *file_ext, - int *rumor_count, - long *rumor_size, - long old_rumor_offset -){ +static unsigned long +read_rumors_file(file_ext, rumor_count, rumor_size, old_rumor_offset) +const char *file_ext; +int *rumor_count; +long *rumor_size; +unsigned long old_rumor_offset; +{ char infile[600]; - long rumor_offset; + unsigned long rumor_offset; + Sprintf(infile, DATA_IN_TEMPLATE, RUMOR_FILE); Strcat(infile, file_ext); if (!(ifp = fopen(infile, RDTMODE))) { perror(infile); - return 0; + return 0L; } /* copy the rumors */ @@ -839,8 +842,7 @@ read_rumors_file( *base = '\0'; } #endif - - (*rumor_count)++; + (*rumor_count)++; #if 0 /*[if we forced binary output, this would be sufficient]*/ *rumor_size += strlen(in_line); /* includes newline */ @@ -848,7 +850,7 @@ read_rumors_file( (void) fputs(xcrypt(in_line), tfp); } /* 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 */ /* 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 [it's much simpler to do so unconditionally, rendering the loop's accumulation above obsolete] */ - - *rumor_size = rumor_offset - old_rumor_offset; + *rumor_size = (long)(rumor_offset - old_rumor_offset); return rumor_offset; } @@ -868,8 +869,8 @@ do_rumors() "%s%04d,%06ld,%06lx;%04d,%06ld,%06lx;0,0,%06lx\n"; char tempfile[600]; int true_rumor_count, false_rumor_count; - long true_rumor_size, false_rumor_size, - true_rumor_offset, false_rumor_offset, eof_offset; + long true_rumor_size, false_rumor_size; + unsigned long true_rumor_offset, false_rumor_offset, eof_offset; Sprintf(tempfile, DATA_TEMPLATE, "rumors.tmp"); filename[0]='\0'; @@ -899,14 +900,12 @@ do_rumors() /* record the current position; true rumors will start here */ true_rumor_offset = ftell(tfp); - false_rumor_offset = read_rumors_file( - ".tru", &true_rumor_count, - &true_rumor_size, true_rumor_offset); + false_rumor_offset = read_rumors_file(".tru", &true_rumor_count, + &true_rumor_size, true_rumor_offset); if(!false_rumor_offset) goto rumors_failure; - eof_offset = read_rumors_file( - ".fal", &false_rumor_count, - &false_rumor_size, false_rumor_offset); + eof_offset = read_rumors_file(".fal", &false_rumor_count, + &false_rumor_size, false_rumor_offset); if(!eof_offset) goto rumors_failure; /* get ready to transfer the contents of temp file to output file */ @@ -1652,7 +1651,8 @@ do_data() ok = (rewind(ofp) == 0); if (ok) { 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) { dead_data: perror(in_line); /* report the problem */ @@ -1718,7 +1718,8 @@ do_oracles() { char infile[60], tempfile[60]; boolean in_oracle, ok; - long txt_offset, offset, fpos; + long fpos; + unsigned long txt_offset, offset; int oracle_cnt; register int i; @@ -1752,7 +1753,8 @@ do_oracles() /* handle special oracle; it must come first */ (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++) { (void) fputs(xcrypt(special_oracle[i]), tfp); (void) fputc('\n', tfp); @@ -1761,7 +1763,8 @@ do_oracles() oracle_cnt = 1; (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; while (fgets(in_line, sizeof in_line, ifp)) { @@ -1773,8 +1776,8 @@ do_oracles() in_oracle = FALSE; oracle_cnt++; (void) fputs("---\n", tfp); - Fprintf(ofp, "%05lx\n", ftell(tfp)); - /* start pos of this oracle */ + offset = (unsigned long)ftell(tfp); + Fprintf(ofp, "%05lx\n", offset); /* start pos of this oracle */ } else { in_oracle = TRUE; (void) fputs(xcrypt(in_line), tfp); @@ -1784,11 +1787,12 @@ do_oracles() if (in_oracle) { /* need to terminate last oracle */ oracle_cnt++; (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 */ - txt_offset = ftell(ofp); + txt_offset = (unsigned long)ftell(ofp); Fclose(ifp); /* all done with original input file */ /* reprocess the scratch file; 1st format an error msg, just in case */ @@ -1817,16 +1821,7 @@ do_oracles() #endif if (!(ok = (fpos = ftell(ofp)) >= 0)) break; if (!(ok = (fseek(ofp, fpos, SEEK_SET) >= 0))) 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; - } + if (!(ok = (fscanf(ofp, "%5lx", &offset) == 1))) break; #ifdef MAC # ifdef __MWERKS__ /* @@ -1839,8 +1834,8 @@ do_oracles() # endif #endif if (!(ok = (fseek(ofp, fpos, SEEK_SET) >= 0))) break; - if (!(ok = (fprintf(ofp, "%05lx\n", offset + txt_offset) >= 0))) - break; + offset += txt_offset; + if (!(ok = (fprintf(ofp, "%05lx\n", offset) >= 0))) break; } } if (!ok) {