fix cmd: tail
@@ -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,13 +690,13 @@ static int cmd_tail(int argc, char **argv)
while ((read(fd, &c, sizeof(char))) > 0)
{
- if (c == '\n')
+ if (current_line >= target_line)
{
- current_line++;
+ rt_kprintf("%c", c);
}
- if (current_line > target_line)
+ if (c == '\n')
{
- rt_kprintf("%c", c);
+ current_line++;
}
}
rt_kprintf("\n");
