#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "../global.h"
#include "shared_survey_string.h"
#include "shared_survey_log.h"
#include "shared_lock.h"
#include "shared_cs_util.h"

void
write_to_log( const char *org)
{
    struct stat buf;
    char log_file[MAX_PATH +1];
    char *buffer;
    FILE *fp;

    if(stat(org, &buf))
       cs_critical_error(ERR_STAT_FAILED, "");
    buffer = (char*) malloc((buf.st_size +1)* sizeof(char));
    fp = fopen(org, "r");
    get_shared_lock(fileno(fp), 1);
    if(!fp)
       cs_critical_error(ERR_FOPEN_READ_FAILED, "");
    if(fread(buffer, buf.st_size, 1, fp) != 1)
	 cs_critical_error(ERR_FREAD_FAILED, "");
   release_lock(fileno(fp));
    fclose(fp);
    *(buffer + buf.st_size) = '\0';
    snprintf(log_file, MAX_PATH +1, "%s_log", org);
    fp = fopen(log_file, "w");
    if(!fp)
       cs_critical_error(ERR_FOPEN_WRITE_FAILED, "");
    fprintf(fp, "%s", buffer);
    fclose(fp);
    if(buffer)
	free(buffer);
}

