| 20 |
#include "log.h" |
#include "log.h" |
| 21 |
#include "io.h" |
#include "io.h" |
| 22 |
#include "screen.h" |
#include "screen.h" |
| 23 |
|
#include <fcntl.h> |
| 24 |
#include <string.h> |
#include <string.h> |
| 25 |
#include <ctype.h> |
#include <ctype.h> |
| 26 |
#include <unistd.h> |
#include <unistd.h> |
| 27 |
#include <stdlib.h> |
#include <stdlib.h> |
| 28 |
|
#include <errno.h> |
| 29 |
#include <sys/types.h> |
#include <sys/types.h> |
| 30 |
#include <sys/stat.h> |
#include <sys/stat.h> |
| 31 |
#include <sys/param.h> |
#include <sys/param.h> |
| 32 |
|
#include <sys/mman.h> |
| 33 |
|
|
| 34 |
#define ACTIVE_BOARD_HEIGHT 8 |
#define ACTIVE_BOARD_HEIGHT 8 |
| 35 |
|
|
| 227 |
return len; |
return len; |
| 228 |
} |
} |
| 229 |
|
|
|
int display_file(const char *filename) |
|
|
{ |
|
|
char buffer[LINE_BUFFER_LEN]; |
|
|
FILE *fin; |
|
|
size_t i; |
|
|
|
|
|
if ((fin = fopen(filename, "r")) == NULL) |
|
|
{ |
|
|
return -1; |
|
|
} |
|
|
|
|
|
while (fgets(buffer, sizeof(buffer) - 1, fin)) |
|
|
{ |
|
|
i = strnlen(buffer, sizeof(buffer) - 1); |
|
|
if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r') |
|
|
{ |
|
|
buffer[i - 1] = '\r'; |
|
|
buffer[i] = '\n'; |
|
|
buffer[i + 1] = '\0'; |
|
|
} |
|
|
prints(buffer); |
|
|
iflush(); |
|
|
} |
|
|
fclose(fin); |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
| 230 |
int display_file_ex(const char *filename, int begin_line, int wait) |
int display_file_ex(const char *filename, int begin_line, int wait) |
| 231 |
{ |
{ |
| 232 |
static int show_help = 1; |
static int show_help = 1; |
| 233 |
char buffer[LINE_BUFFER_LEN]; |
char buffer[LINE_BUFFER_LEN]; |
| 234 |
|
void *p_data; |
| 235 |
int ch = 0; |
int ch = 0; |
| 236 |
int input_ok, line, max_lines; |
int input_ok, line, max_lines; |
| 237 |
long int c_line_current = 0; |
long int c_line_current = 0; |
| 238 |
long int c_line_total = 0; |
long int c_line_total = 0; |
| 239 |
FILE *fin; |
int fd; |
| 240 |
|
struct stat sb; |
| 241 |
long *p_line_offsets; |
long *p_line_offsets; |
| 242 |
long int len; |
long int len; |
| 243 |
long int percentile; |
long int percentile; |
| 244 |
int loop = 1; |
int loop = 1; |
| 245 |
|
|
| 246 |
if ((fin = fopen(filename, "r")) == NULL) |
if ((fd = open(filename, O_RDONLY)) < 0) |
| 247 |
|
{ |
| 248 |
|
log_error("open(%s) error (%d)\n", filename, errno); |
| 249 |
|
return -1; |
| 250 |
|
} |
| 251 |
|
|
| 252 |
|
if (fstat(fd, &sb) < 0) |
| 253 |
|
{ |
| 254 |
|
log_error("fstat(fd) error (%d)\n", errno); |
| 255 |
|
return -1; |
| 256 |
|
} |
| 257 |
|
|
| 258 |
|
p_data = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0L); |
| 259 |
|
if (p_data == MAP_FAILED) |
| 260 |
|
{ |
| 261 |
|
log_error("mmap() error (%d)\n", errno); |
| 262 |
|
return -2; |
| 263 |
|
} |
| 264 |
|
|
| 265 |
|
if (close(fd) < 0) |
| 266 |
{ |
{ |
| 267 |
log_error("Unable to open file %s\n", filename); |
log_error("close(fd) error (%d)\n", errno); |
| 268 |
return -1; |
return -1; |
| 269 |
} |
} |
| 270 |
|
|
| 271 |
p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES); |
p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES); |
| 272 |
|
|
| 273 |
c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES); |
c_line_total = split_data_lines(p_data, screen_cols, p_line_offsets, MAX_FILE_LINES); |
| 274 |
|
|
| 275 |
clrline(begin_line, screen_rows); |
clrline(begin_line, screen_rows); |
| 276 |
line = begin_line; |
line = begin_line; |
| 425 |
continue; |
continue; |
| 426 |
} |
} |
| 427 |
|
|
|
fseek(fin, p_line_offsets[c_line_current], SEEK_SET); |
|
| 428 |
len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current]; |
len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current]; |
| 429 |
if (len >= LINE_BUFFER_LEN) |
if (len >= LINE_BUFFER_LEN) |
| 430 |
{ |
{ |
| 431 |
log_error("Error length exceeds buffer size: %d\n", len); |
log_error("Error length exceeds buffer size: %d\n", len); |
| 432 |
len = LINE_BUFFER_LEN - 1; |
len = LINE_BUFFER_LEN - 1; |
| 433 |
} |
} |
| 434 |
if (fgets(buffer, (int)len + 1, fin) == NULL) |
|
| 435 |
{ |
memcpy(buffer, (const char *)p_data + p_line_offsets[c_line_current], (size_t)len); |
| 436 |
log_error("Reach EOF\n"); |
buffer[len] = '\0'; |
| 437 |
break; |
|
|
} |
|
| 438 |
moveto(line, 0); |
moveto(line, 0); |
| 439 |
clrtoeol(); |
clrtoeol(); |
| 440 |
prints("%s", buffer); |
prints("%s", buffer); |
| 443 |
} |
} |
| 444 |
|
|
| 445 |
cleanup: |
cleanup: |
| 446 |
|
if (munmap(p_data, (size_t)sb.st_size) < 0) |
| 447 |
|
{ |
| 448 |
|
log_error("munmap() error (%d)\n", errno); |
| 449 |
|
} |
| 450 |
|
|
| 451 |
free(p_line_offsets); |
free(p_line_offsets); |
|
fclose(fin); |
|
| 452 |
|
|
| 453 |
return ch; |
return ch; |
| 454 |
} |
} |