James Hunkins wrote:
> I think that I found a bug in the qlib_h include file used by C68.
> Here are the details:
>
> In the 'qdirect' structure, the member 'd_name' is defined as:
>         char d_name[36];

Your misconception is that d_name holds a null terminated string,
which it doesn't. The length of the string is in d_szname. If you need
a C string, you have to copy it, for example like this:

#define MAX_FILENAME     38   // 38 because 37 is just so uneven ;-)
{
    char buffer[MAX_FILENAME];
    char *name;
[...]
    name = qlstr_to_c(buffer, (struct QLSTR *)&dir.d_szname);

Note that "name" always points to "buffer" afterwards and thus is only
valid as long as buffer is in scope. If you need it for longer, malloc
the space for buffer instead of allocating it on the stack.

> If I am correct this structure should define it as:
>
>         char d_name[37];
>
> to allow room for the termination character of a C string.

That'd be pretty fatal, not only because of possible alignment
problems but because qdirect does not define an abstract data
structure you can arbitrarily change, it just provides a mapping onto
the data structure QDOS uses (which is also the reason the string is
not null terminated) and thus has to mach byte by byte.

By the way, I suggest to write new code in a way so that it might one
day easily be changed to handle more than 36 characters. Thus the
MAX_FILENAME define in the example.

Marcel

_______________________________________________
QL-Users Mailing List
http://www.q-v-d.demon.co.uk/smsqe.htm

Reply via email to