Problem with using scandir in Solaris?

I have written a program using scandir to list the files in a dir

#include <sys/types.h>

#include <stdio.h>

#include <dirent.h>

extern int scandir();

extern int alphasort();



int num_entries, i;

struct dirent **namelist, **list;

if ((num_entries =

scandir("/tmp", &namelist, NULL, alphasort)) < 0) {

fprintf(stderr, "Unexpected error\n");



printf("Number of entries is %d\n", num_entries);

if (num_entries) {

printf("Entries are:");

for (i=0, list=namelist; i<num_entries; i++) {

printf(" %s", (*list)->d_name);










when i run the same it gives an error

Undefined first referenced

symbol in file

alphasort /var/tmp/ccobA9PI.o

scandir /var/tmp/ccobA9PI.o

collect2: ld returned 1 exit status

hlp plz

2 Answers

  • morgan
    Lv 7
    1 decade ago
    Best Answer

    Solaris has always had "scandir", but it used to

    be pretty difficult to use. Up through about Solaris 8,

    it was considered to be a "Berkely" function, so

    you had to use it from /usr/ucblib. By itself, that

    wasn't too difficult, but you also had to make sure

    that you got the dirent structure from /usr/ucbinclude

    or you'd discover that the first two characters of

    every directory entry would be chopped off (because

    the runtime structure in the library wouldn't match

    the #included structure in your program.

    The comiler line would be *something* like:

    cc -I/usr/ucbinclude x.c -L/usr/ucblib -R/usr/ucblib -lucb

    Generally, though, I wouldn't recommend using those

    UCB routines because the complexity of matching up

    the headers and libraries is error prone.

    Using the source version from the other answer may be

    your best bet if you have to stay on an old version

    of Solaris. Also, here is the open source version

    of scandir and alphasort from Solaris 10:

    However, please note that scandir has been moved into libc

    in Solaris 10. Also, if you're considering an upgrade,

    Solaris 10 is free, is open source (see ref #1), and

    has free-to-use compilers/debugger/analyzer.

  • 1 decade ago

    this may help you.

    your solaris version MAY not implement scandir..

Still have questions? Get your answers by asking now.