|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Module : mem_file.c
|
|
kusano |
7d535a |
- Description : A general purpose library for manipulating a memory area
|
|
kusano |
7d535a |
- as if it were a file.
|
|
kusano |
7d535a |
- mfs_ stands for memory file system.
|
|
kusano |
7d535a |
- Author : Mike Johnson - Banctec AB 03/07/96
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Copyright (c) 1996 Mike Johnson
|
|
kusano |
7d535a |
Copyright (c) 1996 BancTec AB
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Permission to use, copy, modify, distribute, and sell this software
|
|
kusano |
7d535a |
for any purpose is hereby granted without fee, provided
|
|
kusano |
7d535a |
that (i) the above copyright notices and this permission notice appear in
|
|
kusano |
7d535a |
all copies of the software and related documentation, and (ii) the names of
|
|
kusano |
7d535a |
Mike Johnson and BancTec may not be used in any advertising or
|
|
kusano |
7d535a |
publicity relating to the software.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
kusano |
7d535a |
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
kusano |
7d535a |
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
IN NO EVENT SHALL MIKE JOHNSON OR BANCTEC BE LIABLE FOR
|
|
kusano |
7d535a |
ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
kusano |
7d535a |
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
kusano |
7d535a |
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
kusano |
7d535a |
LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
kusano |
7d535a |
OF THIS SOFTWARE.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Includes
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include <stdio.h></stdio.h>
|
|
kusano |
7d535a |
#include <stdlib.h></stdlib.h>
|
|
kusano |
7d535a |
#include <string.h></string.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Definitions
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#define MAX_BUFFS 20
|
|
kusano |
7d535a |
#define FALSE 0
|
|
kusano |
7d535a |
#define TRUE 1
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Globals
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static char *buf[MAX_BUFFS]; /* Memory for each open buf */
|
|
kusano |
7d535a |
static long buf_off[MAX_BUFFS]; /* File pointer for each buf */
|
|
kusano |
7d535a |
static long buf_size[MAX_BUFFS]; /* Count of bytes allocated for each buf */
|
|
kusano |
7d535a |
static long fds[MAX_BUFFS]; /* File descriptor status */
|
|
kusano |
7d535a |
static int buf_mode[MAX_BUFFS]; /* Mode of buffer (r, w, a) */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int library_init_done = FALSE;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function prototypes
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int mfs_open (void *ptr, int size, char *mode);
|
|
kusano |
7d535a |
int mfs_lseek (int fd, int offset, int whence);
|
|
kusano |
7d535a |
int mfs_read (int fd, void *buf, int size);
|
|
kusano |
7d535a |
int mfs_write (int fd, void *buf, int size);
|
|
kusano |
7d535a |
int mfs_size (int fd);
|
|
kusano |
7d535a |
int mfs_map (int fd, char **addr, size_t *len);
|
|
kusano |
7d535a |
int mfs_unmap (int fd);
|
|
kusano |
7d535a |
int mfs_close (int fd);
|
|
kusano |
7d535a |
static int extend_mem_file (int fd, int size);
|
|
kusano |
7d535a |
static void mem_init ();
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function code
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : mfs_open ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : Pointer to allocated buffer, initial size of buffer,
|
|
kusano |
7d535a |
- mode spec (r, w, a)
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : File descriptor or -1 if error.
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : Register this area of memory (which has been allocated
|
|
kusano |
7d535a |
- and has a file read into it) under the mem_file library.
|
|
kusano |
7d535a |
- A file descriptor is returned which can the be passed
|
|
kusano |
7d535a |
- back to TIFFClientOpen and used as if it was a disk
|
|
kusano |
7d535a |
- based fd.
|
|
kusano |
7d535a |
- If the call is for mode 'w' then pass (void *)NULL as
|
|
kusano |
7d535a |
- the buffer and zero size and the library will
|
|
kusano |
7d535a |
- allocate memory for you.
|
|
kusano |
7d535a |
- If the mode is append then pass (void *)NULL and size
|
|
kusano |
7d535a |
- zero or with a valid address.
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int mfs_open (void *buffer, int size, char *mode)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int ret, i;
|
|
kusano |
7d535a |
void *tmp;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (library_init_done == FALSE)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
mem_init ();
|
|
kusano |
7d535a |
library_init_done = TRUE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Find a free fd */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (i = 0; i < MAX_BUFFS; i++)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
if (fds[i] == -1)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = i;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (i == MAX_BUFFS) /* No more free descriptors */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EMFILE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (ret >= 0 && *mode == 'r')
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
if (buffer == (void *)NULL)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EINVAL;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
buf[ret] = (char *)buffer;
|
|
kusano |
7d535a |
buf_size[ret] = size;
|
|
kusano |
7d535a |
buf_off[ret] = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else if (ret >= 0 && *mode == 'w')
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (buffer != (void *)NULL)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EINVAL;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
tmp = malloc (0); /* Get a pointer */
|
|
kusano |
7d535a |
if (tmp == (void *)NULL)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EDQUOT;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
buf[ret] = (char *)tmp;
|
|
kusano |
7d535a |
buf_size[ret] = 0;
|
|
kusano |
7d535a |
buf_off[ret] = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else if (ret >= 0 && *mode == 'a')
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
if (buffer == (void *) NULL) /* Create space for client */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
tmp = malloc (0); /* Get a pointer */
|
|
kusano |
7d535a |
if (tmp == (void *)NULL)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EDQUOT;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
buf[ret] = (char *)tmp;
|
|
kusano |
7d535a |
buf_size[ret] = 0;
|
|
kusano |
7d535a |
buf_off[ret] = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else /* Client has file read in already */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
buf[ret] = (char *)buffer;
|
|
kusano |
7d535a |
buf_size[ret] = size;
|
|
kusano |
7d535a |
buf_off[ret] = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else /* Some other invalid combination of parameters */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EINVAL;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (ret != -1)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
fds[ret] = 0;
|
|
kusano |
7d535a |
buf_mode[ret] = *mode;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return (ret);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : mfs_lseek ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : File descriptor, offset, whence
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : as per man lseek (2)
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : Does the same as lseek (2) except on a memory based file.
|
|
kusano |
7d535a |
- Note: the memory area will be extended if the caller
|
|
kusano |
7d535a |
- attempts to seek past the current end of file (memory).
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int mfs_lseek (int fd, int offset, int whence)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int ret;
|
|
kusano |
7d535a |
long test_off;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (fds[fd] == -1) /* Not open */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EBADF;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else if (offset < 0 && whence == SEEK_SET)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EINVAL;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
switch (whence)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
case SEEK_SET:
|
|
kusano |
7d535a |
if (offset > buf_size[fd])
|
|
kusano |
7d535a |
extend_mem_file (fd, offset);
|
|
kusano |
7d535a |
buf_off[fd] = offset;
|
|
kusano |
7d535a |
ret = offset;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case SEEK_CUR:
|
|
kusano |
7d535a |
test_off = buf_off[fd] + offset;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (test_off < 0)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EINVAL;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
if (test_off > buf_size[fd])
|
|
kusano |
7d535a |
extend_mem_file (fd, test_off);
|
|
kusano |
7d535a |
buf_off[fd] = test_off;
|
|
kusano |
7d535a |
ret = test_off;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case SEEK_END:
|
|
kusano |
7d535a |
test_off = buf_size[fd] + offset;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (test_off < 0)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EINVAL;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
if (test_off > buf_size[fd])
|
|
kusano |
7d535a |
extend_mem_file (fd, test_off);
|
|
kusano |
7d535a |
buf_off[fd] = test_off;
|
|
kusano |
7d535a |
ret = test_off;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
default:
|
|
kusano |
7d535a |
errno = EINVAL;
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return (ret);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : mfs_read ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : File descriptor, buffer, size
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : as per man read (2)
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : Does the same as read (2) except on a memory based file.
|
|
kusano |
7d535a |
- Note: An attempt to read past the end of memory currently
|
|
kusano |
7d535a |
- allocated to the file will return 0 (End Of File)
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int mfs_read (int fd, void *clnt_buf, int size)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int ret;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (fds[fd] == -1 || buf_mode[fd] != 'r')
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
/* File is either not open, or not opened for read */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EBADF;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else if (buf_off[fd] + size > buf_size[fd])
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = 0; /* EOF */
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
memcpy (clnt_buf, (void *) (buf[fd] + buf_off[fd]), size);
|
|
kusano |
7d535a |
buf_off[fd] = buf_off[fd] + size;
|
|
kusano |
7d535a |
ret = size;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return (ret);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : mfs_write ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : File descriptor, buffer, size
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : as per man write (2)
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : Does the same as write (2) except on a memory based file.
|
|
kusano |
7d535a |
- Note: the memory area will be extended if the caller
|
|
kusano |
7d535a |
- attempts to write past the current end of file (memory).
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int mfs_write (int fd, void *clnt_buf, int size)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int ret;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (fds[fd] == -1 || buf_mode[fd] == 'r')
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
/* Either the file is not open or it is opened for reading only */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EBADF;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else if (buf_mode[fd] == 'w')
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
/* Write */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (buf_off[fd] + size > buf_size[fd])
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
extend_mem_file (fd, buf_off[fd] + size);
|
|
kusano |
7d535a |
buf_size[fd] = (buf_off[fd] + size);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
memcpy ((buf[fd] + buf_off[fd]), clnt_buf, size);
|
|
kusano |
7d535a |
buf_off[fd] = buf_off[fd] + size;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
ret = size;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
/* Append */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (buf_off[fd] != buf_size[fd])
|
|
kusano |
7d535a |
buf_off[fd] = buf_size[fd];
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
extend_mem_file (fd, buf_off[fd] + size);
|
|
kusano |
7d535a |
buf_size[fd] += size;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
memcpy ((buf[fd] + buf_off[fd]), clnt_buf, size);
|
|
kusano |
7d535a |
buf_off[fd] = buf_off[fd] + size;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
ret = size;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return (ret);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : mfs_size ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : File descriptor
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : integer file size
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : This function returns the current size of the file in bytes.
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int mfs_size (int fd)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int ret;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (fds[fd] == -1) /* Not open */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EBADF;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
ret = buf_size[fd];
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return (ret);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : mfs_map ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : File descriptor, ptr to address, ptr to length
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : Map status (succeeded or otherwise)
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : This function tells the client where the file is mapped
|
|
kusano |
7d535a |
- in memory and what size the mapped area is. It is provided
|
|
kusano |
7d535a |
- to satisfy the MapProc function in libtiff. It pretends
|
|
kusano |
7d535a |
- that the file has been mmap (2)ped.
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int mfs_map (int fd, char **addr, size_t *len)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int ret;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (fds[fd] == -1) /* Not open */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EBADF;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
*addr = buf[fd];
|
|
kusano |
7d535a |
*len = buf_size[fd];
|
|
kusano |
7d535a |
ret = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return (ret);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : mfs_unmap ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : File descriptor
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : UnMap status (succeeded or otherwise)
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : This function does nothing as the file is always
|
|
kusano |
7d535a |
- in memory.
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int mfs_unmap (int fd)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
return (0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : mfs_close ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : File descriptor
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : close status (succeeded or otherwise)
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : Close the open memory file. (Make fd available again.)
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int mfs_close (int fd)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int ret;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (fds[fd] == -1) /* Not open */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
errno = EBADF;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
fds[fd] = -1;
|
|
kusano |
7d535a |
ret = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return (ret);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : extend_mem_file ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : File descriptor, length to extend to.
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : 0 - All OK, -1 - realloc () failed.
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : Increase the amount of memory allocated to a file.
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int extend_mem_file (int fd, int size)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
void *new_mem;
|
|
kusano |
7d535a |
int ret;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if ((new_mem = realloc (buf[fd], size)) == (void *) NULL)
|
|
kusano |
7d535a |
ret = -1;
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
buf[fd] = (char *) new_mem;
|
|
kusano |
7d535a |
ret = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return (ret);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
- Function : mem_init ()
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Arguments : None
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Returns : void
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
- Description : Initialise the library.
|
|
kusano |
7d535a |
-
|
|
kusano |
7d535a |
--------------------------------------------------------------------------------
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void mem_init ()
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int i;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (i = 0; i < MAX_BUFFS; i++)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
fds[i] = -1;
|
|
kusano |
7d535a |
buf[i] = (char *)NULL;
|
|
kusano |
7d535a |
buf_size[i] = 0;
|
|
kusano |
7d535a |
buf_off[i] = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Local Variables:
|
|
kusano |
7d535a |
* mode: c
|
|
kusano |
7d535a |
* c-basic-offset: 8
|
|
kusano |
7d535a |
* fill-column: 78
|
|
kusano |
7d535a |
* End:
|
|
kusano |
7d535a |
*/
|