diff options
Diffstat (limited to 'ex_subr.c')
| -rw-r--r-- | ex_subr.c | 97 |
1 files changed, 67 insertions, 30 deletions
@@ -79,6 +79,7 @@ static char sccsid[] = "@(#)ex_subr.c 1.41 (gritter) 12/25/06"; /* from ex_subr.c 7.10.1 (2.11BSD) 1996/3/22 */ +#include <sys/ioctl.h> #include "ex.h" #include "ex_re.h" #include "ex_tty.h" @@ -95,7 +96,7 @@ any(int c, register char *s) { register int x; - while (x = *s++) + while ((x = *s++)) if (x == c) return (1); return (0); @@ -161,14 +162,60 @@ comment(void) ungetchar(c); } -void -Copy(register char *to, register char *from, register int size) -{ +/* + * strlcpy not used since buffers may overlap. + */ +size_t +lcpy(char *dst, const char *src, const size_t dstsize) { + size_t srclen = 0; + size_t copylen = dstsize; + /* avoids to access illegal memory in case + * of unterminated strings */ + while (srclen < copylen && src[srclen]) { + ++srclen; + } + if (srclen < copylen) { + /* '<' means there is room for the final 0 byte */ + copylen = srclen; + } else if (copylen) { + /* src string is too long. Set size to be copied to + * buffer size - 1 to have room for the final 0 byte */ + --copylen; + } + if (copylen) { + memcpy(dst, src, copylen); + } + if (dstsize) { + dst[copylen] = 0; + } + return srclen; /* [sic!] see strlcpy man page */ +} - if (size > 0) - do - *to++ = *from++; - while (--size > 0); +size_t +lcat(char *dst, const char *src, const size_t dstsize) { + size_t srclen = 0; + size_t dstlen = 0; + size_t copylen = dstsize; + while (dstlen < copylen && dst[dstlen]) { + ++dstlen; + } + dst += dstlen; + copylen -= dstlen; + while (srclen < copylen && src[srclen]) { + ++srclen; + } + if (srclen < copylen) { + copylen = srclen; + } else if (copylen) { + --copylen; + } + if (copylen) { + memcpy(dst, src, copylen); + } + if (dstsize) { + dst[dstlen + copylen] = 0; + } + return dstlen + srclen; /* [sic!] see strlcat man page */ } void @@ -340,9 +387,9 @@ killcnt(register int cnt) } if (!notable(cnt)) return; - printf(catgets(catd, 1, 170, "%d lines"), cnt); + ex_printf(catgets(catd, 1, 170, "%d lines"), cnt); if (value(TERSE) == 0) { - printf(catgets(catd, 1, 171, " %c%s"), + ex_printf(catgets(catd, 1, 171, " %c%s"), Command[0] | ' ', Command + 1); if (Command[strlen(Command) - 1] != 'e') putchar('e'); @@ -437,11 +484,11 @@ merror1(intptr_t seekpt) { #ifdef VMUNIX - strcpy(linebuf, (char *)seekpt); + lcpy(linebuf, (char *)seekpt, LBSIZE); #else lseek(erfile, (off_t) seekpt, SEEK_SET); if (read(erfile, linebuf, 128) < 2) - CP(linebuf, "ERROR"); + lcpy(linebuf, "ERROR", LBSIZE); #endif } @@ -553,7 +600,7 @@ netchange(register int i) } if (!notable(i)) return; - printf(mesg(catgets(catd, 1, 177, "%d %slines@in file after %s")), + ex_printf(mesg(catgets(catd, 1, 177, "%d %slines@in file after %s")), i, cp, Command); putNFL(); } @@ -816,7 +863,7 @@ void strcLIN(char *dp) { - CP(linebuf, dp); + lcpy(linebuf, dp, LBSIZE); } void @@ -928,9 +975,10 @@ int _ovno; void onemt(int signum) { - int oovno; + /* int oovno; unused? */ - oovno = _ovno; + (void)signum; + /* oovno = _ovno; unused? */ /* 2 and 3 are valid on 11/40 type vi, so */ if (_ovno < 0 || _ovno > 3) _ovno = 0; @@ -951,7 +999,7 @@ onemt(int signum) void onhup(int signum) { - + (void)signum; /* * USG tty driver can send multiple HUP's!! */ @@ -981,7 +1029,7 @@ onhup(int signum) void onintr(int signum) { - + (void)signum; alarm(0); /* in case we were called from map */ draino(); if (!inopen) { @@ -1061,6 +1109,7 @@ onsusp(int signum) #endif sigset_t set; + (void)signum; f = setty(normf); vnfl(); putpad(TE); @@ -1099,18 +1148,6 @@ onsusp(int signum) #endif /* SIGTSTP */ /* - * For regular strcpy(), source and destination may not overlap. - */ -char * -movestr(char *s1, const char *s2) -{ - char *cp = s1; - - while (*s1++ = *s2++); - return cp; -} - -/* * strcpy() checking the maximum size of s1, printing msg in case of overflow. */ char * |
