/***************************************
- $Header: /home/amb/routino/src/RCS/files.c,v 1.18 2010/03/29 18:20:06 amb Exp $
+ $Header: /home/amb/routino/src/RCS/files.c,v 1.24 2010/10/16 10:59:18 amb Exp $
Functions to handle files.
along with this program. If not, see <http://www.gnu.org/licenses/>.
***************************************/
+#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
-#include "functions.h"
+#include "files.h"
/*+ A structure to contain the list of memory mapped files. +*/
{
close(fd);
- fprintf(stderr,"Cannot mmap file '%s' [%s].\n",filename,strerror(errno));
+ assert(0);
+
+ fprintf(stderr,"Cannot mmap file '%s' for reading [%s].\n",filename,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ mappedfiles=(struct mmapinfo*)realloc((void*)mappedfiles,(nmappedfiles+1)*sizeof(struct mmapinfo));
+
+ mappedfiles[nmappedfiles].filename=filename;
+ mappedfiles[nmappedfiles].fd=fd;
+ mappedfiles[nmappedfiles].address=address;
+ mappedfiles[nmappedfiles].length=size;
+
+ nmappedfiles++;
+
+ return(address);
+}
+
+
+/*++++++++++++++++++++++++++++++++++++++
+ Open a file and map it into memory.
+
+ void *MapFileWriteable Returns the address of the file or exits in case of an error.
+
+ const char *filename The name of the file to open.
+ ++++++++++++++++++++++++++++++++++++++*/
+
+void *MapFileWriteable(const char *filename)
+{
+ int fd;
+ off_t size;
+ void *address;
+
+ /* Open the file and get its size */
+
+ fd=ReOpenFileWriteable(filename);
+
+ size=SizeFile(filename);
+
+ /* Map the file */
+
+ address=mmap(NULL,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
+
+ if(address==MAP_FAILED)
+ {
+ close(fd);
+
+ fprintf(stderr,"Cannot mmap file '%s' for reading and writing [%s].\n",filename,strerror(errno));
exit(EXIT_FAILURE);
}
/*++++++++++++++++++++++++++++++++++++++
Open a new file on disk for writing to.
- int OpenFile Returns the file descriptor if OK or exits in case of an error.
+ int OpenFileNew Returns the file descriptor if OK or exits in case of an error.
const char *filename The name of the file to create.
++++++++++++++++++++++++++++++++++++++*/
-int OpenFile(const char *filename)
+int OpenFileNew(const char *filename)
{
int fd;
/* Open the file */
- fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+ fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(fd<0)
{
/*++++++++++++++++++++++++++++++++++++++
- Open a new file on disk for reading and appending.
+ Open a new file on disk for reading from and appending.
- int AppendFile Returns the file descriptor if OK or exits in case of an error.
+ int OpenFileAppend Returns the file descriptor if OK or exits in case of an error.
const char *filename The name of the file to create.
++++++++++++++++++++++++++++++++++++++*/
-int AppendFile(const char *filename)
+int OpenFileAppend(const char *filename)
{
int fd;
/*++++++++++++++++++++++++++++++++++++++
- Open an existing file on disk for reading from.
+ Open an existing file on disk for reading.
int ReOpenFile Returns the file descriptor if OK or exits in case of an error.
/*++++++++++++++++++++++++++++++++++++++
- Write data to a file on disk.
-
- int WriteFile Returns 0 if OK or something else in case of an error.
+ Open an existing file on disk for reading from or writing to.
- int fd The file descriptor to write to.
+ int ReOpenFileWriteable Returns the file descriptor if OK or exits in case of an error.
- const void *address The address of the data to be written from.
-
- size_t length The length of data to write.
+ const char *filename The name of the file to open.
++++++++++++++++++++++++++++++++++++++*/
-int WriteFile(int fd,const void *address,size_t length)
+int ReOpenFileWriteable(const char *filename)
{
- /* Write the data */
-
- if(write(fd,address,length)!=length)
- return(-1);
-
- return(0);
-}
-
-
-/*++++++++++++++++++++++++++++++++++++++
- Read data from a file on disk.
-
- int ReadFile Returns 0 if OK or something else in case of an error.
-
- int fd The file descriptor to read from.
-
- void *address The address of the data to be read into.
+ int fd;
- size_t length The length of data to read.
- ++++++++++++++++++++++++++++++++++++++*/
+ /* Open the file */
-int ReadFile(int fd,void *address,size_t length)
-{
- /* Read the data */
+ fd=open(filename,O_RDWR);
- if(read(fd,address,length)!=length)
- return(-1);
+ if(fd<0)
+ {
+ fprintf(stderr,"Cannot open file '%s' for reading and writing [%s].\n",filename,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
- return(0);
+ return(fd);
}
/*++++++++++++++++++++++++++++++++++++++
- Seek to a position in a file on disk.
-
- int SeekFile Returns 0 if OK or something else in case of an error.
-
- int fd The file descriptor to seek within.
-
- off_t position The position to seek to.
- ++++++++++++++++++++++++++++++++++++++*/
-
-int SeekFile(int fd,off_t position)
-{
- /* Seek the data */
-
- if(lseek(fd,position,SEEK_SET)!=position)
- return(-1);
-
- return(0);
-}
-
-
-/*++++++++++++++++++++++++++++++++++++++
Close a file on disk.
int fd The file descriptor to close.