epg/file.c

95 lines
1.4 KiB
C

/*
* Humax EPG Tool
* by af123, 2011
*/
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <strings.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "lint.h"
struct epg *
open_file(char *filename)
{
struct epg *epg;
struct stat st, st2;
if (debug)
printf("Opening file '%s'\n", filename);
if (stat(filename, &st) == -1)
{
perror(filename);
return NULL;
}
sleep(1);
if (stat(filename, &st2) == -1)
{
perror(filename);
return NULL;
}
if (st.st_size != st2.st_size)
{
printf("EPG data file is updating, try later.\n");
return NULL;
}
if (!(epg = malloc(sizeof(struct epg))))
{
perror("malloc");
return NULL;
}
strcpy(epg->fname, filename);
epg->binsize = st.st_size;
if (debug)
printf("Opening %s, %lu bytes.\n", epg->fname,
(unsigned long)epg->binsize);
if ((epg->fd = open(epg->fname, O_RDONLY, 0)) == -1)
{
perror(epg->fname);
free(epg);
return NULL;
}
epg->bin = (uint8_t *)mmap(NULL, epg->binsize, PROT_READ,
MAP_PRIVATE, epg->fd, 0);
if (epg->bin == MAP_FAILED)
{
perror("mmap");
close(epg->fd);
free(epg);
return NULL;
}
epg->offset = 0;
return epg;
}
void
close_file(struct epg *epg)
{
if (debug)
printf("Closing file.\n");
munmap((void *)epg->bin, epg->binsize);
if (epg->fd > 0)
close(epg->fd);
epg->fd = -1;
}