timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ttail_argparse_files.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <check.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "ttail_check.h"
  6. #include "ttail.h"
  7. #include "ttail_init.h"
  8. ttail_t *ttail;
  9. #define __Fname_sz 5
  10. #define FNAME_NO_EXIST 0
  11. #define FNAME_EXIST 1
  12. char *fname[__Fname_sz];
  13. /*
  14. * file argument parsing
  15. */
  16. void setup_fname(void)
  17. {
  18. int i;
  19. FILE *fp;
  20. for(i=0;i<__Fname_sz;i++)
  21. {
  22. fname[i] = NULL;
  23. }
  24. for(i=0; i<__Fname_sz; i++)
  25. {
  26. fname[i] = tempnam(NULL, "ttail_check");
  27. if(i%2)
  28. {
  29. if((fp=fopen(fname[i], "w+")) == NULL)
  30. {
  31. perror("Unable to create file for testing");
  32. ck_abort_msg("Unable to create file for testing");
  33. }
  34. if(fclose(fp))
  35. {
  36. perror("Unable to close file for testing");
  37. ck_abort_msg("Unable to close file for testing");
  38. }
  39. }
  40. }
  41. }
  42. void teardown_fname(void)
  43. {
  44. int i;
  45. for(i=0; i<__Fname_sz; i++)
  46. {
  47. unlink(fname[i]);
  48. if(fname[i] != NULL)
  49. {
  50. free(fname[i]);
  51. }
  52. }
  53. }
  54. START_TEST (test_argparse_file_non_exist)
  55. {
  56. ttail_t *t;
  57. char *args[4] = {"foo", "-d", "Mar 6 13:37", NULL};
  58. args[3] = fname[FNAME_NO_EXIST];
  59. t = ttail_init(4, args);
  60. ck_assert_msg(t != NULL, "init failed");
  61. ck_assert_msg(t->logfile_sz == 1,
  62. "ttail_t.logfile_sz should be 1");
  63. ck_assert_msg(strcmp(t->logfile_name[0], args[3]) == 0,
  64. "ttail_t.logfile_name[0] does not contain the filename");
  65. ck_assert_msg(t->logfile[0] == NULL,
  66. "ttail_t.logfile[0] should be NULL");
  67. ttail_free(t);
  68. }
  69. END_TEST
  70. START_TEST (test_argparse_file_exist)
  71. {
  72. ttail_t *t;
  73. char *args[4] = {"foo", "-d", "Mar 10 13:37", NULL};
  74. args[3] = fname[FNAME_EXIST];
  75. t = ttail_init(4, args);
  76. ck_assert_msg(t != NULL, "init failed");
  77. ck_assert_msg(t->logfile_sz == 1,
  78. "ttail_t.logfile_sz should be 1");
  79. ck_assert_msg(strcmp(t->logfile_name[0], args[3]) == 0,
  80. "ttail_t.logfile_name[0] does not contain the filename");
  81. ck_assert_msg(t->logfile[0] != NULL,
  82. "ttail_t.logfile[0] shouldn't be NULL");
  83. ttail_free(t);
  84. }
  85. END_TEST
  86. START_TEST (test_argparse_file_multiple)
  87. {
  88. ttail_t *t;
  89. int i;
  90. char *args[8] = {"foo", "-d", "mar 10 13:37", NULL, NULL, NULL, NULL,
  91. NULL};
  92. for(i=0; i<__Fname_sz; i++)
  93. {
  94. args[i+3] = fname[i];
  95. }
  96. t = ttail_init(8, args);
  97. ck_assert_msg(t != NULL, "init failed");
  98. ck_assert_msg(t->logfile_sz == __Fname_sz,
  99. "ttail_t.logfile_sz doesn't have the good value");
  100. for(i=0;i<__Fname_sz; i++)
  101. {
  102. ck_assert_msg(strcmp(t->logfile_name[i], args[i+3]) == 0,
  103. "some filename corrupted in ttail_t.logfile_name");
  104. if(i%2)
  105. {
  106. ck_assert_msg(t->logfile[i] != NULL,
  107. "logfile not opened");
  108. } else {
  109. ck_assert_msg(t->logfile[i] == NULL,
  110. "logfile should be NULL");
  111. }
  112. }
  113. }
  114. END_TEST
  115. Suite * ttail_init_suite(void)
  116. {
  117. Suite *s;
  118. TCase *tc_argparse_files;
  119. s = suite_create("ttail argument parsing checks");
  120. tc_argparse_files = tcase_create("files options parse");
  121. tcase_add_checked_fixture(tc_argparse_files,
  122. setup_fname, teardown_fname);
  123. tcase_add_test(tc_argparse_files, test_argparse_file_non_exist);
  124. tcase_add_test(tc_argparse_files, test_argparse_file_exist);
  125. tcase_add_test(tc_argparse_files, test_argparse_file_multiple);
  126. suite_add_tcase(s, tc_argparse_files);
  127. return s;
  128. }
  129. TTAIL_CHECK_MAIN(ttail_init_suite)