Guess what? More anonymous unions!

This commit is contained in:
rsc 2004-03-25 23:25:45 +00:00
parent 285b4f85c0
commit 1a84af59e0
2 changed files with 11 additions and 11 deletions

View file

@ -76,12 +76,12 @@ EXTERN union
* if a line requires multiple reads, we keep shifting * if a line requires multiple reads, we keep shifting
* buf down into pre and then do another read into * buf down into pre and then do another read into
* buf. so you'll get the last 16-32k of the matching line. * buf. so you'll get the last 16-32k of the matching line.
* if pre were smaller than buf you'd get a suffix of the * if h were smaller than buf you'd get a suffix of the
* line with a hole cut out. * line with a hole cut out.
*/ */
uchar pre[16*1024]; /* to save to previous '\n' */ uchar pre[16*1024]; /* to save to previous '\n' */
uchar buf[16*1024]; /* input buffer */ uchar buf[16*1024]; /* input buffer */
}; } u;
} u; } u;
EXTERN char *filename; EXTERN char *filename;

View file

@ -115,27 +115,27 @@ search(char *file, int flag)
eof = 0; eof = 0;
empty = 1; empty = 1;
nl = 0; nl = 0;
lp = u.buf; lp = u.u.buf;
bol = lp; bol = lp;
loop0: loop0:
n = lp-bol; n = lp-bol;
if(n > sizeof(u.pre)) if(n > sizeof(u.u.pre))
n = sizeof(u.pre); n = sizeof(u.u.pre);
memmove(u.buf-n, bol, n); memmove(u.u.buf-n, bol, n);
bol = u.buf-n; bol = u.u.buf-n;
n = read(fid, u.buf, sizeof(u.buf)); n = read(fid, u.u.buf, sizeof(u.u.buf));
/* if file has no final newline, simulate one to emit matches to last line */ /* if file has no final newline, simulate one to emit matches to last line */
if(n > 0) { if(n > 0) {
empty = 0; empty = 0;
nl = u.buf[n-1]=='\n'; nl = u.u.buf[n-1]=='\n';
} else { } else {
if(n < 0){ if(n < 0){
fprint(2, "grep: read error on %s: %r\n", file); fprint(2, "grep: read error on %s: %r\n", file);
return count != 0; return count != 0;
} }
if(!eof && !nl && !empty) { if(!eof && !nl && !empty) {
u.buf[0] = '\n'; u.u.buf[0] = '\n';
n = 1; n = 1;
eof = 1; eof = 1;
} }
@ -152,7 +152,7 @@ loop0:
Bflush(&bout); Bflush(&bout);
return count != 0; return count != 0;
} }
lp = u.buf; lp = u.u.buf;
elp = lp+n; elp = lp+n;
if(flag & Iflag) if(flag & Iflag)
goto loopi; goto loopi;