less: move code to count lines into a separate function
function old new delta update_num_lines - 159 +159 m_status_print 409 266 -143 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/1 up/down: 159/-143) Total: 16 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
ae1a9e899e
commit
159e032bf4
@ -609,6 +609,35 @@ static int safe_lineno(int fline)
|
|||||||
return LINENO(flines[fline]) + 1;
|
return LINENO(flines[fline]) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* count number of lines in file */
|
||||||
|
static void update_num_lines(void)
|
||||||
|
{
|
||||||
|
int count, fd;
|
||||||
|
ssize_t len, i;
|
||||||
|
char buf[4096];
|
||||||
|
struct stat stbuf;
|
||||||
|
|
||||||
|
if (num_lines == READING_FILE) {
|
||||||
|
count = 0;
|
||||||
|
fd = open(filename, O_RDONLY);
|
||||||
|
if (fd < 0)
|
||||||
|
goto skip;
|
||||||
|
if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode))
|
||||||
|
goto do_close;
|
||||||
|
while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
|
||||||
|
for (i = 0; i < len; ++i) {
|
||||||
|
if (buf[i] == '\n' && ++count == MAXLINES)
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
done:
|
||||||
|
num_lines = count;
|
||||||
|
do_close:
|
||||||
|
close(fd);
|
||||||
|
skip: ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Print a status line if -M was specified */
|
/* Print a status line if -M was specified */
|
||||||
static void m_status_print(void)
|
static void m_status_print(void)
|
||||||
{
|
{
|
||||||
@ -629,32 +658,7 @@ static void m_status_print(void)
|
|||||||
: safe_lineno(cur_fline + max_displayed_line);
|
: safe_lineno(cur_fline + max_displayed_line);
|
||||||
printf(" lines %i-%i", first, last);
|
printf(" lines %i-%i", first, last);
|
||||||
|
|
||||||
if (num_lines == READING_FILE) {
|
update_num_lines();
|
||||||
int count, fd;
|
|
||||||
ssize_t len, i;
|
|
||||||
char buf[4096];
|
|
||||||
struct stat stbuf;
|
|
||||||
|
|
||||||
/* count number of lines in file */
|
|
||||||
count = 0;
|
|
||||||
fd = open(filename, O_RDONLY);
|
|
||||||
if (fd < 0)
|
|
||||||
goto skip;
|
|
||||||
if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode))
|
|
||||||
goto do_close;
|
|
||||||
while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
|
|
||||||
for (i = 0; i < len; ++i) {
|
|
||||||
if (buf[i] == '\n' && ++count == MAXLINES)
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
done:
|
|
||||||
num_lines = count;
|
|
||||||
do_close:
|
|
||||||
close(fd);
|
|
||||||
skip: ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (num_lines >= 0)
|
if (num_lines >= 0)
|
||||||
printf("/%i", num_lines);
|
printf("/%i", num_lines);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user