Go to the first, previous, next, last section, table of contents.


__FSEXT_set_data

Syntax

#include <sys/fsext.h>

int __FSEXT_set_data(int _fd, void *_data);

Description

This function is part of the section File System Extensions. It is used to store a descriptor-specific pointer that can later be retrieved by __FSEXT_get_data (see section __FSEXT_get_data). The pointer is not otherwise used.

This is useful when writing an extension that may be handling several open pseudo-files. __FSEXT_set_data can be used when creating or opening the file to store a pointer to data about the specific file. Later, when specific operation needs to be done (e.g. read, write, etc.) a pointer to pseudo-file associated with the file descriptor can be fetched with __FSEXT_get_data.

Return Value

Returns the pointer you passed it, or NULL if there was an error.

Portability

not ANSI, not POSIX

Example

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 lookup 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; ... } }


Go to the first, previous, next, last section, table of contents.