2 Commits

Author SHA1 Message Date
  Yann Weber a05a5dda01 Bugfix in _ttail_search_closest_stdin() 6 years ago
  Yann Weber bcecc4d5f1 Add test for ttail_search_std_init() 6 years ago
3 changed files with 100 additions and 4 deletions
  1. 8
    2
      src/include/ttail_search_std.h
  2. 31
    2
      src/ttail_search_std.c
  3. 61
    0
      tests/ttail_check.h

+ 8
- 2
src/include/ttail_search_std.h View File

@@ -22,6 +22,7 @@
22 22
 #include <errno.h>
23 23
 #include <string.h>
24 24
 #include <stdlib.h>
25
+#include <stdio.h>
25 26
 
26 27
 typedef struct _ttail_search_stdin_s ttail_search_stdin_t;
27 28
 /*<! Private search session for stdin */
@@ -45,14 +46,13 @@ struct _ttail_search_stdin_s
45 46
  *@return @ref getline()
46 47
  */
47 48
 #define ttail_std_getline(TTAIL) (getline(\
48
-	&(TTAIL->session->std.buff), &(TTAIL->session->std.buff_sz), stdin))
49
+	&(TTAIL->session->std.buff), &(TTAIL->session->std.buff_sz), TTAIL_STDIN))
49 50
 /*<!Accessor to getline wrapper buffer */
50 51
 #define ttail_std_getline_buff(TTAIL) (TTAIL->session->std.buff)
51 52
 
52 53
 /**@brief init private session members
53 54
  *@patam ttail_t* t
54 55
  *@return -1 on error else 0
55
- *@todo checks
56 56
  */
57 57
 int ttail_search_std_init(ttail_t* t);
58 58
 
@@ -88,5 +88,11 @@ void _ttail_search_print_stdin(ttail_t*, int);
88 88
  */
89 89
 void _ttail_search_stdin_free(ttail_t* t);
90 90
 
91
+/**@brief Set the stdin file descriptor
92
+ *
93
+ *Usefull for checks
94
+ */
95
+void _ttail_set_stdin(int);
96
+
91 97
 #endif
92 98
 

+ 31
- 2
src/ttail_search_std.c View File

@@ -18,8 +18,25 @@
18 18
  */
19 19
 #include "ttail_search_std.h"
20 20
 
21
+int TTAIL_STDIN_FD = 0;
22
+FILE *TTAIL_STDIN = NULL;
23
+
21 24
 int ttail_search_std_init(ttail_t* t)
22 25
 {
26
+	if(!TTAIL_STDIN_FD)
27
+	{
28
+		TTAIL_STDIN = stdin;
29
+	}
30
+	else
31
+	{
32
+		TTAIL_STDIN = fdopen(TTAIL_STDIN_FD, "r");
33
+		if(!TTAIL_STDIN)
34
+		{
35
+			perror("Fail to open stdin");
36
+			return -1;
37
+		}
38
+	}
39
+
23 40
 	t->session = (ttail_search_t*)malloc(sizeof(ttail_search_stdin_t));
24 41
 	if(!t->session)
25 42
 	{
@@ -151,7 +168,9 @@ int _ttail_search_closest_stdin(ttail_t* t)
151 168
 					fprintf(stderr, "\n");
152 169
 				}
153 170
 				ttail_strict_msg();
171
+				return -1;
154 172
 			}
173
+			rd_sz = ttail_std_getline(t);
155 174
 			continue;
156 175
 		}
157 176
 		ret = ttail_tm_cmp(&tm, &(t->date_min));
@@ -165,7 +184,7 @@ int _ttail_search_closest_stdin(ttail_t* t)
165 184
 	if(rd_sz < 0)
