summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/sntp/tests/fileHandlingTest.c
blob: 8acad5498f8b5ecefcc8b8dee93245673439793c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include "config.h"
#include "stdlib.h"
#include "sntptest.h"

#include "fileHandlingTest.h" /* required because of the h.in thingy */

#include <string.h>
#include <unistd.h>

const char *
CreatePath(
	const char *		filename,
	enum DirectoryType 	argument
	)
{
	const char 	srcdir[] = SRCDIR_DEF;//"@abs_srcdir@/data/";
	size_t		plen = sizeof(srcdir) + strlen(filename) + 1;
	char * 		path = emalloc(plen);
	ssize_t		retc;

	UNUSED_ARG(argument);

	retc = snprintf(path, plen, "%s%s", srcdir, filename);
	if (retc <= 0 || (size_t)retc >= plen)
		exit(1);
	return path;
}


void
DestroyPath(
	const char *	pathname
	)
{
	/* use a union to get terminally rid of the 'const' attribute */
	union {
		const char *ccp;
		void       *vp;
	} any;

	any.ccp = pathname;
	free(any.vp);
}


int
GetFileSize(
	FILE *	file
	)
{
	fseek(file, 0L, SEEK_END);
	int length = ftell(file);
	fseek(file, 0L, SEEK_SET);

	return length;
}


bool
CompareFileContent(
	FILE *	expected,
	FILE *	actual
	)
{
	int currentLine = 1;

	char actualLine[1024];
	char expectedLine[1024];
	size_t lenAct = sizeof actualLine;
	size_t lenExp = sizeof expectedLine;
	
	while (  ( (fgets(actualLine, lenAct, actual)) != NULL)
	      && ( (fgets(expectedLine, lenExp, expected)) != NULL )
	      ) {

	
		if( strcmp(actualLine,expectedLine) !=0 ){
			printf("Comparision failed on line %d",currentLine);
			return FALSE;
		}

		currentLine++;
	}

	return TRUE;
}


void
ClearFile(
	const char * filename
	)
{
	if (!truncate(filename, 0))
		exit(1);
}
OpenPOWER on IntegriCloud