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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "helper_methods.h"
int compare_files(char expected_output_filename[],char actual_output_filename[] )
{
FILE *expected_output_fh;
FILE *actual_output_fh;
char *expected_buffer;
char *actual_buffer;
long numbytes;
expected_output_fh = fopen(expected_output_filename, "r");
actual_output_fh = fopen(actual_output_filename, "r");
fseek(expected_output_fh, 0L, SEEK_END);
numbytes = ftell(expected_output_fh);
fseek(expected_output_fh, 0L, SEEK_SET);
expected_buffer = (char*)calloc(numbytes +1, sizeof(char));
fread(expected_buffer, sizeof(char), numbytes, expected_output_fh);
fclose(expected_output_fh);
fseek(actual_output_fh, 0L, SEEK_END);
numbytes = ftell(actual_output_fh);
fseek(actual_output_fh, 0L, SEEK_SET);
actual_buffer = (char*)calloc(numbytes +1, sizeof(char));
fread(actual_buffer, sizeof(char), numbytes, actual_output_fh);
fclose(actual_output_fh);
if(strcmp(expected_buffer,actual_buffer) == 0)
{
free(expected_buffer);
free(actual_buffer);
return 1;
}
free(expected_buffer);
free(actual_buffer);
return 0;
}
int number_of_recombinations_in_file(char * fileName)
{
FILE *file = fopen(fileName, "r");
int ch, prev = '\n', lines = 0;
while ( (ch = fgetc(file)) != EOF )
{
if ( ch == '\n' )
{
++lines;
}
prev = ch;
}
fclose(file);
if ( prev != '\n' )
{
++lines;
}
return (int) lines/6;
}
int file_exists(char * fileName)
{
struct stat buf;
int i = stat ( fileName, &buf );
if ( i == 0 )
{
return 1;
}
return 0;
}
int cp(const char *to, const char *from)
{
int fd_to, fd_from;
char buf[4096];
ssize_t nread;
int saved_errno;
fd_from = open(from, O_RDONLY);
if (fd_from < 0)
return -1;
fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd_to < 0)
goto out_error;
while (nread = read(fd_from, buf, sizeof buf), nread > 0)
{
char *out_ptr = buf;
ssize_t nwritten;
do {
nwritten = write(fd_to, out_ptr, nread);
if (nwritten >= 0)
{
nread -= nwritten;
out_ptr += nwritten;
}
else if (errno != EINTR)
{
goto out_error;
}
} while (nread > 0);
}
if (nread == 0)
{
if (close(fd_to) < 0)
{
fd_to = -1;
goto out_error;
}
close(fd_from);
/* Success! */
return 0;
}
out_error:
saved_errno = errno;
close(fd_from);
if (fd_to >= 0)
close(fd_to);
errno = saved_errno;
return -1;
}
|