Page 1 of 1

(UNIX) non-POSIX compliant code segment

Posted: Thu Jul 29, 2004 11:58 pm
by xir

Code: Select all


void cDirectoryListing::InternalRetrieve( void )
{
  char filePath[512];
#if defined(__unix__)
  DIR *dir = opendir(".");
  struct dirent *dirp = NULL;

  while( ( dirp = readdir( dir ) ) )
  {
    if( dirp->d_type == DT_DIR && doRecursion )
    {
      if( strcmp( dirp->d_name, "." ) && strcmp( dirp->d_name, ".." ) )
        subdirectories.push_back( cDirectoryListing( currentDir + "/" + dirp->d_name, extension, doRecursion ) );
      continue;
    }
    shortList.push_back( dirp->d_name );
    sprintf( filePath, "%s/%s", currentDir.c_str(), dirp->d_name );
    filenameList.push_back( filePath );
  }

cServerDefinitions.cpp around line 480

Ok since the latest thing is to try to get it working in linux, I said i'll try my hand with it on Solaris. It mostly compiles except for a few problems. The main one I found was this piece of code.

The d_type member in struct dirent isn't POSIX compliant. If you define __USE_BSD it will work on Linux because it supports this but looking at "man standards" I found out that solaris doesn't support this standard. I think one can rewrite this using stat() if someone feels up to it and make it Unix compilable too :)

Just messing with it and thought I'd post it here for reference if anyone ever needs to get it compiled on Solaris, they'll know what to do :)

Posted: Fri Jul 30, 2004 11:28 pm
by xir
I think this should be the fix. I don't have a linux box to test it on and I don't have mul files on my unix shell so I'm not 100% if the changes work.

Code: Select all

#if defined(__unix__)
  DIR *dir = opendir(".");
  struct dirent *dirp = NULL;
  struct stat dirstat;

  while( ( dirp = readdir( dir ) ) )
  {
    stat(dirp->d_name, &dirstat);
    if( S_ISDIR(dirstat.st_mode) && doRecursion )
    {
      if( strcmp( dirp->d_name, "." ) && strcmp( dirp->d_name, ".." ) )
      {
        subdirectories.push_back( cDirectoryListing( currentDir + "/" + dirp->d_name, extension, doRecursion ) );
        printf("%s/%s/n",currentDir, dirp->d_name);
        continue;
      }
    }
    shortList.push_back( dirp->d_name );
    sprintf( filePath, "%s/%s", currentDir.c_str(), dirp->d_name );
    filenameList.push_back( filePath );
  }

#else

Posted: Fri Jul 30, 2004 11:29 pm
by xir
BTW the changes should work on linux also as well as Unix. I'm nearly sure stat() is standard.

Posted: Sat Jul 31, 2004 2:51 am
by Maarc
Thanks for the info, but it surprises me. I built that code based on a textbook I have here (Network Programming for Unix I think it was).