Hello tech@,
I'm currently working on removing the binc family tree (including
{GET,ADD}_SPACE_{RET,GOTO}) from vi. During this quest I found this gem
which I reckon can be done better with the libc native function.
This has the benefit of extra checks from getline and it echos all
system errors to the console instead of only malloc errors.
OK?
martijn@
Index: ex/ex_util.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_util.c,v
retrieving revision 1.9
diff -u -p -r1.9 ex_util.c
--- ex/ex_util.c 6 Jan 2016 22:28:52 -0000 1.9
+++ ex/ex_util.c 8 May 2016 11:25:38 -0000
@@ -73,34 +73,26 @@ int
ex_getline(SCR *sp, FILE *fp, size_t *lenp)
{
EX_PRIVATE *exp;
- size_t off;
- int ch;
- char *p;
+ ssize_t off;
+ long curr_off;
exp = EXP(sp);
- for (errno = 0, off = 0, p = exp->ibp;;) {
- if (off >= exp->ibp_len) {
- BINC_RET(sp, exp->ibp, exp->ibp_len, off + 1);
- p = exp->ibp + off;
+ curr_off = ftell(fp);
+ if ((off = getline(&(exp->ibp), &(exp->ibp_len), fp)) == -1) {
+ if (errno == EINTR) {
+ errno = 0;
+ clearerr(fp);
+ fseek(fp, curr_off, SEEK_SET);
+ return ex_getline(sp, fp, lenp);
}
- if ((ch = getc(fp)) == EOF && !feof(fp)) {
- if (errno == EINTR) {
- errno = 0;
- clearerr(fp);
- continue;
- }
- return (1);
- }
- if (ch == EOF || ch == '\n') {
- if (ch == EOF && !off)
- return (1);
- *lenp = off;
- return (0);
- }
- *p++ = ch;
- ++off;
+ if (errno)
+ msgq(sp, M_SYSERR, "getline");
+ return 1;
}
- /* NOTREACHED */
+ if (exp->ibp[off-1] == '\n')
+ exp->ibp[--off] = '\0';
+ *lenp = off;
+ return 0;
}
/*