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