166 185
 	{
167 186
 		tmp = errno;
168
-		ret = feof(stdin);
187
+		ret = feof(TTAIL_STDIN);
169 188
 		if(ret <= 0)
170 189
 		{
171 190
 			errno = tmp;
@@ -226,7 +245,7 @@ void _ttail_search_print_stdin(ttail_t* t, int fd)
226 245
 	if(rd_sz < 0)
227 246
 	{
228 247
 		tmp = errno;
229
-		ret = feof(stdin);
248
+		ret = feof(TTAIL_STDIN);
230 249
 		if(ret <= 0)
231 250
 		{
232 251
 			errno = tmp;
@@ -238,9 +257,19 @@ void _ttail_search_print_stdin(ttail_t* t, int fd)
238 257
 
239 258
 void _ttail_search_stdin_free(ttail_t* t)
240 259
 {
260
+	if(TTAIL_STDIN)
261
+	{
262
+		fclose(TTAIL_STDIN);
263
+	}
241 264
 	if(t->session->std.buff)
242 265
 	{
243 266
 		free(t->session->std.buff);
244 267
 	}
245 268
 }
246 269
 
270
+void _ttail_set_stdin(int std)
271
+{
272
+	TTAIL_STDIN_FD = std;
273
+	TTAIL_STDIN = NULL;
274
+}
275
+

+ 61
- 0
tests/ttail_check.h View File

@@ -6,6 +6,9 @@
6 6
 #include <stdio.h>
7 7
 #include <unistd.h>
8 8
 #include <libgen.h>
9
+#include <fcntl.h>
10
+#include <sys/types.h>
11
+#include <sys/stat.h>
9 12
 
10 13
 #include "ttail.h"
11 14
 #include "ttail_init.h"
@@ -66,6 +69,30 @@ int main(int argc, char **argv) {\
66 69
 	ck_assert_msg(test, _err_msg);\
67 70
 }
68 71
 
72
+#define close_stdpipe() {\
73
+	close(STDPIPE);\
74
+	STDPIPE = -1;\
75
+}
76
+
77
+#define send_sample_stdpipe(id) {\
78
+	int ret;\
79
+	char buff[1024];\
80
+	while((ret= read(samples_fd[id], buff, 1024)) > 0)\
81
+	{\
82
+		ret = write(STDPIPE, buff, strlen(buff));\
83
+		if(ret < 0)\
84
+		{\
85
+			perror("Unable to write through pipe");\
86
+			ck_abort_msg("Unable to write through pipe");\
87
+		}\
88
+	}\
89
+	if(ret < 0)\
90
+	{\
91
+		perror("Unable to read through pipe");\
92
+		ck_abort_msg("Unable to read through pipe");\
93
+	}\
94
+}
95
+
69 96
 char *fname[__Fname_sz];
70 97
 /*date formats*/
71 98
 char *fmt[] = TTAIL_DEFAULT_FORMATS;
@@ -74,6 +101,10 @@ char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
74 101
 	"./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
75 102
 off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
76 103
 
104
+int samples_fd[6];
105
+
106
+int STDPIPE;
107
+
77 108
 void teardown_ttail(void)
78 109
 {
79 110
 	ttail_free(ttail);
@@ -143,6 +174,36 @@ void setup_closest(void)
143 174
 	ttail = ttail_init(1, (char**)&"foo");
144 175
 }
145 176
 
177
+void setup_closest_stdin(void)
178
+{
179
+	int ret, pipes[2], i;
180
+
181
+	ret = pipe(pipes);
182
+	if(ret < 0)
183
+	{
184
+		perror("fail to open pipe");
185
+		ck_abort_msg("fail to open pipe");
186
+	}
187
+	_ttail_set_stdin(pipes[0]);
188
+	STDPIPE = pipes[1];
189
+	
190
+	for(i=0; i<sizeof(samples_fd)/sizeof(int); i++)
191
+	{
192
+		samples_fd[i] = open(samples[i], 0, O_RDONLY);
193
+	}
194
+
195
+	setup_closest();
196
+}
197
+
198
+void teardown_closest_stdin(void)
199
+{
200
+	if(STDPIPE > 0)
201
+	{
202
+		close(STDPIPE);
203
+	}
204
+	teardown_closest();
205
+}
206
+
146 207
 void setup_closest_fileinit(void)
147 208
 {
148 209
 	size_t i;

Loading…
Cancel
Save