Add streq() and start finding places it fixes warnings.

Some type fixes from Michael Allison.
This commit is contained in:
nhkeni
2022-03-16 18:50:17 -04:00
parent dc72e07a2b
commit 7f484815e5
8 changed files with 42 additions and 19 deletions
+1
View File
@@ -999,6 +999,7 @@ extern int strncmpi(const char *, const char *, int);
#ifndef STRSTRI #ifndef STRSTRI
extern char *strstri(const char *, const char *); extern char *strstri(const char *, const char *);
#endif #endif
extern int streq(const char *, const char *, boolean);
extern boolean fuzzymatch(const char *, const char *, const char *, boolean); extern boolean fuzzymatch(const char *, const char *, const char *, boolean);
extern void init_random(int(*fn)(int)); extern void init_random(int(*fn)(int));
extern void reseed_random(int(*fn)(int)); extern void reseed_random(int(*fn)(int));
+1 -1
View File
@@ -158,7 +158,7 @@ dupstr_n(const char *string, unsigned int *lenout)
if(len >= LARGEST_INT) if(len >= LARGEST_INT)
panic("string too long"); panic("string too long");
*lenout = (unsigned int) len; *lenout = (unsigned int) len;
return strcpy((char *) alloc((unsigned)len + 1), string); return strcpy((char *) alloc(len + 1), string);
} }
/*alloc.c*/ /*alloc.c*/
+4 -6
View File
@@ -364,8 +364,7 @@ title_to_mon(const char *str, int *rank_indx, int *title_length)
/* loop through each of the rank titles for role #i */ /* loop through each of the rank titles for role #i */
for (j = 0; j < 9; j++) { for (j = 0; j < 9; j++) {
if (roles[i].rank[j].m if (roles[i].rank[j].m
&& !strncmpi(str, roles[i].rank[j].m, && streq(str, roles[i].rank[j].m, TRUE)) {
strlen(roles[i].rank[j].m))) {
if (rank_indx) if (rank_indx)
*rank_indx = j; *rank_indx = j;
if (title_length) if (title_length)
@@ -373,8 +372,7 @@ title_to_mon(const char *str, int *rank_indx, int *title_length)
return roles[i].mnum; return roles[i].mnum;
} }
if (roles[i].rank[j].f if (roles[i].rank[j].f
&& !strncmpi(str, roles[i].rank[j].f, && streq(str, roles[i].rank[j].m, TRUE)) {
strlen(roles[i].rank[j].f))) {
if (rank_indx) if (rank_indx)
*rank_indx = j; *rank_indx = j;
if (title_length) if (title_length)
@@ -498,7 +496,7 @@ static char *conditionbitmask2str(unsigned long);
static unsigned long match_str2conditionbitmask(const char *); static unsigned long match_str2conditionbitmask(const char *);
static unsigned long str2conditionbitmask(char *); static unsigned long str2conditionbitmask(char *);
static boolean parse_condition(char (*)[QBUFSZ], int); static boolean parse_condition(char (*)[QBUFSZ], int);
static char *hlattr2attrname(int, char *, int); static char *hlattr2attrname(int, char *, size_t);
static void status_hilite_linestr_add(int, struct hilite_s *, unsigned long, static void status_hilite_linestr_add(int, struct hilite_s *, unsigned long,
const char *); const char *);
static void status_hilite_linestr_done(void); static void status_hilite_linestr_done(void);
@@ -2926,7 +2924,7 @@ clear_status_hilites(void)
} }
static char * static char *
hlattr2attrname(int attrib, char *buf, int bufsz) hlattr2attrname(int attrib, char *buf, size_t bufsz)
{ {
if (attrib && buf) { if (attrib && buf) {
char attbuf[BUFSZ]; char attbuf[BUFSZ];
+1 -2
View File
@@ -2466,8 +2466,7 @@ donamelevel(void)
/* add new annotation, unless it's all spaces (which will be an /* add new annotation, unless it's all spaces (which will be an
empty string after mungspaces() above) */ empty string after mungspaces() above) */
if (*nbuf && strcmp(nbuf, " ")) { if (*nbuf && strcmp(nbuf, " ")) {
mptr->custom = dupstr(nbuf); mptr->custom = dupstr_n(nbuf,&mptr->custom_lth);
mptr->custom_lth = strlen(mptr->custom);
} }
return ECMD_OK; return ECMD_OK;
} }
+25 -1
View File
@@ -635,7 +635,7 @@ rounddiv(long x, int y)
divsgn = -divsgn; divsgn = -divsgn;
x = -x; x = -x;
} }
r = x / y; r = (int) (x / y);
m = x % y; m = x % y;
if (2 * m >= y) if (2 * m >= y)
r++; r++;
@@ -837,6 +837,30 @@ strstri(const char *str, const char *sub)
} }
#endif /* STRSTRI */ #endif /* STRSTRI */
/* string equality, possibly ignoring case; panics on huge strings */
int
streq(register const char *s1, register const char *s2,
boolean caseblind)
{
register char t1, t2;
int n = LARGEST_INT;
while (n--) {
if (!*s2)
return (*s1 == 0); /* s1 >= s2 */
else if (!*s1)
return 1; /* s1 < s2 */
t1 = caseblind ? lowc(*s1) : *s1;
t2 = caseblind ? lowc(*s2) : *s2;
s1++,s2++;
if (t1 != t2)
return 0;
}
if (n==0)
panic("string too long");
return 1;
}
/* compare two strings for equality, ignoring the presence of specified /* compare two strings for equality, ignoring the presence of specified
characters (typically whitespace) and possibly ignoring case */ characters (typically whitespace) and possibly ignoring case */
boolean boolean
+7 -7
View File
@@ -860,20 +860,20 @@ name_to_monplus(
for (len = 0, i = LOW_PM; i < NUMMONS; i++) { for (len = 0, i = LOW_PM; i < NUMMONS; i++) {
for (mgend = MALE; mgend < NUM_MGENDERS; mgend++) { for (mgend = MALE; mgend < NUM_MGENDERS; mgend++) {
int m_i_len; size_t m_i_len;
if (!mons[i].pmnames[mgend]) if (!mons[i].pmnames[mgend])
continue; continue;
m_i_len = (int) strlen(mons[i].pmnames[mgend]); m_i_len = strlen(mons[i].pmnames[mgend]);
if (m_i_len > len && !strncmpi(mons[i].pmnames[mgend], str, m_i_len)) { if (m_i_len > (size_t) len && !strncmpi(mons[i].pmnames[mgend], str, (int) m_i_len)) {
if (m_i_len == (int) slen) { if (m_i_len == slen) {
mntmp = i; mntmp = i;
len = m_i_len; len = (int) m_i_len;
matchgend = mgend; matchgend = mgend;
exact_match = TRUE; exact_match = TRUE;
break; /* exact match */ break; /* exact match */
} else if ((int) slen > m_i_len } else if (slen > m_i_len
&& (str[m_i_len] == ' ' && (str[m_i_len] == ' '
|| !strcmpi(&str[m_i_len], "s") || !strcmpi(&str[m_i_len], "s")
|| !strncmpi(&str[m_i_len], "s ", 2) || !strncmpi(&str[m_i_len], "s ", 2)
@@ -884,7 +884,7 @@ name_to_monplus(
|| !strcmpi(&str[m_i_len], "es") || !strcmpi(&str[m_i_len], "es")
|| !strncmpi(&str[m_i_len], "es ", 3))) { || !strncmpi(&str[m_i_len], "es ", 3))) {
mntmp = i; mntmp = i;
len = m_i_len; len = (int) m_i_len;
matchgend = mgend; matchgend = mgend;
} }
} }
+1 -1
View File
@@ -3196,7 +3196,7 @@ wizterrainwish(struct _readobjnam_data *d)
const char *tname; const char *tname;
tname = trapname(trap, TRUE); tname = trapname(trap, TRUE);
if (strncmpi(tname, bp, strlen(tname))) if(!streq(tname, bp, TRUE))
continue; continue;
/* found it; avoid stupid mistakes */ /* found it; avoid stupid mistakes */
if (is_hole(trap) && !Can_fall_thru(&u.uz)) if (is_hole(trap) && !Can_fall_thru(&u.uz))
+2 -1
View File
@@ -6660,7 +6660,8 @@ msgtype_parse_add(char *str)
int i; int i;
for (i = 0; i < SIZE(msgtype_names); i++) for (i = 0; i < SIZE(msgtype_names); i++)
if (!strncmpi(msgtype_names[i].name, msgtype, strlen(msgtype))) { if (streq(msgtype_names[i].name, msgtype, TRUE)) {
//if (!strncmpi(msgtype_names[i].name, msgtype, strlen(msgtype))) {
typ = msgtype_names[i].msgtyp; typ = msgtype_names[i].msgtyp;
break; break;
} }