2 Commits

Author SHA1 Message Date
  Yann Weber a05a5dda01 Bugfix in _ttail_search_closest_stdin() 7 years ago
  Yann Weber bcecc4d5f1 Add test for ttail_search_std_init() 7 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
 #include <errno.h>
22
 #include <errno.h>
23
 #include <string.h>
23
 #include <string.h>
24
 #include <stdlib.h>
24
 #include <stdlib.h>
25
+#include <stdio.h>
25
 
26
 
26
 typedef struct _ttail_search_stdin_s ttail_search_stdin_t;
27
 typedef struct _ttail_search_stdin_s ttail_search_stdin_t;
27
 /*<! Private search session for stdin */
28
 /*<! Private search session for stdin */
45
  *@return @ref getline()
46
  *@return @ref getline()
46
  */
47
  */
47
 #define ttail_std_getline(TTAIL) (getline(\
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
 /*<!Accessor to getline wrapper buffer */
50
 /*<!Accessor to getline wrapper buffer */
50
 #define ttail_std_getline_buff(TTAIL) (TTAIL->session->std.buff)
51
 #define ttail_std_getline_buff(TTAIL) (TTAIL->session->std.buff)
51
 
52
 
52
 /**@brief init private session members
53
 /**@brief init private session members
53
  *@patam ttail_t* t
54
  *@patam ttail_t* t
54
  *@return -1 on error else 0
55
  *@return -1 on error else 0
55
- *@todo checks
56
  */
56
  */
57
 int ttail_search_std_init(ttail_t* t);
57
 int ttail_search_std_init(ttail_t* t);
58
 
58
 
88
  */
88
  */
89
 void _ttail_search_stdin_free(ttail_t* t);
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
 #endif
97
 #endif
92
 
98
 

+ 31
- 2
src/ttail_search_std.c View File

18
  */
18
  */
19
 #include "ttail_search_std.h"
19
 #include "ttail_search_std.h"
20
 
20
 
21
+int TTAIL_STDIN_FD = 0;
22
+FILE *TTAIL_STDIN = NULL;
23
+
21
 int ttail_search_std_init(ttail_t* t)
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
 	t->session = (ttail_search_t*)malloc(sizeof(ttail_search_stdin_t));
40
 	t->session = (ttail_search_t*)malloc(sizeof(ttail_search_stdin_t));
24
 	if(!t->session)
41
 	if(!t->session)
25
 	{
42
 	{
151
 					fprintf(stderr, "\n");
168
 					fprintf(stderr, "\n");
152
 				}
169
 				}
153
 				ttail_strict_msg();
170
 				ttail_strict_msg();
171
+				return -1;
154
 			}
172
 			}
173
+			rd_sz = ttail_std_getline(t);
155
 			continue;
174
 			continue;
156
 		}
175
 		}
157
 		ret = ttail_tm_cmp(&tm, &(t->date_min));
176
 		ret = ttail_tm_cmp(&tm, &(t->date_min));
165
 	if(rd_sz < 0)
184
 	if(rd_sz < 0)
166
 	{
185
 	{
167
 		tmp = errno;
186
 		tmp = errno;
168
-		ret = feof(stdin);
187
+		ret = feof(TTAIL_STDIN);
169
 		if(ret <= 0)
188
 		if(ret <= 0)
170
 		{
189
 		{
171
 			errno = tmp;
190
 			errno = tmp;
226
 	if(rd_sz < 0)
245
 	if(rd_sz < 0)
227
 	{
246
 	{
228
 		tmp = errno;
247
 		tmp = errno;
229
-		ret = feof(stdin);
248
+		ret = feof(TTAIL_STDIN);
230
 		if(ret <= 0)
249
 		if(ret <= 0)
231
 		{
250
 		{
232
 			errno = tmp;
251
 			errno = tmp;
238
 
257
 
239
 void _ttail_search_stdin_free(ttail_t* t)
258
 void _ttail_search_stdin_free(ttail_t* t)
240
 {
259
 {
260
+	if(TTAIL_STDIN)
261
+	{
262
+		fclose(TTAIL_STDIN);
263
+	}
241
 	if(t->session->std.buff)
264
 	if(t->session->std.buff)
242
 	{
265
 	{
243
 		free(t->session->std.buff);
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
 #include <stdio.h>
6
 #include <stdio.h>
7
 #include <unistd.h>
7
 #include <unistd.h>
8
 #include <libgen.h>
8
 #include <libgen.h>
9
+#include <fcntl.h>
10
+#include <sys/types.h>
11
+#include <sys/stat.h>
9
 
12
 
10
 #include "ttail.h"
13
 #include "ttail.h"
11
 #include "ttail_init.h"
14
 #include "ttail_init.h"
66
 	ck_assert_msg(test, _err_msg);\
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
 char *fname[__Fname_sz];
96
 char *fname[__Fname_sz];
70
 /*date formats*/
97
 /*date formats*/
71
 char *fmt[] = TTAIL_DEFAULT_FORMATS;
98
 char *fmt[] = TTAIL_DEFAULT_FORMATS;
74
 	"./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
101
 	"./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
75
 off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
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
 void teardown_ttail(void)
108
 void teardown_ttail(void)
78
 {
109
 {
79
 	ttail_free(ttail);
110
 	ttail_free(ttail);
143
 	ttail = ttail_init(1, (char**)&"foo");
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
 void setup_closest_fileinit(void)
207
 void setup_closest_fileinit(void)
147
 {
208
 {
148
 	size_t i;
209
 	size_t i;

Loading…
Cancel
Save