xref: /freebsd/contrib/ntp/sntp/tests/fileHandlingTest.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1 
2 #include "config.h"
3 #include "stdlib.h"
4 #include "sntptest.h"
5 
6 #include "fileHandlingTest.h" /* required because of the h.in thingy */
7 
8 #include <string.h>
9 #include <unistd.h>
10 
11 const char *
12 CreatePath(
13 	const char *		filename,
14 	enum DirectoryType 	argument
15 	)
16 {
17 	const char 	srcdir[] = SRCDIR_DEF;//"@abs_srcdir@/data/";
18 	size_t		plen = sizeof(srcdir) + strlen(filename) + 1;
19 	char * 		path = emalloc(plen);
20 	ssize_t		retc;
21 
22 	UNUSED_ARG(argument);
23 
24 	retc = snprintf(path, plen, "%s%s", srcdir, filename);
25 	if (retc <= 0 || (size_t)retc >= plen)
26 		exit(1);
27 	return path;
28 }
29 
30 
31 void
32 DestroyPath(
33 	const char *	pathname
34 	)
35 {
36 	/* use a union to get terminally rid of the 'const' attribute */
37 	union {
38 		const char *ccp;
39 		void       *vp;
40 	} any;
41 
42 	any.ccp = pathname;
43 	free(any.vp);
44 }
45 
46 
47 int
48 GetFileSize(
49 	FILE *	file
50 	)
51 {
52 	fseek(file, 0L, SEEK_END);
53 	int length = ftell(file);
54 	fseek(file, 0L, SEEK_SET);
55 
56 	return length;
57 }
58 
59 
60 bool
61 CompareFileContent(
62 	FILE *	expected,
63 	FILE *	actual
64 	)
65 {
66 	int currentLine = 1;
67 
68 	char actualLine[1024];
69 	char expectedLine[1024];
70 	size_t lenAct = sizeof actualLine;
71 	size_t lenExp = sizeof expectedLine;
72 
73 	while (  ( (fgets(actualLine, lenAct, actual)) != NULL)
74 	      && ( (fgets(expectedLine, lenExp, expected)) != NULL )
75 	      ) {
76 
77 
78 		if( strcmp(actualLine,expectedLine) !=0 ){
79 			printf("Comparision failed on line %d",currentLine);
80 			return FALSE;
81 		}
82 
83 		currentLine++;
84 	}
85 
86 	return TRUE;
87 }
88 
89 
90 void
91 ClearFile(
92 	const char * filename
93 	)
94 {
95 	if (!truncate(filename, 0))
96 		exit(1);
97 }
98