19034852cSGleb Smirnoff
29034852cSGleb Smirnoff #include "config.h"
39034852cSGleb Smirnoff #include "stdlib.h"
49034852cSGleb Smirnoff #include "sntptest.h"
59034852cSGleb Smirnoff
6*68ba7e87SXin LI #include "fileHandlingTest.h" /* required because of the h.in thingy */
79034852cSGleb Smirnoff
89034852cSGleb Smirnoff #include <string.h>
99034852cSGleb Smirnoff #include <unistd.h>
109034852cSGleb Smirnoff
119034852cSGleb Smirnoff const char *
CreatePath(const char * filename,enum DirectoryType argument)12*68ba7e87SXin LI CreatePath(
13*68ba7e87SXin LI const char * filename,
14*68ba7e87SXin LI enum DirectoryType argument
15*68ba7e87SXin LI )
16*68ba7e87SXin LI {
179034852cSGleb Smirnoff const char srcdir[] = SRCDIR_DEF;//"@abs_srcdir@/data/";
18*68ba7e87SXin LI size_t plen = sizeof(srcdir) + strlen(filename) + 1;
19*68ba7e87SXin LI char * path = emalloc(plen);
20*68ba7e87SXin LI ssize_t retc;
219034852cSGleb Smirnoff
22*68ba7e87SXin LI UNUSED_ARG(argument);
239034852cSGleb Smirnoff
24*68ba7e87SXin LI retc = snprintf(path, plen, "%s%s", srcdir, filename);
25*68ba7e87SXin LI if (retc <= 0 || (size_t)retc >= plen)
26*68ba7e87SXin LI exit(1);
279034852cSGleb Smirnoff return path;
289034852cSGleb Smirnoff }
299034852cSGleb Smirnoff
309034852cSGleb Smirnoff
31*68ba7e87SXin LI void
DestroyPath(const char * pathname)32*68ba7e87SXin LI DestroyPath(
33*68ba7e87SXin LI const char * pathname
34*68ba7e87SXin LI )
35*68ba7e87SXin LI {
36*68ba7e87SXin LI /* use a union to get terminally rid of the 'const' attribute */
37*68ba7e87SXin LI union {
38*68ba7e87SXin LI const char *ccp;
39*68ba7e87SXin LI void *vp;
40*68ba7e87SXin LI } any;
41*68ba7e87SXin LI
42*68ba7e87SXin LI any.ccp = pathname;
43*68ba7e87SXin LI free(any.vp);
44*68ba7e87SXin LI }
45*68ba7e87SXin LI
46*68ba7e87SXin LI
479034852cSGleb Smirnoff int
GetFileSize(FILE * file)48*68ba7e87SXin LI GetFileSize(
49*68ba7e87SXin LI FILE * file
50*68ba7e87SXin LI )
51*68ba7e87SXin LI {
529034852cSGleb Smirnoff fseek(file, 0L, SEEK_END);
539034852cSGleb Smirnoff int length = ftell(file);
549034852cSGleb Smirnoff fseek(file, 0L, SEEK_SET);
559034852cSGleb Smirnoff
569034852cSGleb Smirnoff return length;
579034852cSGleb Smirnoff }
589034852cSGleb Smirnoff
599034852cSGleb Smirnoff
609034852cSGleb Smirnoff bool
CompareFileContent(FILE * expected,FILE * actual)61*68ba7e87SXin LI CompareFileContent(
62*68ba7e87SXin LI FILE * expected,
63*68ba7e87SXin LI FILE * actual
64*68ba7e87SXin LI )
65*68ba7e87SXin LI {
669034852cSGleb Smirnoff int currentLine = 1;
679034852cSGleb Smirnoff
689034852cSGleb Smirnoff char actualLine[1024];
699034852cSGleb Smirnoff char expectedLine[1024];
709034852cSGleb Smirnoff size_t lenAct = sizeof actualLine;
719034852cSGleb Smirnoff size_t lenExp = sizeof expectedLine;
729034852cSGleb Smirnoff
739034852cSGleb Smirnoff while ( ( (fgets(actualLine, lenAct, actual)) != NULL)
749034852cSGleb Smirnoff && ( (fgets(expectedLine, lenExp, expected)) != NULL )
759034852cSGleb Smirnoff ) {
769034852cSGleb Smirnoff
779034852cSGleb Smirnoff
789034852cSGleb Smirnoff if( strcmp(actualLine,expectedLine) !=0 ){
799034852cSGleb Smirnoff printf("Comparision failed on line %d",currentLine);
809034852cSGleb Smirnoff return FALSE;
819034852cSGleb Smirnoff }
829034852cSGleb Smirnoff
839034852cSGleb Smirnoff currentLine++;
849034852cSGleb Smirnoff }
859034852cSGleb Smirnoff
869034852cSGleb Smirnoff return TRUE;
879034852cSGleb Smirnoff }
889034852cSGleb Smirnoff
899034852cSGleb Smirnoff
909034852cSGleb Smirnoff void
ClearFile(const char * filename)91*68ba7e87SXin LI ClearFile(
92*68ba7e87SXin LI const char * filename
93*68ba7e87SXin LI )
94*68ba7e87SXin LI {
959034852cSGleb Smirnoff if (!truncate(filename, 0))
969034852cSGleb Smirnoff exit(1);
979034852cSGleb Smirnoff }
98