d_name member of struct dirent

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default d_name member of struct dirent

    Hello,

    There is someting strange I've noticed when debugging code that use struct
    dirent.
    If you consider the follwing piece of code, which prints the name of all files
    in the directory /home/foo

    #include <dirent.h>
    #include <stdio.h>

    int main()
    {
    struct dirent *dp;
    DIR *dfd;
    dfd = opendir("/home/foo);
    while ((dp = readdir(dfd)) != NULL)
    printf("%s\n", dp->d_name);
    return 0;
    }

    It works well (I haven't provided any error checking because it is not
    relevant to my question) but if I set a breakpoint after dp has been
    initialized, for instance, right before the call to printf and check the
    value of dp->d_name, I get garbage.
    Am I missing something?

    Thank you,
    Sam
    --
    "So if you meet me, have some courtesy, have some sympathy, and some taste
    Use all your well-learned politesse, or I'll lay your soul to waste"

    - The Rolling Stones, "Sympathy for the Devil"

    Sam Zoghaib Guest

  2. Similar Questions and Discussions

    1. Issues with declaring struct arrays inside of a struct
      I have the following C++ code: define MAXXPAXX 64 // Pack sub component of database struct. typedef PREFIX_PACKED struct { DWORD packid;...
    2. Calling fun taking struct and not pointer to struct?
      Robert Feldt wrote: I'm a little confused by the question... are you asking if: 1) The act of using a struct in the declaration of another...
    3. Fwd: Calling fun taking struct and not pointer to struct?
      Related to the recent thread about nested structs and Ruby/DL here is the answer from Ruby/DL's author: So that's what I ask you: Is inlining...
    4. member.rect does not work member.regpoint does
      Hi, I'am trying to change the rect of a member but it does not work. example member("ManVrouw","Bio_images").rect=rect(0,0,100,100) at least it...
    5. struct in asp
      how can i have in asp the equivalent (in C++) of this: struct s_my_table { string name, string sexe, int age, }
  3. #2

    Default Re: d_name member of struct dirent

    Sam Zoghaib <--sam@zep.homedns.org--> wrote:
    > There is someting strange I've noticed when debugging code that use struct
    > dirent.
    > If you consider the follwing piece of code, which prints the name of all files
    > in the directory /home/foo
    > #include <dirent.h>
    > #include <stdio.h>
    > int main()
    > {
    > struct dirent *dp;
    > DIR *dfd;
    > dfd = opendir("/home/foo);
    > while ((dp = readdir(dfd)) != NULL)
    > printf("%s\n", dp->d_name);
    > return 0;
    > }
    > It works well (I haven't provided any error checking because it is not
    > relevant to my question) but if I set a breakpoint after dp has been
    > initialized, for instance, right before the call to printf and check the
    > value of dp->d_name, I get garbage.
    > Am I missing something?
    Did you compile with optimization switched on? In this case it would
    be possible (or even likely) that dp is kept in a register and the
    "real" variable in memory you're looking at in the debugger never
    gets changed, only the register.
    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| [email]Jens.Toerring@physik.fu-berlin.de[/email]
    _ | | | | | |
    | |_| | | | | | [url]http://www.physik.fu-berlin.de/~toerring[/url]
    \___/ens|_|homs|_|oerring
    Jens.Toerring@physik.fu-berlin.de Guest

  4. #3

    Default Re: d_name member of struct dirent

    [email]Jens.Toerring@physik.fu-berlin.de[/email] wrote in article
    <behp52$5bf9p$2@fu-berlin.de> on Wednesday 09 July 2003 21:07 in
    comp.unix.programmer:
    > Did you compile with optimization switched on?
    No, I didn't.

    Sam
    --
    "Fear is the path to the dark side.
    Fear leads to anger, anger leads to hatred, hatred leads to suffering.
    I sense much fear in you."

    Sam Zoghaib Guest

  5. #4

    Default Re: d_name member of struct dirent

    Sam Zoghaib wrote:
    > Hello,
    >
    > There is someting strange I've noticed when debugging code that use struct
    > dirent.
    > If you consider the follwing piece of code, which prints the name of all files
    > in the directory /home/foo
    >
    > #include <dirent.h>
    > #include <stdio.h>
    >
    > int main()
    > {
    > struct dirent *dp;
    > DIR *dfd;
    > dfd = opendir("/home/foo);
    > while ((dp = readdir(dfd)) != NULL)
    > printf("%s\n", dp->d_name);
    > return 0;
    > }
    >
    > It works well (I haven't provided any error checking because it is not
    > relevant to my question) but if I set a breakpoint after dp has been
    > initialized, for instance, right before the call to printf and check the
    > value of dp->d_name, I get garbage.
    > Am I missing something?
    >
    > Thank you,
    > Sam
    It might be something as simple as you forgot to turn on the debugging
    flag for your compilation. That might cause odd results in what you're
    seeing, based on where the debugger thinks the instructions are for the
    line you're trying to break on.

    What compiler/debugger suite are you using?

    dbtid Guest

  6. #5

    Default Re: d_name member of struct dirent

    dbtid wrote in article <2bfe7ca0a6e068b9129a9f58f7df0857@free.teranews.co m> on
    Wednesday 09 July 2003 21:29 in comp.unix.programmer:
    > It might be something as simple as you forgot to turn on the debugging
    > flag for your compilation. That might cause odd results in what you're
    > seeing, based on where the debugger thinks the instructions are for the
    > line you're trying to break on.
    No, I did not forget to turn debugging info on. In fact, that's the only
    variable for which I get garbage.
    > What compiler/debugger suite are you using?
    gcc (I tried 2.95, 3.0, 3.2 and 3.3) and the debugger is gdb.

    Sam
    --
    "Giving the Linus Torvalds Award to the Free Software Foundation is a bit like
    giving the Han Solo Award to the Rebel Alliance"

    - Richard Stallman, August 1999

    Sam Zoghaib Guest

  7. #6

    Default Re: d_name member of struct dirent

    Sam Zoghaib <--sam@zep.homedns.org--> wrote:
    > dbtid wrote in article <2bfe7ca0a6e068b9129a9f58f7df0857@free.teranews.co m> on
    > Wednesday 09 July 2003 21:29 in comp.unix.programmer:
    >> It might be something as simple as you forgot to turn on the debugging
    >> flag for your compilation. That might cause odd results in what you're
    >> seeing, based on where the debugger thinks the instructions are for the
    >> line you're trying to break on.
    > No, I did not forget to turn debugging info on. In fact, that's the only
    > variable for which I get garbage.
    >> What compiler/debugger suite are you using?
    > gcc (I tried 2.95, 3.0, 3.2 and 3.3) and the debugger is gdb.
    What make you think that you get garbage? If I run your program under
    gdb I get e.g. after the first call of readdir()

    (gdb) n
    10 printf("%s\n", dp->d_name);
    (gdb) p dp
    $1 = (struct dirent *) 0x80496c8
    (gdb) p dp->d_name
    $2 =
    ".\000\000\000\000\002\000\000\000\030\000\000\000 \020\000\000..\000\000\000\220\t\000\000,\000\000\ 000\030\000\000.Xauthority\000\000\213\b\000\000@\ 000\000\000\030\000\000.Xresources\000\000\001À\00 1\000P\000\000\000\020\000\000lab\000\000\005\b\00 0\000h\000\000\000
    \000\000.bash_history\000\000\000\000\000\000\000\ 000\023\t\000\000x\000\000\000\030\000\000.bashrc\ 000\000\000\000\000\000\a\b\000\000\220\000\000\00 0
    \000\000cperl-mode.el\000\000\000\000\000\000\000\000\202\t\000\ 000*\000\000\000\030\000\000.fvwm2rc\000\000\000\0 00\000\t\b\000"...

    What you see here isn't garbage. When you have a look at the header file
    /usr/include/bits/dirent.h where "struct dirent" is defined you will
    find that the d_name member is defined as

    char d_name[256]; /* We must not include limits.h! */

    So gdb isn't showing you the string only up to the first '\0' character
    (as it would do for a char *) but all 256 chars that belong to d_name.
    Perhaps that's why you think you're getting garbage?

    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| [email]Jens.Toerring@physik.fu-berlin.de[/email]
    _ | | | | | |
    | |_| | | | | | [url]http://www.physik.fu-berlin.de/~toerring[/url]
    \___/ens|_|homs|_|oerring
    Jens.Toerring@physik.fu-berlin.de Guest

  8. #7

    Default Re: d_name member of struct dirent

    [email]Jens.Toerring@physik.fu-berlin.de[/email] wrote in article
    <bei6ea$5hdgr$1@fu-berlin.de> on Thursday 10 July 2003 00:54 in
    comp.unix.programmer:
    > So gdb isn't showing you the string only up to the first '\0' character
    > (as it would do for a char *) but all 256 chars that belong to d_name.
    > Perhaps that's why you think you're getting garbage?
    >
    Ok, I see.
    But why isn't dp->d_name

    <name_of_entry_read>\000\000....

    here, it seems it is more like:

    <name_of_1st_entry>\000\000...<name_of_2nd_entry<\ 000....

    I know it doesn't really matter, since we'll only read up to the first \000,
    but the fact that the other entries' names appear mean that we can somehow
    know the name of several files in directory (I guess up to 256 - n
    characters, n being the number of files) with only one call to readdir(3). I
    wonder how.

    I realize I should not have called it garbage, since it probably led you to
    think I got random character. I had noticed there were filenames in the
    output of gdb, but did not notice that the chars up to the first \000 were
    always the filename I was looking for. Sorry for the inaccuracy.

    Thank you,
    Sam
    --
    "Don't be afraid, I'm gonna give you the choice I never had..."

    - Lestat in "Interview with the Vampire" (Ann Rice, 1976)

    Sam Zoghaib Guest

  9. #8

    Default Re: d_name member of struct dirent


    "Sam Zoghaib >" <<sam@zep.homedns.org> wrote in message
    news:bejfbj$m6v$1@news-reader6.wanadoo.fr...
    > I know it doesn't really matter, since we'll only read up to the first
    \000,
    > but the fact that the other entries' names appear mean that we can somehow
    > know the name of several files in directory (I guess up to 256 - n
    > characters, n being the number of files) with only one call to readdir(3).
    I
    > wonder how.
    Until the system read the file name, it had no idea how big it was. So
    it read more bytes than needed. This way, the next call to 'readdir' won't
    require a real read (since the system already ready it).

    It also might just read the whole directory in one shot. Then it points
    you at pieces of it as you call 'readdir'. If you think about it, what would
    make the least sense is trying to actually read/parse the directory file by
    file.

    DS


    David Schwartz Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139