fix cmd: tail

This commit is contained in:
mazhiyuan 2021-12-23 15:36:36 +08:00 committed by Bernard Xiong
parent bcdfa35f86
commit a2561a41b9
1 changed files with 13 additions and 9 deletions

View File

@ -612,7 +612,7 @@ static int cmd_tail(int argc, char **argv)
rt_uint32_t target_line = 0;
rt_uint32_t current_line = 0;
rt_uint32_t required_lines = 0;
rt_uint32_t after_xxx_line = 0;
rt_uint32_t start_line = 0;
if (argc < 2)
{
@ -632,7 +632,7 @@ static int cmd_tail(int argc, char **argv)
}
else
{
after_xxx_line = atoi(&argv[2][1]); /* eg: +100, to get the 100 */
start_line = atoi(&argv[2][1]); /* eg: +100, to get the 100 */
}
file_name = argv[3];
}
@ -651,6 +651,10 @@ static int cmd_tail(int argc, char **argv)
while ((read(fd, &c, sizeof(char))) > 0)
{
if(total_lines == 0)
{
total_lines++;
}
if (c == '\n')
{
total_lines++;
@ -659,11 +663,11 @@ static int cmd_tail(int argc, char **argv)
rt_kprintf("\nTotal Number of lines:%d\n", total_lines);
if (after_xxx_line != 0)
if (start_line != 0)
{
if (total_lines > after_xxx_line)
if (total_lines >= start_line)
{
required_lines = total_lines - after_xxx_line;
required_lines = total_lines - start_line + 1;
}
else
{
@ -686,14 +690,14 @@ static int cmd_tail(int argc, char **argv)
while ((read(fd, &c, sizeof(char))) > 0)
{
if (current_line >= target_line)
{
rt_kprintf("%c", c);
}
if (c == '\n')
{
current_line++;
}
if (current_line > target_line)
{
rt_kprintf("%c", c);
}
}
rt_kprintf("\n");