MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / cmd_tail

Function cmd_tail

components/finsh/msh_file.c:741–841  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

739MSH_CMD_EXPORT_ALIAS(cmd_echo, echo, echo string to file);
740
741static int cmd_tail(int argc, char **argv)
742{
743 int fd;
744 char c = RT_NULL;
745 char *file_name = RT_NULL;
746 rt_uint32_t total_lines = 0;
747 rt_uint32_t target_line = 0;
748 rt_uint32_t current_line = 0;
749 rt_uint32_t required_lines = 0;
750 rt_uint32_t start_line = 0;
751
752 if (argc < 2)
753 {
754 rt_kprintf("Usage: tail [-n numbers] <filename>\n");
755 return -1;
756 }
757 else if (argc == 2)
758 {
759 required_lines = 10; /* default: 10 lines from tail */
760 file_name = argv[1];
761 }
762 else if (rt_strcmp(argv[1], "-n") == 0)
763 {
764 if (argv[2][0] != '+')
765 {
766 required_lines = atoi(argv[2]);
767 }
768 else
769 {
770 start_line = atoi(&argv[2][1]); /* eg: +100, to get the 100 */
771 }
772 file_name = argv[3];
773 }
774 else
775 {
776 rt_kprintf("Usage: tail [-n numbers] <filename>\n");
777 return -1;
778 }
779
780 fd = open(file_name, O_RDONLY);
781 if (fd < 0)
782 {
783 rt_kprintf("File doesn't exist\n");
784 return -1;
785 }
786
787 while ((read(fd, &c, sizeof(char))) > 0)
788 {
789 if(total_lines == 0)
790 {
791 total_lines++;
792 }
793 if (c == '\n')
794 {
795 total_lines++;
796 }
797 }
798

Callers

nothing calls this directly

Calls 6

rt_kprintfFunction · 0.85
rt_strcmpFunction · 0.85
openFunction · 0.50
readFunction · 0.50
closeFunction · 0.50
lseekFunction · 0.50

Tested by

no test coverage detected