26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Advanced topics<br />

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG.<br />

my_read_dir()<br />

In my_read_dir() is where the fun begins. From a high level perspective, we allocate a<br />

buffer that’s going to hold the result of this operation (called reply_msg). We then use<br />

dp to “walk” along the output buffer, stuffing struct dirent entries as we go along.<br />

The helper routine dirent_size() is used to determine if we have sufficient room in the<br />

output buffer to stuff the next entry; the helper routine dirent_fill() is used to perform<br />

the stuffing. (Note that these routines are not part of the resource manager library;<br />

they’re discussed and documented below.)<br />

On first glance this code may look inefficient; we’re using sprintf() to create a<br />

two-byte filename (the filename character and a NUL terminator) into a buffer that’s<br />

_POSIX_PATH_MAX (256) bytes long. This was done to keep the code as generic as<br />

possible.<br />

Finally, notice that we use the OCB’s offset member to indicate to us which particular<br />

filename we’re generating the struct dirent for at any given time. This means that<br />

we also have to update the offset field whenever we return data.<br />

The return of data to the client is accomplished in the “usual” way, via MsgReply().<br />

Note that the status field of MsgReply() is used to indicate the number of bytes that<br />

were sent to the client.<br />

static int<br />

my_read_dir (resmgr_context_t *ctp, io_read_t *msg,<br />

iofunc_ocb_t *ocb)<br />

{<br />

int nbytes;<br />

int nleft;<br />

struct dirent *dp;<br />

char *reply_msg;<br />

char fname [_POSIX_PATH_MAX];<br />

// allocate a buffer for the reply<br />

reply_msg = calloc (1, msg -> i.nbytes);<br />

if (reply_msg == NULL) {<br />

return (ENOMEM);<br />

}<br />

// assign output buffer<br />

dp = (struct dirent *) reply_msg;<br />

// we have "nleft" bytes left<br />

nleft = msg -> i.nbytes;<br />

while (ocb -> offset < NUM_ENTS) {<br />

// create the filename<br />

sprintf (fname, "%c", ocb -> offset + ’a’);<br />

// see how big the result is<br />

nbytes = dirent_size (fname);<br />

// do we have room for it?<br />

if (nleft - nbytes >= 0) {<br />

// fill the dirent, and advance the dirent pointer<br />

dp = dirent_fill (dp, ocb -> offset + 1,<br />

280 Chapter 5 • Resource Managers April 30, 2009

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!