From b4697103687179bd67847bd7a75e468847fcbaf3 Mon Sep 17 00:00:00 2001 From: Mark Nudelman Date: Mon, 13 Nov 2023 16:08:04 -0800 Subject: [PATCH] Use size_t where appropriate. gcc -Wconversion warning output is reduced from 975 lines to 679 lines. --- ch.c | 34 +++++++++++-------- charset.c | 8 ++--- cmdbuf.c | 24 ++++++------- command.c | 8 ++--- cvt.c | 16 ++++----- decode.c | 42 +++++++++++------------ edit.c | 16 ++++----- evar.c | 30 ++++++++--------- filename.c | 50 +++++++++++++-------------- forwback.c | 2 +- input.c | 2 +- lang.h | 4 ++- less.h | 4 +-- lesskey.c | 10 +++--- lesskey_parse.c | 2 +- line.c | 89 +++++++++++++++++++++++++------------------------ lsystem.c | 2 +- main.c | 22 ++++++------ optfunc.c | 8 ++--- opttbl.c | 6 ++-- os.c | 10 +++--- output.c | 8 ++--- pattern.c | 9 ++--- position.c | 10 +++--- prompt.c | 6 ++-- screen.c | 2 +- search.c | 50 +++++++++++++-------------- tags.c | 14 ++++---- ttyin.c | 2 +- xbuf.c | 6 ++-- xbuf.h | 10 +++--- 31 files changed, 257 insertions(+), 249 deletions(-) diff --git a/ch.c b/ch.c index 5c59f657..8afb3fa4 100644 --- a/ch.c +++ b/ch.c @@ -46,7 +46,7 @@ struct bufnode { struct buf { struct bufnode node; BLOCKNUM block; - unsigned int datasize; + size_t datasize; unsigned char data[LBUFSIZE]; }; #define bufnode_buf(bn) ((struct buf *) bn) @@ -64,7 +64,7 @@ struct filestate { POSITION fpos; int nbufs; BLOCKNUM block; - unsigned int offset; + size_t offset; POSITION fsize; }; @@ -139,6 +139,13 @@ extern char *namelogfile; static int ch_addbuf(); +/* + * Return the file position corresponding to an offset within a block. + */ +static POSITION ch_position(BLOCKNUM block, size_t offset) +{ + return (ch_block * LBUFSIZE) + (POSITION) offset; +} /* * Get the character pointed to by the read pointer. @@ -147,7 +154,7 @@ static int ch_get(void) { struct buf *bp; struct bufnode *bn; - int n; + ssize_t n; int read_again; int h; POSITION pos; @@ -223,7 +230,7 @@ static int ch_get(void) for (;;) { - pos = (ch_block * LBUFSIZE) + bp->datasize; + pos = ch_position(ch_block, bp->datasize); if ((len = ch_length()) != NULL_POSITION && pos >= len) /* * At end of file. @@ -264,8 +271,7 @@ static int ch_get(void) n = 1; } else { - n = iread(ch_file, &bp->data[bp->datasize], - (unsigned int)(LBUFSIZE - bp->datasize)); + n = iread(ch_file, &bp->data[bp->datasize], LBUFSIZE - bp->datasize); } read_again = FALSE; @@ -298,12 +304,12 @@ static int ch_get(void) if (secure_allow(SF_LOGFILE)) { if (logfile >= 0 && n > 0) - write(logfile, (char *) &bp->data[bp->datasize], n); + write(logfile, (char *) &bp->data[bp->datasize], (size_t) n); } #endif ch_fpos += n; - bp->datasize += n; + bp->datasize += (size_t) n; if (n == 0) { @@ -489,7 +495,7 @@ public int ch_seek(POSITION pos) * Set read pointer. */ ch_block = new_block; - ch_offset = pos % LBUFSIZE; + ch_offset = (size_t) (pos % LBUFSIZE); return (0); } @@ -536,7 +542,7 @@ public int ch_end_buffer_seek(void) FOR_BUFS(bn) { bp = bufnode_buf(bn); - buf_pos = (bp->block * LBUFSIZE) + bp->datasize; + buf_pos = ch_position(bp->block, bp->datasize); if (buf_pos > end_pos) end_pos = buf_pos; } @@ -600,7 +606,7 @@ public POSITION ch_tell(void) { if (thisfile == NULL) return (NULL_POSITION); - return (ch_block * LBUFSIZE) + ch_offset; + return ch_position(ch_block, ch_offset); } /* @@ -650,14 +656,14 @@ public int ch_back_get(void) * Set max amount of buffer space. * bufspace is in units of 1024 bytes. -1 mean no limit. */ -public void ch_setbufspace(int bufspace) +public void ch_setbufspace(size_t bufspace) { if (bufspace < 0) maxbufs = -1; else { - int lbufk = LBUFSIZE / 1024; - maxbufs = bufspace / lbufk + (bufspace % lbufk != 0); + size_t lbufk = LBUFSIZE / 1024; + maxbufs = (int) (bufspace / lbufk + (bufspace % lbufk != 0)); if (maxbufs < 1) maxbufs = 1; } diff --git a/charset.c b/charset.c index b9d46c10..13259783 100644 --- a/charset.c +++ b/charset.c @@ -139,7 +139,7 @@ static struct wchar_range_table user_prt_table; static void wchar_range_table_set(struct wchar_range_table *tbl, struct xbuffer *arr) { tbl->table = (struct wchar_range *) arr->data; - tbl->count = arr->end / sizeof(struct wchar_range); + tbl->count = (unsigned int) (arr->end / sizeof(struct wchar_range)); } /* @@ -845,8 +845,8 @@ static struct wchar_range comb_table[] = { static int is_in_table(LWCHAR ch, struct wchar_range_table *table) { - int hi; - int lo; + unsigned int hi; + unsigned int lo; /* Binary search in the table. */ if (table->table == NULL || table->count == 0 || ch < table->table[0].first) @@ -855,7 +855,7 @@ static int is_in_table(LWCHAR ch, struct wchar_range_table *table) hi = table->count - 1; while (lo <= hi) { - int mid = (lo + hi) / 2; + unsigned int mid = (lo + hi) / 2; if (ch > table->table[mid].last) lo = mid + 1; else if (ch < table->table[mid].first) diff --git a/cmdbuf.c b/cmdbuf.c index 696f99e2..b8fca6eb 100644 --- a/cmdbuf.c +++ b/cmdbuf.c @@ -31,7 +31,7 @@ static int prompt_col; /* Column of cursor just after prompt */ static char *cp; /* Pointer into cmdbuf */ static int cmd_offset; /* Index into cmdbuf of first displayed char */ static int literal; /* Next input char should not be interpreted */ -public int updown_match = -1; /* Prefix length in up/down movement */ +public ssize_t updown_match = -1; /* Prefix length in up/down movement */ #if TAB_COMPLETE_FILENAME static int cmd_complete(int action); @@ -186,7 +186,7 @@ public int len_cmdbuf(void) * {{ Returning pwidth and bswidth separately is a historical artifact * since they're always the same. Maybe clean this up someday. }} */ -static constant char * cmd_step_common(char *p, LWCHAR ch, int len, int *pwidth, int *bswidth) +static constant char * cmd_step_common(char *p, LWCHAR ch, size_t len, int *pwidth, int *bswidth) { constant char *pr; int width; @@ -226,7 +226,7 @@ static constant char * cmd_step_right(char **pp, int *pwidth, int *bswidth) char *p = *pp; LWCHAR ch = step_char(pp, +1, p + strlen(p)); - return cmd_step_common(p, ch, *pp - p, pwidth, bswidth); + return cmd_step_common(p, ch, ptr_diff(*pp, p), pwidth, bswidth); } /* @@ -237,7 +237,7 @@ static constant char * cmd_step_left(char **pp, int *pwidth, int *bswidth) char *p = *pp; LWCHAR ch = step_char(pp, -1, cmdbuf); - return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth); + return cmd_step_common(*pp, ch, ptr_diff(p, *pp), pwidth, bswidth); } /* @@ -436,7 +436,7 @@ static int cmd_left(void) /* * Insert a char into the command buffer, at the current position. */ -static int cmd_ichar(constant char *cs, int clen) +static int cmd_ichar(constant char *cs, size_t clen) { char *s; @@ -644,9 +644,7 @@ static int cmd_updown(int action) } if (updown_match < 0) - { - updown_match = (int) (cp - cmdbuf); - } + updown_match = ptr_diff(cp, cmdbuf); /* * Find the next history entry which matches. @@ -911,7 +909,7 @@ static int cmd_istr(constant char *str) { constant char *os = s; step_charc(&s, +1, endline); - action = cmd_ichar(os, s - os); + action = cmd_ichar(os, ptr_diff(s, os)); if (action != CC_OK) return (action); } @@ -932,7 +930,7 @@ static char * delimit_word(void) int delim_quoted = 0; int meta_quoted = 0; constant char *esc = get_meta_escape(); - int esclen = (int) strlen(esc); + size_t esclen = strlen(esc); #endif /* @@ -1039,8 +1037,8 @@ static void init_compl(void) */ if (tk_original != NULL) free(tk_original); - tk_original = (char *) ecalloc(cp-word+1, sizeof(char)); - strncpy(tk_original, word, cp-word); + tk_original = (char *) ecalloc(ptr_diff(cp,word)+1, sizeof(char)); + strncpy(tk_original, word, ptr_diff(cp,word)); /* * Get the expanded filename. * This may result in a single filename, or @@ -1190,7 +1188,7 @@ static int cmd_complete(int action) public int cmd_char(int c) { int action; - int len; + size_t len; if (!utf_mode) { diff --git a/command.c b/command.c index ebf14b79..ada01515 100644 --- a/command.c +++ b/command.c @@ -2047,13 +2047,13 @@ public void commands(void) * Shift view left. */ if (number > 0) - shift_count = number; + shift_count = (int) number; else number = (shift_count > 0) ? shift_count : sc_width / 2; if (number > hshift) number = hshift; pos_rehead(); - hshift -= number; + hshift -= (int) number; screen_trashed(); break; @@ -2062,11 +2062,11 @@ public void commands(void) * Shift view right. */ if (number > 0) - shift_count = number; + shift_count = (int) number; else number = (shift_count > 0) ? shift_count : sc_width / 2; pos_rehead(); - hshift += number; + hshift += (int) number; screen_trashed(); break; diff --git a/cvt.c b/cvt.c index 99f58a3b..04ed303e 100644 --- a/cvt.c +++ b/cvt.c @@ -19,7 +19,7 @@ extern int utf_mode; /* * Get the length of a buffer needed to convert a string. */ -public int cvt_length(int len, int ops) +public size_t cvt_length(size_t len, int ops) { if (utf_mode) /* @@ -34,10 +34,10 @@ public int cvt_length(int len, int ops) /* * Allocate a chpos array for use by cvt_text. */ -public int * cvt_alloc_chpos(int len) +public int * cvt_alloc_chpos(size_t len) { int i; - int *chpos = (int *) ecalloc(sizeof(int), len); + int *chpos = (int *) ecalloc(len, sizeof(int)); /* Initialize all entries to an invalid position. */ for (i = 0; i < len; i++) chpos[i] = -1; @@ -49,7 +49,7 @@ public int * cvt_alloc_chpos(int len) * Returns converted text in odst. The original offset of each * odst character (when it was in osrc) is returned in the chpos array. */ -public void cvt_text(char *odst, constant char *osrc, int *chpos, int *lenp, int ops) +public void cvt_text(char *odst, constant char *osrc, int *chpos, size_t *lenp, int ops) { char *dst; char *edst = odst; @@ -64,8 +64,8 @@ public void cvt_text(char *odst, constant char *osrc, int *chpos, int *lenp, int for (src = osrc, dst = odst; src < src_end; ) { - int src_pos = (int) (src - osrc); - int dst_pos = (int) (dst - odst); + size_t src_pos = ptr_diff(src, osrc); + size_t dst_pos = ptr_diff(dst, odst); struct ansi_state *pansi; ch = step_charc(&src, +1, src_end); if ((ops & CVT_BS) && ch == '\b' && dst > odst) @@ -96,7 +96,7 @@ public void cvt_text(char *odst, constant char *osrc, int *chpos, int *lenp, int if (chpos != NULL) { while (cdst++ < dst) - chpos[dst_pos++] = src_pos; + chpos[dst_pos++] = (int) src_pos; /*{{type-issue}}*/ } } if (dst > edst) @@ -106,6 +106,6 @@ public void cvt_text(char *odst, constant char *osrc, int *chpos, int *lenp, int edst--; *edst = '\0'; if (lenp != NULL) - *lenp = (int) (edst - odst); + *lenp = ptr_diff(edst, odst); /* FIXME: why was this here? if (chpos != NULL) chpos[dst - odst] = src - osrc; */ } diff --git a/decode.c b/decode.c index 21caa70c..c4f402e9 100644 --- a/decode.c +++ b/decode.c @@ -245,13 +245,13 @@ static struct tablelist *list_sysvar_tables = NULL; /* * Expand special key abbreviations in a command table. */ -static void expand_special_keys(unsigned char *table, int len) +static void expand_special_keys(unsigned char *table, size_t len) { unsigned char *fm; unsigned char *to; int a; constant char *repl; - int klen; + size_t klen; for (fm = table; fm < table + len; ) { @@ -278,7 +278,7 @@ static void expand_special_keys(unsigned char *table, int len) repl = special_key_str(fm[1]); klen = fm[2] & 0377; fm += klen; - if (repl == NULL || (int) strlen(repl) > klen) + if (repl == NULL || strlen(repl) > klen) repl = "\377"; while (*repl != '\0') *to++ = *repl++; @@ -308,7 +308,7 @@ static void expand_cmd_table(struct tablelist *tlist) struct tablelist *t; for (t = tlist; t != NULL; t = t->t_next) { - expand_special_keys(t->t_start, t->t_end - t->t_start); + expand_special_keys(t->t_start, ptr_diff(t->t_end, t->t_start)); } } @@ -378,7 +378,7 @@ public void init_cmds(void) /* * Add a command table. */ -static int add_cmd_table(struct tablelist **tlist, unsigned char *buf, int len) +static int add_cmd_table(struct tablelist **tlist, unsigned char *buf, size_t len) { struct tablelist *t; @@ -434,7 +434,7 @@ static void pop_cmd_table(struct tablelist **tlist) /* * Add a command table. */ -public void add_fcmd_table(unsigned char *buf, int len) +public void add_fcmd_table(unsigned char *buf, size_t len) { if (add_cmd_table(&list_fcmd_tables, buf, len) < 0) error("Warning: some commands disabled", NULL_PARG); @@ -443,7 +443,7 @@ public void add_fcmd_table(unsigned char *buf, int len) /* * Add an editing command table. */ -public void add_ecmd_table(unsigned char *buf, int len) +public void add_ecmd_table(unsigned char *buf, size_t len) { if (add_cmd_table(&list_ecmd_tables, buf, len) < 0) error("Warning: some edit commands disabled", NULL_PARG); @@ -452,7 +452,7 @@ public void add_ecmd_table(unsigned char *buf, int len) /* * Add an environment variable table. */ -static void add_var_table(struct tablelist **tlist, unsigned char *buf, int len) +static void add_var_table(struct tablelist **tlist, unsigned char *buf, size_t len) { struct xbuffer xbuf; @@ -759,11 +759,11 @@ public constant char * lgetenv(constant char *var) /* * Like lgetenv, but also uses a buffer partially filled with an env table. */ -public constant char * lgetenv_ext(constant char *var, unsigned char *env_buf, int env_buf_len) +public constant char * lgetenv_ext(constant char *var, unsigned char *env_buf, size_t env_buf_len) { constant char *r; - int e; - int env_end = 0; + size_t e; + size_t env_end = 0; for (e = 0;;) { @@ -802,9 +802,9 @@ public int isnullenv(constant char *s) * Integers are stored in a funny format: * two bytes, low order first, in radix KRADIX. */ -static int gint(unsigned char **sp) +static size_t gint(unsigned char **sp) { - int n; + size_t n; n = *(*sp)++; n += *(*sp)++ * KRADIX; @@ -814,7 +814,7 @@ static int gint(unsigned char **sp) /* * Process an old (pre-v241) lesskey file. */ -static int old_lesskey(unsigned char *buf, int len) +static int old_lesskey(unsigned char *buf, size_t len) { /* * Old-style lesskey file. @@ -832,12 +832,12 @@ static int old_lesskey(unsigned char *buf, int len) /* * Process a new (post-v241) lesskey file. */ -static int new_lesskey(unsigned char *buf, int len, int sysvar) +static int new_lesskey(unsigned char *buf, size_t len, int sysvar) { unsigned char *p; unsigned char *end; int c; - int n; + size_t n; /* * New-style lesskey file. @@ -894,7 +894,7 @@ public int lesskey(constant char *filename, int sysvar) { unsigned char *buf; POSITION len; - long n; + ssize_t n; int f; if (!secure_allow(SF_LESSKEY)) @@ -923,7 +923,7 @@ public int lesskey(constant char *filename, int sysvar) close(f); return (-1); } - if ((buf = (unsigned char *) calloc((int)len, sizeof(char))) == NULL) + if ((buf = (unsigned char *) calloc((size_t)len, sizeof(char))) == NULL) { close(f); return (-1); @@ -934,7 +934,7 @@ public int lesskey(constant char *filename, int sysvar) close(f); return (-1); } - n = read(f, buf, (unsigned int) len); + n = read(f, buf, (size_t) len); close(f); if (n != len) { @@ -949,8 +949,8 @@ public int lesskey(constant char *filename, int sysvar) if (len < 4 || buf[0] != C0_LESSKEY_MAGIC || buf[1] != C1_LESSKEY_MAGIC || buf[2] != C2_LESSKEY_MAGIC || buf[3] != C3_LESSKEY_MAGIC) - return (old_lesskey(buf, (int)len)); - return (new_lesskey(buf, (int)len, sysvar)); + return (old_lesskey(buf, (size_t) len)); + return (new_lesskey(buf, (size_t) len, sysvar)); } #if HAVE_LESSKEYSRC diff --git a/edit.c b/edit.c index 85a343ef..ab8d609c 100644 --- a/edit.c +++ b/edit.c @@ -66,7 +66,7 @@ public void init_textlist(struct textlist *tlist, mutable char *str) int meta_quoted = 0; int delim_quoted = 0; constant char *esc = get_meta_escape(); - int esclen = (int) strlen(esc); + size_t esclen = strlen(esc); #endif tlist->string = skipsp(str); @@ -147,9 +147,9 @@ public constant char * back_textlist(struct textlist *tlist, constant char *prev /* * Parse a single option setting in a modeline. */ -static void modeline_option(constant char *str, int opt_len) +static void modeline_option(constant char *str, size_t opt_len) { - struct mloption { constant char *opt_name; void (*opt_func)(constant char*,int); }; + struct mloption { constant char *opt_name; void (*opt_func)(constant char*,size_t); }; struct mloption options[] = { { "ts=", set_tabs }, { "tabstop=", set_tabs }, @@ -158,7 +158,7 @@ static void modeline_option(constant char *str, int opt_len) struct mloption *opt; for (opt = options; opt->opt_name != NULL; opt++) { - int name_len = strlen(opt->opt_name); + size_t name_len = strlen(opt->opt_name); if (opt_len > name_len && strncmp(str, opt->opt_name, name_len) == 0) { (*opt->opt_func)(str + name_len, opt_len - name_len); @@ -171,7 +171,7 @@ static void modeline_option(constant char *str, int opt_len) * String length, terminated by option separator (space or colon). * Space/colon can be escaped with backspace. */ -static int modeline_option_len(constant char *str) +static size_t modeline_option_len(constant char *str) { int esc = FALSE; constant char *s; @@ -184,7 +184,7 @@ static int modeline_option_len(constant char *str) else if (*s == ' ' || *s == ':') /* separator */ break; } - return (s - str); + return ptr_diff(s, str); } /* @@ -194,7 +194,7 @@ static void modeline_options(constant char *str, char end_char) { for (;;) { - int opt_len; + size_t opt_len; str = skipspc(str); if (*str == '\0' || *str == end_char) break; @@ -249,7 +249,7 @@ static void check_modelines(void) for (i = 0; i < modelines; i++) { constant char *line; - int line_len; + size_t line_len; if (ABORT_SIGS()) return; pos = forw_raw_line(pos, &line, &line_len); diff --git a/evar.c b/evar.c index bb1f6521..547dcb00 100644 --- a/evar.c +++ b/evar.c @@ -23,7 +23,7 @@ struct replace { /* * Skip to the next unescaped slash or right curly bracket in a string. */ -static int skipsl(constant char *buf, int len, int e) +static size_t skipsl(constant char *buf, size_t len, size_t e) { int esc = 0; while (e < len && buf[e] != '\0' && (esc || (buf[e] != '/' && buf[e] != '}'))) @@ -39,16 +39,16 @@ static int skipsl(constant char *buf, int len, int e) * (slash, pattern, slash, replacement), followed by right curly bracket. * Replacement may be empty in which case the second slash is optional. */ -static struct replace * make_replaces(mutable char *buf, int len, int *pe, char term) +static struct replace * make_replaces(mutable char *buf, size_t len, size_t *pe, char term) { - int e = *pe; + size_t e = *pe; struct replace *replaces = NULL; while (term == '/') { struct replace *repl; - int to; - int fm = e; + size_t to; + size_t fm = e; e = skipsl(buf, len, e); if (e >= len) break; if (e == fm) /* missing fm string; we're done */ @@ -98,9 +98,9 @@ static void free_replaces(struct replace *replaces) * Backslash escapes in the pattern are ignored. * Return the length of the matched substring, or 0 if no match. */ -static int evar_match(constant char *str, constant char *pat) +static size_t evar_match(constant char *str, constant char *pat) { - int len = 0; + size_t len = 0; while (*pat != '\0') { if (*pat == '\\') ++pat; @@ -114,11 +114,11 @@ static int evar_match(constant char *str, constant char *pat) * Find the replacement for a string (&evar[*pv]), * given a list of replace structs. */ -static constant char * find_replace(constant struct replace *repl, constant char *evar, int *pv) +static constant char * find_replace(constant struct replace *repl, constant char *evar, size_t *pv) { for (; repl != NULL; repl = repl->r_next) { - int len = evar_match(&evar[*pv], repl->r_fm); + size_t len = evar_match(&evar[*pv], repl->r_fm); if (len > 0) { *pv += len; @@ -135,10 +135,10 @@ static constant char * find_replace(constant struct replace *repl, constant char * Write evar to xbuf, performing any specified text replacements. * Return the new value of e to point just after the final right curly bracket. */ -static int add_evar(struct xbuffer *xbuf, mutable char *buf, int len, int e, constant char *evar, char term) +static size_t add_evar(struct xbuffer *xbuf, mutable char *buf, size_t len, size_t e, constant char *evar, char term) { struct replace *replaces = make_replaces(buf, len, &e, term); - int v; + size_t v; for (v = 0; evar[v] != '\0'; ) { @@ -147,7 +147,7 @@ static int add_evar(struct xbuffer *xbuf, mutable char *buf, int len, int e, con xbuf_add_byte(xbuf, evar[v++]); else { - int r; + size_t r; for (r = 0; repl[r] != '\0'; r++) { if (repl[r] == '\\') ++r; @@ -163,16 +163,16 @@ static int add_evar(struct xbuffer *xbuf, mutable char *buf, int len, int e, con * Expand env variables in a string. * Writes expanded output to xbuf. Corrupts buf. */ -public void expand_evars(mutable char *buf, int len, struct xbuffer *xbuf) +public void expand_evars(mutable char *buf, size_t len, struct xbuffer *xbuf) { - int i; + size_t i; for (i = 0; i < len; ) { if (i+1 < len && buf[i] == '$' && buf[i+1] == '{') { constant char *evar; char term; - int e; + size_t e; i += 2; /* skip "${" */ for (e = i; e < len; e++) if (buf[e] == '\0' || buf[e] == '}' || buf[e] == '/') diff --git a/filename.c b/filename.c index e358c321..a3a0170e 100644 --- a/filename.c +++ b/filename.c @@ -83,7 +83,7 @@ public char * shell_unquote(constant char *str) } else { constant char *esc = get_meta_escape(); - int esclen = (int) strlen(esc); + size_t esclen = strlen(esc); while (*str != '\0') { if (esclen > 0 && strncmp(str, esc, esclen) == 0) @@ -140,9 +140,9 @@ public char * shell_quote(constant char *s) constant char *p; char *np; char *newstr; - int len; + size_t len; constant char *esc = get_meta_escape(); - int esclen = (int) strlen(esc); + size_t esclen = strlen(esc); int use_quotes = 0; int have_quotes = 0; @@ -180,7 +180,7 @@ public char * shell_quote(constant char *s) * We can't quote a string that contains quotes. */ return (NULL); - len = (int) strlen(s) + 3; + len = strlen(s) + 3; } /* * Allocate and construct the new string. @@ -215,7 +215,7 @@ public char * shell_quote(constant char *s) public char * dirfile(constant char *dirname, constant char *filename, int must_exist) { char *pathname; - int len; + size_t len; int f; if (dirname == NULL || *dirname == '\0') @@ -223,7 +223,7 @@ public char * dirfile(constant char *dirname, constant char *filename, int must_ /* * Construct the full pathname. */ - len = (int) (strlen(dirname) + strlen(filename) + 2); + len = strlen(dirname) + strlen(filename) + 2; pathname = (char *) calloc(len, sizeof(char)); if (pathname == NULL) return (NULL); @@ -295,7 +295,7 @@ public char * fexpand(constant char *s) { constant char *fr; char *to; - int n; + size_t n; char *e; IFILE ifile; @@ -330,7 +330,7 @@ public char * fexpand(constant char *s) if (ifile == NULL_IFILE) n++; else - n += (int) strlen(get_filename(ifile)); + n += strlen(get_filename(ifile)); } /* * Else it is the first char in a string of @@ -408,11 +408,11 @@ public char * fcomplete(constant char *s) */ { constant char *slash; - int len; + size_t len; for (slash = s+strlen(s)-1; slash > s; slash--) if (*slash == *PATHNAME_SEP || *slash == '/') break; - len = (int) strlen(s) + 4; + len = strlen(s) + 4; fpat = (char *) ecalloc(len, sizeof(char)); if (strchr(slash, '.') == NULL) SNPRINTF1(fpat, len, "%s*.*", s); @@ -421,7 +421,7 @@ public char * fcomplete(constant char *s) } #else { - int len = (int) strlen(s) + 2; + size_t len = strlen(s) + 2; fpat = (char *) ecalloc(len, sizeof(char)); SNPRINTF1(fpat, len, "%s*", s); } @@ -448,7 +448,7 @@ public char * fcomplete(constant char *s) */ public int bin_file(int f) { - int n; + ssize_t n; int bin_count = 0; char data[256]; constant char* p; @@ -464,7 +464,7 @@ public int bin_file(int f) edata = &data[n]; for (p = data; p < edata; ) { - if (utf_mode && !is_utf8_well_formed(p, edata-p)) + if (utf_mode && !is_utf8_well_formed(p, (int) ptr_diff(edata,p))) { bin_count++; utf_skip_to_lead(&p, edata); @@ -547,7 +547,7 @@ static FILE * shellcmd(constant char *cmd) fd = popen(cmd, "r"); } else { - int len = (int) (strlen(shell) + strlen(esccmd) + 5); + size_t len = strlen(shell) + strlen(esccmd) + 5; scmd = (char *) ecalloc(len, sizeof(char)); SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd); free(esccmd); @@ -586,7 +586,7 @@ public char * lglob(constant char *afilename) /* * The globbing function returns a list of names. */ - int length; + size_t length; char *p; char *qfilename; DECL_GLOB_LIST(list) @@ -632,8 +632,8 @@ public char * lglob(constant char *afilename) * is called multiple times to walk thru all names. */ char *p; - int len; - int n; + size_t len; + size_t n; char *pfilename; char *qfilename; DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle) @@ -649,14 +649,14 @@ public char * lglob(constant char *afilename) gfilename = (char *) ecalloc(len, sizeof(char)); p = gfilename; do { - n = (int) (strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1); + n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1; pfilename = (char *) ecalloc(n, sizeof(char)); SNPRINTF3(pfilename, n, "%s%s%s", drive, dir, fnd.GLOB_NAME); qfilename = shell_quote(pfilename); free(pfilename); if (qfilename != NULL) { - n = (int) strlen(qfilename); + n = strlen(qfilename); while (p - gfilename + n + 2 >= len) { /* @@ -697,7 +697,7 @@ public char * lglob(constant char *afilename) char *cmd; constant char *esc; char *qesc; - int len; + size_t len; esc = get_meta_escape(); if (strlen(esc) == 0) @@ -713,7 +713,7 @@ public char * lglob(constant char *afilename) /* * Invoke lessecho, and read its output (a globbed list of filenames). */ - len = (int) (strlen(lessecho) + strlen(filename) + (7*strlen(metachars())) + 24); + len = strlen(lessecho) + strlen(filename) + (7*strlen(metachars())) + 24; cmd = (char *) ecalloc(len, sizeof(char)); SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, (unsigned char) openquote, (unsigned char) closequote, qesc); @@ -814,7 +814,7 @@ public char * open_altfile(constant char *filename, int *pf, void **pfd) constant char *lessopen; char *qfilename; char *cmd; - int len; + size_t len; FILE *fd; #if HAVE_FILENO int returnfd = 0; @@ -859,7 +859,7 @@ public char * open_altfile(constant char *filename, int *pf, void **pfd) } qfilename = shell_quote(filename); - len = (int) (strlen(lessopen) + strlen(qfilename) + 2); + len = strlen(lessopen) + strlen(qfilename) + 2; cmd = (char *) ecalloc(len, sizeof(char)); SNPRINTF1(cmd, len, lessopen, qfilename); free(qfilename); @@ -937,7 +937,7 @@ public void close_altfile(constant char *altfilename, constant char *filename) char *qaltfilename; FILE *fd; char *cmd; - int len; + size_t len; if (!secure_allow(SF_LESSOPEN)) return; @@ -951,7 +951,7 @@ public void close_altfile(constant char *altfilename, constant char *filename) } qfilename = shell_quote(filename); qaltfilename = shell_quote(altfilename); - len = (int) (strlen(lessclose) + strlen(qfilename) + strlen(qaltfilename) + 2); + len = strlen(lessclose) + strlen(qfilename) + strlen(qaltfilename) + 2; cmd = (char *) ecalloc(len, sizeof(char)); SNPRINTF2(cmd, len, lessclose, qfilename, qaltfilename); free(qaltfilename); diff --git a/forwback.c b/forwback.c index e89e2090..0b893177 100644 --- a/forwback.c +++ b/forwback.c @@ -35,7 +35,7 @@ extern int header_lines; extern int header_cols; extern int full_screen; #if HILITE_SEARCH -extern int size_linebuf; +extern size_t size_linebuf; extern int hilite_search; extern int status_col; #endif diff --git a/input.c b/input.c index 5974faa4..1982240b 100644 --- a/input.c +++ b/input.c @@ -29,7 +29,7 @@ extern POSITION start_attnpos; extern POSITION end_attnpos; #if HILITE_SEARCH extern int hilite_search; -extern int size_linebuf; +extern size_t size_linebuf; extern int show_attn; #endif diff --git a/lang.h b/lang.h index a400477a..b5986829 100644 --- a/lang.h +++ b/lang.h @@ -28,4 +28,6 @@ #define public /* PUBLIC FUNCTION */ -#endif +#define ptr_diff(p1,p2) ((size_t) ((p1)-(p2))) + +#endif // LESS_LANG_H_ diff --git a/less.h b/less.h index 98c806a3..8c5e81f9 100644 --- a/less.h +++ b/less.h @@ -353,8 +353,8 @@ struct wchar_range struct wchar_range_table { - struct wchar_range *table; - int count; + struct wchar_range *table; + unsigned int count; }; #if HAVE_POLL diff --git a/lesskey.c b/lesskey.c index 03a28fca..86743d40 100644 --- a/lesskey.c +++ b/lesskey.c @@ -130,7 +130,7 @@ void out_of_memory(void) exit(1); } -void * ecalloc(int count, unsigned int size) +void * ecalloc(size_t count, size_t size) { void *p; @@ -246,7 +246,7 @@ static void parse_args(int argc, constant char **argv) /* * Output some bytes. */ -static void fputbytes(FILE *fd, constant char *buf, int len) +static void fputbytes(FILE *fd, constant char *buf, size_t len) { while (len-- > 0) { @@ -258,14 +258,14 @@ static void fputbytes(FILE *fd, constant char *buf, int len) /* * Output an integer, in special KRADIX form. */ -static void fputint(FILE *fd, unsigned int val) +static void fputint(FILE *fd, size_t val) { char c; if (val >= KRADIX*KRADIX) { - fprintf(stderr, "error: cannot write %d, max %d\n", - val, KRADIX*KRADIX); + fprintf(stderr, "error: cannot write %ld, max %ld\n", + (long) val, (long) (KRADIX*KRADIX)); exit(1); } c = val % KRADIX; diff --git a/lesskey_parse.c b/lesskey_parse.c index 97dd1bad..2957e110 100644 --- a/lesskey_parse.c +++ b/lesskey_parse.c @@ -20,7 +20,7 @@ extern void lesskey_parse_error(char *msg); extern char *homefile(char *filename); -extern void *ecalloc(int count, unsigned int size); +extern void *ecalloc(size_t count, size_t size); extern int lstrtoi(char *str, char **end, int radix); extern char version[]; diff --git a/line.c b/line.c index 35c6347e..3f887b77 100644 --- a/line.c +++ b/line.c @@ -25,12 +25,12 @@ #define MAX_PFX_WIDTH (MAX_LINENUM_WIDTH + MAX_STATUSCOL_WIDTH + 1) static struct { char *buf; /* Buffer which holds the current output line */ - int *attr; /* Parallel to buf, to hold attributes */ - int print; /* Index in buf of first printable char */ - int end; /* Number of chars in buf */ + int *attr; /* Parallel to buf, to hold attributes */ + size_t print; /* Index in buf of first printable char */ + size_t end; /* Number of chars in buf */ char pfx[MAX_PFX_WIDTH]; /* Holds status column and line number */ int pfx_attr[MAX_PFX_WIDTH]; - int pfx_end; /* Number of chars in pfx */ + size_t pfx_end; /* Number of chars in pfx */ } linebuf; /* @@ -50,7 +50,7 @@ static struct xbuffer last_ansi; static struct xbuffer last_ansis[NUM_LAST_ANSIS]; static int curr_last_ansi; -public int size_linebuf = 0; /* Size of line buffer (and attr buffer) */ +public size_t size_linebuf = 0; /* Size of line buffer (and attr buffer) */ static struct ansi_state *line_ansi = NULL; static int ansi_in_line; static int hlink_in_line; @@ -108,7 +108,7 @@ static char mbc_buf[MAX_UTF_CHAR_LEN]; static int mbc_buf_len = 0; static int mbc_buf_index = 0; static POSITION mbc_pos; -static int saved_line_end; +static size_t saved_line_end; static int saved_end_column; /* Configurable color map */ @@ -173,7 +173,7 @@ public void init_line(void) static int expand_linebuf(void) { /* Double the size of the line buffer. */ - int new_size = size_linebuf * 2; + size_t new_size = size_linebuf * 2; char *new_buf = (char *) calloc(new_size, sizeof(char)); int *new_attr = (int *) calloc(new_size, sizeof(int)); if (new_buf == NULL || new_attr == NULL) @@ -212,7 +212,7 @@ static void inc_end_column(int w) if (end_column > right_column && w > 0) { right_column = end_column; - right_curr = linebuf.end; + right_curr = (int) linebuf.end; } end_column += w; } @@ -261,7 +261,7 @@ public void prewind(void) /* * Set a character in the line buffer. */ -static void set_linebuf(int n, char ch, int attr) +static void set_linebuf(size_t n, char ch, int attr) { if (n >= size_linebuf) { @@ -297,7 +297,7 @@ static void addstr_linebuf(constant char *s, int attr, int cw) /* * Set a character in the line prefix buffer. */ -static void set_pfx(int n, char ch, int attr) +static void set_pfx(size_t n, char ch, int attr) { linebuf.pfx[n] = ch; linebuf.pfx_attr[n] = attr; @@ -358,7 +358,7 @@ public void plinestart(POSITION pos) if (linenums == OPT_ONPLUS) { char buf[INT_STRLEN_BOUND(linenum) + 2]; - int len; + size_t len; linenum = vlinenum(linenum); if (linenum == 0) @@ -366,28 +366,28 @@ public void plinestart(POSITION pos) else { linenumtoa(linenum, buf, 10); - len = (int) strlen(buf); + len = strlen(buf); } - for (i = 0; i < linenum_width - len; i++) + for (i = 0; i < (size_t) linenum_width - len; i++) add_pfx(' ', AT_NORMAL); for (i = 0; i < len; i++) add_pfx(buf[i], AT_BOLD|AT_COLOR_LINENUM); add_pfx(' ', AT_NORMAL); } - end_column = linebuf.pfx_end; + end_column = (int) linebuf.pfx_end; /*{{type-issue}}*/ } /* * Return the width of the line prefix (status column and line number). * {{ Actual line number can be wider than linenum_width. }} */ -public int line_pfx_width(void) +public size_t line_pfx_width(void) { - int width = 0; + size_t width = 0; if (status_col) - width += status_col_width; + width += (size_t) status_col_width; /*{{type-issue}}*/ if (linenums == OPT_ONPLUS) - width += linenum_width + 1; + width += (size_t) linenum_width + 1; /*{{type-issue}}*/ return width; } @@ -397,12 +397,12 @@ public int line_pfx_width(void) */ public void pshift_all(void) { - int i; + size_t i; for (i = linebuf.print; i < linebuf.end; i++) if (linebuf.attr[i] == AT_ANSI) xbuf_add_char(&shifted_ansi, linebuf.buf[i]); linebuf.end = linebuf.print; - end_column = linebuf.pfx_end; + end_column = (int) linebuf.pfx_end; /*{{type-issue}}*/ } /* @@ -465,7 +465,7 @@ public int pwidth(LWCHAR ch, int a, LWCHAR prev_ch, int prev_a) * Backspace moves backwards one or two positions. */ if (prev_a & (AT_ANSI|AT_BINARY)) - return strlen(prchar('\b')); + return (int) strlen(prchar('\b')); /*{{type-issue}}*/ return (utf_mode && is_wide_char(prev_ch)) ? -2 : -1; } @@ -531,7 +531,7 @@ static int backc(void) { LWCHAR prev_ch; int width; - linebuf.end = (int) (p - linebuf.buf); + linebuf.end = ptr_diff(p, linebuf.buf); prev_ch = step_char(&p, -1, linebuf.buf); width = pwidth(ch, linebuf.attr[linebuf.end], prev_ch, linebuf.attr[linebuf.end-1]); end_column -= width; @@ -680,13 +680,14 @@ static int fits_on_screen(int w, int a) static int store_char(LWCHAR ch, int a, constant char *rep, POSITION pos) { int w; - int i; - int replen; + size_t i; + size_t replen; char cs; + int ov; - i = (a & (AT_UNDERLINE|AT_BOLD)); - if (i != AT_NORMAL) - last_overstrike = i; + ov = (a & (AT_UNDERLINE|AT_BOLD)); + if (ov != AT_NORMAL) + last_overstrike = ov; #if HILITE_SEARCH { @@ -787,7 +788,7 @@ static int store_char(LWCHAR ch, int a, constant char *rep, POSITION pos) if (linebuf.end > linebuf.print) { /* Shift left enough to put last byte of this char at print-1. */ - int i; + size_t i; for (i = 0; i < linebuf.print; i++) { linebuf.buf[i] = linebuf.buf[i+replen]; @@ -831,7 +832,7 @@ static int store_string(constant char *s, int a, POSITION pos) static int store_tab(int attr, POSITION pos) { - int to_tab = end_column - linebuf.pfx_end; + int to_tab = end_column - (int) linebuf.pfx_end; /*{{type-issue}}*/ if (ntabstops < 2 || to_tab >= tabstops[ntabstops-1]) to_tab = tabdefault - @@ -1005,13 +1006,13 @@ static int store_ansi(LWCHAR ch, constant char *rep, POSITION pos) { /* Remove whole unrecognized sequence. */ constant char *start = (cshift < hshift) ? xbuf_char_data(&shifted_ansi): linebuf.buf; - int *end = (cshift < hshift) ? &shifted_ansi.end : &linebuf.end; + size_t *end = (cshift < hshift) ? &shifted_ansi.end : &linebuf.end; constant char *p = start + *end; LWCHAR bch; do { bch = step_charc(&p, -1, start); } while (p > start && !IS_CSI_START(bch)); - *end = (int) (p - start); + *end = ptr_diff(p, start); } xbuf_reset(&last_ansi); ansi_done(line_ansi); @@ -1191,7 +1192,7 @@ public void pdone(int endline, int chopped, int forw) { /* We've already written in the rightmost char. */ end_column = right_column; - linebuf.end = right_curr; + linebuf.end = (size_t) right_curr; } add_attr_normal(); while (end_column < sc_width-1 + cshift) @@ -1262,7 +1263,7 @@ public void pdone(int endline, int chopped, int forw) */ public void set_attr_line(int a) { - int i; + size_t i; for (i = linebuf.print; i < linebuf.end; i++) if ((linebuf.attr[i] & AT_COLOR) == 0 || (a & AT_COLOR) == 0) @@ -1282,7 +1283,7 @@ public void set_status_col(char c, int attr) * Return the character as the function return value, * and the character attribute in *ap. */ -public int gline(int i, int *ap) +public int gline(size_t i, int *ap) { if (is_null_line) { @@ -1328,9 +1329,9 @@ public void null_line(void) * lines which are not split for screen width. * {{ This is supposed to be more efficient than forw_line(). }} */ -public POSITION forw_raw_line_len(POSITION curr_pos, int read_len, constant char **linep, int *line_lenp) +public POSITION forw_raw_line_len(POSITION curr_pos, ssize_t read_len, constant char **linep, size_t *line_lenp) { - int n; + size_t n; int c; POSITION new_pos; @@ -1374,7 +1375,7 @@ public POSITION forw_raw_line_len(POSITION curr_pos, int read_len, constant char return (new_pos); } -public POSITION forw_raw_line(POSITION curr_pos, constant char **linep, int *line_lenp) +public POSITION forw_raw_line(POSITION curr_pos, constant char **linep, size_t *line_lenp) { return forw_raw_line_len(curr_pos, -1, linep, line_lenp); } @@ -1383,9 +1384,9 @@ public POSITION forw_raw_line(POSITION curr_pos, constant char **linep, int *lin * Analogous to back_line(), but deals with "raw lines". * {{ This is supposed to be more efficient than back_line(). }} */ -public POSITION back_raw_line(POSITION curr_pos, constant char **linep, int *line_lenp) +public POSITION back_raw_line(POSITION curr_pos, constant char **linep, size_t *line_lenp) { - int n; + size_t n; int c; POSITION new_pos; @@ -1419,7 +1420,7 @@ public POSITION back_raw_line(POSITION curr_pos, constant char **linep, int *lin } if (n <= 0) { - int old_size_linebuf = size_linebuf; + size_t old_size_linebuf = size_linebuf; char *fm; char *to; if (expand_linebuf()) @@ -1453,12 +1454,12 @@ public POSITION back_raw_line(POSITION curr_pos, constant char **linep, int *lin * Skip cols printable columns at the start of line. * Return number of bytes skipped. */ -public int skip_columns(int cols, constant char **linep, int *line_lenp) +public int skip_columns(int cols, constant char **linep, size_t *line_lenp) { constant char *line = *linep; constant char *eline = line + *line_lenp; LWCHAR pch = 0; - int bytes; + size_t bytes; while (cols > 0 && line < eline) { @@ -1476,10 +1477,10 @@ public int skip_columns(int cols, constant char **linep, int *line_lenp) pch = ch; } } - bytes = line - *linep; + bytes = ptr_diff(line, *linep); *linep = line; *line_lenp -= bytes; - return (bytes); + return (int) bytes; /*{{type-issue}}*/ } /* diff --git a/lsystem.c b/lsystem.c index 61d109b8..72c86c1c 100644 --- a/lsystem.c +++ b/lsystem.c @@ -135,7 +135,7 @@ public void lsystem(constant char *cmd, constant char *donemsg) char *esccmd = shell_quote(cmd); if (esccmd != NULL) { - int len = (int) (strlen(shell) + strlen(esccmd) + 5); + size_t len = strlen(shell) + strlen(esccmd) + 5; p = (char *) ecalloc(len, sizeof(char)); SNPRINTF3(p, len, "%s %s %s", shell, shell_coption(), esccmd); free(esccmd); diff --git a/main.c b/main.c index 32198173..521241f8 100644 --- a/main.c +++ b/main.c @@ -147,12 +147,12 @@ static void try_utf8_locale(int *pargc, constant char ***pargv) } #endif -static int security_feature_error(constant char *type, int len, constant char *name) +static int security_feature_error(constant char *type, size_t len, constant char *name) { PARG parg; - int msglen = len+strlen(type)+64; + size_t msglen = len + strlen(type) + 64; char *msg = ecalloc(msglen, sizeof(char)); - SNPRINTF3(msg, msglen, "LESSSECURE_ALLOW: %s feature name \"%.*s\"", type, len, name); + SNPRINTF3(msg, msglen, "LESSSECURE_ALLOW: %s feature name \"%.*s\"", type, (int) len, name); parg.p_string = msg; error("%s", &parg); free(msg); @@ -162,7 +162,7 @@ static int security_feature_error(constant char *type, int len, constant char *n /* * Return the SF_xxx value of a secure feature given the name of the feature. */ -static int security_feature(constant char *name, int len) +static int security_feature(constant char *name, size_t len) { struct secure_feature { constant char *name; int sf_value; }; static struct secure_feature features[] = { @@ -221,7 +221,7 @@ static void init_secure(void) estr = strchr(str, ','); if (estr == NULL) estr = str + strlen(str); while (estr > str && estr[-1] == ' ') --estr; /* trim trailing spaces */ - secure_allow_features |= security_feature(str, estr-str); + secure_allow_features |= security_feature(str, ptr_diff(estr, str)); str = estr; } } @@ -489,7 +489,7 @@ public void out_of_memory(void) * Allocate memory. * Like calloc(), but never returns an error (NULL). */ -public void * ecalloc(int count, unsigned int size) +public void * ecalloc(size_t count, size_t size) { void * p; @@ -522,11 +522,11 @@ public constant char * skipspc(constant char *s) * If uppercase is true, the first string must begin with an uppercase * character; the remainder of the first string may be either case. */ -public int sprefix(constant char *ps, constant char *s, int uppercase) +public size_t sprefix(constant char *ps, constant char *s, int uppercase) { - int c; - int sc; - int len = 0; + char c; + char sc; + size_t len = 0; for ( ; *s != '\0'; s++, ps++) { @@ -534,7 +534,7 @@ public int sprefix(constant char *ps, constant char *s, int uppercase) if (uppercase) { if (len == 0 && ASCII_IS_LOWER(c)) - return (-1); + return (0); if (ASCII_IS_UPPER(c)) c = ASCII_TO_LOWER(c); } diff --git a/optfunc.c b/optfunc.c index ca885402..d6330ed1 100644 --- a/optfunc.c +++ b/optfunc.c @@ -199,9 +199,9 @@ static void query_fraction(int value, long fraction, constant char *int_msg, con } else { char buf[INT_STRLEN_BOUND(long)+2]; - int len; + size_t len; SNPRINTF1(buf, sizeof(buf), ".%06ld", fraction); - len = (int) strlen(buf); + len = strlen(buf); while (len > 2 && buf[len-1] == '0') len--; buf[len] = '\0'; @@ -475,7 +475,7 @@ public void opt_b(int type, constant char *s) /* * Set the new number of buffers. */ - ch_setbufspace(bufspace); + ch_setbufspace((size_t) bufspace); break; case QUERY: break; @@ -688,7 +688,7 @@ public void opt_D(int type, constant char *s) /* */ -public void set_tabs(constant char *s, int len) +public void set_tabs(constant char *s, size_t len) { int i; constant char *es = s + len; diff --git a/opttbl.c b/opttbl.c index e1c9bf1f..0ed57fce 100644 --- a/opttbl.c +++ b/opttbl.c @@ -804,11 +804,11 @@ public struct loption * findopt_name(constant char **p_optname, constant char ** constant char *optname = *p_optname; struct loption *o; struct optname *oname; - int len; + size_t len; int uppercase; struct loption *maxo = NULL; struct optname *maxoname = NULL; - int maxlen = 0; + size_t maxlen = 0; int ambig = 0; int exact = 0; @@ -830,7 +830,7 @@ public struct loption * findopt_name(constant char **p_optname, constant char ** for (uppercase = 0; uppercase <= 1; uppercase++) { len = sprefix(optname, oname->oname, uppercase); - if (len <= 0 || is_optchar(optname[len])) + if (len == 0 || is_optchar(optname[len])) { /* * We didn't use all of the option name. diff --git a/os.c b/os.c index 6d27e7e4..961b4899 100644 --- a/os.c +++ b/os.c @@ -163,9 +163,9 @@ public int supports_ctrl_x(void) * A call to intread() from a signal handler will interrupt * any pending iread(). */ -public int iread(int fd, unsigned char *buf, unsigned int len) +public ssize_t iread(int fd, unsigned char *buf, size_t len) { - int n; + ssize_t n; start: #if MSDOS_COMPILER==WIN32C @@ -360,7 +360,7 @@ public char * errno_message(constant char *filename) { char *p; char *m; - int len; + size_t len; #if HAVE_ERRNO #if MUST_DEFINE_ERRNO extern int errno; @@ -369,7 +369,7 @@ public char * errno_message(constant char *filename) #else p = "cannot open"; #endif - len = (int) (strlen(filename) + strlen(p) + 3); + len = strlen(filename) + strlen(p) + 3; m = (char *) ecalloc(len, sizeof(char)); SNPRINTF2(m, len, "%s: %s", filename, p); return (m); @@ -453,7 +453,7 @@ char * strchr(char *s, char c) #endif #if !HAVE_MEMCPY -void * memcpy(void *dst, void *src, int len) +void * memcpy(void *dst, void *src, size_t len) { char *dstp = (char *) dst; char *srcp = (char *) src; diff --git a/output.c b/output.c index eae109f6..f975a837 100644 --- a/output.c +++ b/output.c @@ -51,7 +51,7 @@ extern int vt_enabled; public void put_line(void) { int c; - int i; + size_t i; int a; if (ABORT_SIGS()) @@ -291,7 +291,7 @@ static void win_flush(void) * Leave it unprocessed * in the buffer. */ - int slop = (int) (q - anchor); + size_t slop = ptr_diff(q, anchor); /* {{ strcpy args overlap! }} */ strcpy(obuf, anchor); ob = &obuf[slop]; @@ -345,9 +345,9 @@ static void win_flush(void) */ public void flush(void) { - int n; + size_t n; - n = (int) (ob - obuf); + n = ptr_diff(ob, obuf); if (n == 0) return; ob = obuf; diff --git a/pattern.c b/pattern.c index 3c24e06b..e4f9e5e3 100644 --- a/pattern.c +++ b/pattern.c @@ -152,7 +152,7 @@ public int compile_pattern(constant char *pattern, int search_type, int show_err } else { char *cvt_pattern = (char*) ecalloc(1, cvt_length(strlen(pattern), CVT_TO_LC)); - cvt_text(cvt_pattern, pattern, (int *)NULL, (int *)NULL, CVT_TO_LC); + cvt_text(cvt_pattern, pattern, NULL, NULL, CVT_TO_LC); result = compile_pattern2(cvt_pattern, search_type, comp_pattern, show_error); free(cvt_pattern); } @@ -257,7 +257,7 @@ public int is_null_pattern(PATTERN_TYPE pattern) * Simple pattern matching function. * It supports no metacharacters like *, etc. */ -static int match(constant char *pattern, int pattern_len, constant char *buf, int buf_len, constant char ***sp, constant char ***ep, int nsubs) +static int match(constant char *pattern, size_t pattern_len, constant char *buf, int buf_len, constant char ***sp, constant char ***ep, int nsubs) { constant char *pp; constant char *lp; @@ -294,9 +294,10 @@ static int match(constant char *pattern, int pattern_len, constant char *buf, in * Set sp[i] and ep[i] to the start and end of the i-th matched subpattern. * Subpatterns are defined by parentheses in the regex language. */ -static int match_pattern1(PATTERN_TYPE pattern, constant char *tpattern, constant char *line, int line_len, constant char **sp, constant char **ep, int nsp, int notbol, int search_type) +static int match_pattern1(PATTERN_TYPE pattern, constant char *tpattern, constant char *line, size_t aline_len, constant char **sp, constant char **ep, int nsp, int notbol, int search_type) { int matched; + int line_len = (int) aline_len; /*{{type-issue}}*/ #if NO_REGEX search_type |= SRCH_NO_REGEX; @@ -442,7 +443,7 @@ static int match_pattern1(PATTERN_TYPE pattern, constant char *tpattern, constan return (matched); } -public int match_pattern(PATTERN_TYPE pattern, constant char *tpattern, constant char *line, int line_len, constant char **sp, constant char **ep, int nsp, int notbol, int search_type) +public int match_pattern(PATTERN_TYPE pattern, constant char *tpattern, constant char *line, size_t line_len, constant char **sp, constant char **ep, int nsp, int notbol, int search_type) { int matched = match_pattern1(pattern, tpattern, line, line_len, sp, ep, nsp, notbol, search_type); int i; diff --git a/position.c b/position.c index 3cc9fec3..5103d38b 100644 --- a/position.c +++ b/position.c @@ -113,7 +113,7 @@ public void pos_init(void) free((char*)table); } else scrpos.pos = NULL_POSITION; - table = (POSITION *) ecalloc(sc_height, sizeof(POSITION)); + table = (POSITION *) ecalloc((size_t) sc_height, sizeof(POSITION)); /*{{type-issue}}*/ table_size = sc_height; pos_clear(); if (scrpos.pos != NULL_POSITION) @@ -243,10 +243,10 @@ public int sindex_from_sline(int sline) * return the number of characters (not bytes) between the * beginning of the line and the first byte of the choff character. */ -static int pos_shift(POSITION linepos, int choff) +static int pos_shift(POSITION linepos, size_t choff) { constant char *line; - int line_len; + size_t line_len; POSITION pos; int cvt_ops; char *cline; @@ -259,7 +259,7 @@ static int pos_shift(POSITION linepos, int choff) cline = (char *) ecalloc(1, line_len+1); cvt_text(cline, line, NULL, &line_len, cvt_ops); free(cline); - return line_len; + return (int) line_len; /*{{type-issue}}*/ } /* @@ -297,6 +297,6 @@ public void pos_rehead(void) if (linepos == tpos) return; table[TOP] = linepos; - hshift = pos_shift(linepos, (int)(tpos - linepos)); + hshift = pos_shift(linepos, (size_t) (tpos - linepos)); screen_trashed(); } diff --git a/prompt.c b/prompt.c index 5835fca2..9e63205d 100644 --- a/prompt.c +++ b/prompt.c @@ -79,11 +79,11 @@ public void init_prompt(void) */ static void ap_str(constant char *s) { - int len; + size_t len; - len = (int) strlen(s); + len = strlen(s); if (mp + len >= message + PROMPT_SIZE) - len = (int) (message + PROMPT_SIZE - mp - 1); + len = ptr_diff(message, mp) + PROMPT_SIZE - 1; strncpy(mp, s, len); mp += len; *mp = '\0'; diff --git a/screen.c b/screen.c index 424f1ad5..1f4e847d 100644 --- a/screen.c +++ b/screen.c @@ -1655,7 +1655,7 @@ static void ltputs(constant char *str, int affcnt, int (*f_putc)(int)) if (obrac != NULL) { char str2[64]; - int slen = obrac - str; + size_t slen = ptr_diff(obrac, str); if (slen < sizeof(str2)) { int delay; diff --git a/search.c b/search.c index 401a078b..da12c0b3 100644 --- a/search.c +++ b/search.c @@ -42,7 +42,7 @@ extern int header_lines; extern int header_cols; #if HILITE_SEARCH extern int hilite_search; -extern int size_linebuf; +extern size_t size_linebuf; extern int squished; extern int can_goto_line; static int hide_hilite; @@ -573,18 +573,18 @@ public POSITION prev_unfiltered(POSITION pos) return (pos); } -static void shift_visible(int start_off, int end_off, int line_len) +static void shift_visible(size_t start_off, size_t end_off, size_t line_len) { - int swidth = sc_width - line_pfx_width(); + size_t swidth = (size_t) sc_width - line_pfx_width(); int new_hshift; if (end_off < swidth) /* whole string is in first screen */ new_hshift = 0; else if (start_off >= line_len - swidth) /* whole string is in last screen */ - new_hshift = line_len - swidth; - else if (start_off > hshift && end_off < hshift + swidth) + new_hshift = (int) (line_len - swidth); + else if (start_off > (size_t) hshift && end_off < (size_t) hshift + swidth) /*{{type-issue}}*/ new_hshift = hshift; /* already visible; leave hshift unchanged */ else /* shift it to column match_shift */ - new_hshift = (start_off < match_shift) ? 0 : (start_off - match_shift); + new_hshift = (start_off < (size_t) match_shift) ? 0 : (int) (start_off - (size_t) match_shift); /*{{type-issue}}*/ if (new_hshift != hshift) { hshift = new_hshift; @@ -661,7 +661,7 @@ static struct hilite_storage * hlist_getstorage(struct hilite_tree *anchor) } s = (struct hilite_storage *) ecalloc(1, sizeof(struct hilite_storage)); - s->nodes = (struct hilite_node *) ecalloc(capacity, sizeof(struct hilite_node)); + s->nodes = (struct hilite_node *) ecalloc((size_t) capacity, sizeof(struct hilite_node)); s->capacity = capacity; s->used = 0; s->next = NULL; @@ -925,10 +925,10 @@ static void add_hilite(struct hilite_tree *anchor, struct hilite *hl) */ static void create_hilites(POSITION linepos, constant char *line, constant char *sp, constant char *ep, int attr, int *chpos) { - int start_index = sp - line; - int end_index = ep - line; + size_t start_index = ptr_diff(sp, line); /*{{type-issue}}*/ + size_t end_index = ptr_diff(ep, line); struct hilite hl; - int i; + size_t i; /* Start the first hilite. */ hl.hl_startpos = linepos + chpos[start_index]; @@ -962,7 +962,7 @@ static void create_hilites(POSITION linepos, constant char *line, constant char * the current pattern. * sp,ep delimit the first match already found. */ -static void hilite_line(POSITION linepos, constant char *line, int line_len, int *chpos, constant char **sp, constant char **ep, int nsp, int cvt_ops) +static void hilite_line(POSITION linepos, constant char *line, size_t line_len, int *chpos, constant char **sp, constant char **ep, int nsp, int cvt_ops) { constant char *searchp; constant char *line_end = line + line_len; @@ -1013,7 +1013,7 @@ static void hilite_line(POSITION linepos, constant char *line, int line_len, int else /* end of line */ break; } while (match_pattern(info_compiled(&search_info), search_info.text, - searchp, line_end - searchp, sp, ep, nsp, 1, search_info.search_type)); + searchp, ptr_diff(line_end, searchp), sp, ep, nsp, 1, search_info.search_type)); } #endif @@ -1147,7 +1147,7 @@ static POSITION search_pos(int search_type) * If so, add an entry to the filter list. */ #if HILITE_SEARCH -static int matches_filters(POSITION pos, char *cline, int line_len, int *chpos, POSITION linepos, constant char **sp, constant char **ep, int nsp) +static int matches_filters(POSITION pos, char *cline, size_t line_len, int *chpos, POSITION linepos, constant char **sp, constant char **ep, int nsp) { struct pattern_info *filter; @@ -1198,19 +1198,19 @@ static int search_range(POSITION pos, POSITION endpos, int search_type, int matc { constant char *line; char *cline; - int line_len; + size_t line_len; LINENUM linenum; #define NSP (NUM_SEARCH_COLORS+2) constant char *sp[NSP]; constant char *ep[NSP]; int line_match; int cvt_ops; - int cvt_len; + size_t cvt_len; int *chpos; POSITION linepos, oldpos; int skip_bytes = 0; - int swidth = sc_width - line_pfx_width(); - int sheight = sc_height - sindex_from_sline(jump_sline); + size_t swidth = (size_t) sc_width - line_pfx_width(); /*{{type-issue}}*/ + size_t sheight = (size_t) sc_height - sindex_from_sline(jump_sline); linenum = find_linenum(pos); if (nosearch_headers && linenum <= header_lines) @@ -1405,8 +1405,8 @@ static int search_range(POSITION pos, POSITION endpos, int search_type, int matc */ if (sp[0] != NULL && ep[0] != NULL) { - int start_off = sp[0] - cline; - int end_off = ep[0] - cline; + size_t start_off = ptr_diff(sp[0], cline); + size_t end_off = ptr_diff(ep[0], cline); shift_visible(start_off, end_off, line_len); } } else if (plastlinepos != NULL) @@ -1421,7 +1421,7 @@ static int search_range(POSITION pos, POSITION endpos, int search_type, int matc */ if (ep[0] != NULL) { - int end_off = ep[0] - cline; + size_t end_off = ptr_diff(ep[0], cline); if (end_off >= swidth * sheight / 4) /* heuristic */ *plastlinepos = get_lastlinepos(linepos, linepos + chpos[end_off], sheight); } @@ -1665,11 +1665,11 @@ public void prep_hilite(POSITION spos, POSITION epos, int maxlines) int result; int i; -/* - * Search beyond where we're asked to search, so the prep region covers - * more than we need. Do one big search instead of a bunch of small ones. - */ -#define SEARCH_MORE (3*size_linebuf) + /* + * Search beyond where we're asked to search, so the prep region covers + * more than we need. Do one big search instead of a bunch of small ones. + */ + POSITION SEARCH_MORE = (POSITION) (3*size_linebuf); if (!prev_pattern(&search_info) && !is_filtering()) return; diff --git a/tags.c b/tags.c index 5f533ef6..86cb18dc 100644 --- a/tags.c +++ b/tags.c @@ -255,7 +255,7 @@ static enum tag_result findctag(constant char *tag) char *p; char *q; FILE *f; - int taglen; + size_t taglen; LINENUM taglinenum; char *tagfile; char *tagpattern; @@ -273,7 +273,7 @@ static enum tag_result findctag(constant char *tag) cleantags(); total = 0; - taglen = (int) strlen(tag); + taglen = strlen(tag); /* * Search the tags file for the desired tag. @@ -384,7 +384,7 @@ static int curtag_match(char constant *line, POSITION linepos) * If tagendline is set, make sure we match all * the way to end of line (no extra chars after the match). */ - int len = (int) strlen(curtag->tag_pattern); + size_t len = strlen(curtag->tag_pattern); if (strncmp(curtag->tag_pattern, line, len) == 0 && (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r')) { @@ -407,7 +407,7 @@ static POSITION ctagsearch(void) { POSITION pos, linepos; LINENUM linenum; - int line_len; + size_t line_len; constant char *line; int found; @@ -456,7 +456,7 @@ static POSITION ctagsearch(void) } else { int cvt_ops = CVT_ANSI; - int cvt_len = cvt_length(line_len, cvt_ops); + size_t cvt_len = cvt_length(line_len, cvt_ops); int *chpos = cvt_alloc_chpos(cvt_len); char *cline = (char *) ecalloc(1, cvt_len); cvt_text(cline, line, chpos, &line_len, cvt_ops); @@ -552,7 +552,7 @@ static enum tag_result findgtag(constant char *tag, int type) constant char *name; constant char *file; constant char *line; - int len; + size_t len; if (sigs) { @@ -562,7 +562,7 @@ static enum tag_result findgtag(constant char *tag, int type) #endif return TAG_INTR; } - len = (int) strlen(buf); + len = strlen(buf); if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0'; else diff --git a/ttyin.c b/ttyin.c index 974438f7..8d7b87cf 100644 --- a/ttyin.c +++ b/ttyin.c @@ -179,7 +179,7 @@ public int default_wheel_lines(void) public int getchr(void) { char c; - int result; + ssize_t result; do { diff --git a/xbuf.c b/xbuf.c index b7c86649..0b332774 100644 --- a/xbuf.c +++ b/xbuf.c @@ -9,7 +9,7 @@ public void xbuf_init(struct xbuffer *xbuf) xbuf_init_size(xbuf, 16); } -public void xbuf_init_size(struct xbuffer *xbuf, int init_size) +public void xbuf_init_size(struct xbuffer *xbuf, size_t init_size) { xbuf->data = NULL; xbuf->size = xbuf->end = 0; @@ -66,9 +66,9 @@ public void xbuf_add_char(struct xbuffer *xbuf, char c) /* * Add arbitrary data to an xbuf. */ -public void xbuf_add_data(struct xbuffer *xbuf, constant unsigned char *data, int len) +public void xbuf_add_data(struct xbuffer *xbuf, constant unsigned char *data, size_t len) { - int i; + size_t i; for (i = 0; i < len; i++) xbuf_add_byte(xbuf, data[i]); } diff --git a/xbuf.h b/xbuf.h index 5a334a61..a5bdefb3 100644 --- a/xbuf.h +++ b/xbuf.h @@ -6,18 +6,18 @@ struct xbuffer { unsigned char *data; - int end; - int size; - int init_size; + size_t end; + size_t size; + size_t init_size; }; void xbuf_init(struct xbuffer *xbuf); -void xbuf_init_size(struct xbuffer *xbuf, int init_size); +void xbuf_init_size(struct xbuffer *xbuf, size_t init_size); void xbuf_deinit(struct xbuffer *xbuf); void xbuf_reset(struct xbuffer *xbuf); void xbuf_add_byte(struct xbuffer *xbuf, unsigned char b); void xbuf_add_char(struct xbuffer *xbuf, char c); -void xbuf_add_data(struct xbuffer *xbuf, constant unsigned char *data, int len); +void xbuf_add_data(struct xbuffer *xbuf, constant unsigned char *data, size_t len); int xbuf_pop(struct xbuffer *xbuf); constant char *xbuf_char_data(constant struct xbuffer *xbuf);