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.

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

d_offset<br />

d_reclen<br />

d_namelen<br />

d_name<br />

The offset into the directory we just talked about above. In our<br />

example, this will be a simple number like “0,” “1,” “2,” etc.<br />

The size of the entire struct dirent field and any extensions that<br />

may be placed <strong>with</strong>in it. The size includes any alignment filler<br />

required.<br />

The number of characters in the d_name field, not including the NUL<br />

terminator.<br />

The name of this directory entry, which must be NUL terminated.<br />

When returning the struct dirent entries, the return code passed back to the client<br />

is the number of bytes returned.<br />

Example<br />

In this example, we’re going to create a resource manager called /dev/atoz that will<br />

be a directory resource manager. It’s going to manifest the “files” /dev/atoz/a<br />

through to dev/atoz/z, <strong>with</strong> a cat of any of the files returning the uppercase letter<br />

corresponding to the filename. Here’s a sample command-line session to give you an<br />

idea of how this works:<br />

# cd /dev<br />

# ls<br />

atoz null ptyp2 socket ttyp0 ttyp3<br />

enet0 ptyp0 ptyp3 text ttyp1 zero<br />

mem ptyp1 shmem tty ttyp2<br />

# ls -ld atoz<br />

dr-xr-xr-x 1 root 0 26 Sep 05 07:59 atoz<br />

# cd atoz<br />

# ls<br />

a e i m q u y<br />

b f j n r v z<br />

c g k o s w<br />

d h l p t x<br />

# ls -l e<br />

-r--r--r-- 1 root 0 1 Sep 05 07:59 e<br />

# cat m<br />

M# cat q<br />

Q#<br />

The example above illustrates that the directory atoz shows up in the /dev directory,<br />

and that you can do an ls of the directory itself and cd into it. The /dev/atoz<br />

directory has a size of “26,” which is the number that we selected in the code. Once in<br />

the atoz directory, doing another ls shows the contents — the files a through z.<br />

Doing an ls of a particular file, say e, shows that the file is readable by all (the<br />

-r--r--r-- part) and is one byte in size. Finally, doing a few random cat’s shows<br />

that the files indeed have the stated contents. (Note that since the files contain only one<br />

byte, there’s no linefeed after the character is printed, which is why the prompt shows<br />

up on the same line as the output.)<br />

Now that we’ve seen the characteristics, let’s take a look at the code, which is<br />

organized into the following functions:<br />

April 30, 2009 Chapter 5 • Resource Managers 275

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

Saved successfully!

Ooh no, something went wrong!