| typedef struct
{
void* Ptr;
off_t Current_Ofs;
size_t Size;
} _mem_file_t;
int my_fsext(__FSEXT_Fnumber Op, int* RV, va_list Args)
{
const char* Path;
void* Buffer;
size_t Size;
int fd;
_mem_file_t* MPtr;
switch (Op)
{
case __FSEXT_creat:
/* Create a new memory file */
Path = va_list(Args, const char*);
/* Check to see if we should create a new file */
if (strnicmp("/tmp/", Path, 5) != 0) return 0;
/* Allocate some memory to keep info on our fake file */
MPtr = malloc(sizeof(_mem_file_t));
if (!MPtr) return 0;
memset(MPtr, 0, sizeof(_mem_file_t));
/* Get a file descriptor we can use */
fd = __FSEXT_alloc_fd(my_fsext);
if (fd < 0)
{
free(MPtr);
return 0;
}
/* Now store our note about this file descriptor so we can
* look it up quickly later. */
__FSEXT_set_data(fd, MPtr);
/* Return the file descriptor
*RV = fd;
return 1;
case __FSEXT_read:
/* Read from our memory file. */
fd = va_list(Args, int);
Buffer = va_list(Args, void*);
Size = va_list(Args, size_t);
/* Look up the information about this file */
MPtr = __FSEXT_get_data(fd);
if (!MPtr)
{
*RV = -1;
return 1;
}
if (MPtr->Current_Ofs >= MPtr->Size)
{
*RV = 0;
return 1;
}
if (Size > (MPtr->Size - MPtr->Current_Ofs))
Size = MPtr->Size - MPtr->Current_Ofs;
memcpy(Buffer, (char*) MPtr->Ptr+MPtr->Current_Ofs, Size);
MPtr->Current_Ofs += Size;
*RV = Size;
return 1;
...
}
}
|