Skip to contentSkip to conversation

readdir(3) return and error code

Linux-ia64 Jacky Malcles 2 participants 2 messages May 2, 2002

lore
Share & tools
lore
#0 lore raw

While doing some testing I came across something that is odd for me, according to readdir(3) EBADF is returned for an invalid argument.

I'm doing a basic call with a bad argument i,e:

    struct dirent *dptr;
    dptr = readdir("test_dir_1");

I thought that the call would return
errno = 9 : Bad file descriptor and dptr=NULL

but,

on an ia32 platform (Linux version 2.4.9-31custom)
I get:

Program received signal SIGSEGV, Segmentation fault. __readdir (dirp=0x804be44) at ../sysdeps/unix/readdir.c:82
82 ../sysdeps/unix/readdir.c: No such file or directory.
in ../sysdeps/unix/readdir.c
(gdb) where

#0  __readdir (dirp=0x804be44) at ../sysdeps/unix/readdir.c:82
#1  0x08049113 in main ()
#2  0x40045507 in __libc_start_main (main=0x8049020 <main>, argc=1,
    ubp_av=0xbfffe524, init=0x8048b7c <_init>, fini=0x804bde0 <_fini>,
    rtld_fini=0x4000dc14 <_dl_fini>, stack_end=0xbfffe51c)
    at ../sysdeps/generic/libc-start.c:129

(gdb)

if I do this to an ia64 platform (Linux version 2.4.17) I get
errno = 9 : Bad file descriptor and dptr=NULL,

so, is there anything wrong with the libc ?
can or cannot I write a test code like that ?
is it a pb of shell, version ...

thanks for your help,

Email : [email protected]

#1 lore raw

Jacky Malcles <[email protected]> writes:

|> While doing some testing I came across something that is odd for me, |> according to readdir(3) EBADF is returned for an invalid argument.

The POSIX standard says:

The readdir() function may fail if:

[EBADF] The dirp argument does not refer to an open directory stream.

(Note that readdir() is not _required_ to fail in this case.)

|> I'm doing a basic call with a bad argument i,e:
|>     struct dirent *dptr;
|>     dptr = readdir("test_dir_1");

This is complete garbage, so you cannot expect anything usefull according to the GIGO priciple. A string is not remotely anything matching DIR*, all bets are off.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE GmbH, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."