(UNIX) non-POSIX compliant code segment

Want to discuss changes to the UOX3 source code? Got a code-snippet you'd like to post? Anything related to coding/programming goes here!
Post Reply
xir
Has thanked: 0
Been thanked: 0

(UNIX) non-POSIX compliant code segment

Post 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 :)
xir
Has thanked: 0
Been thanked: 0

Post 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
xir
Has thanked: 0
Been thanked: 0

Post by xir »

BTW the changes should work on linux also as well as Unix. I'm nearly sure stat() is standard.
Maarc
Developer
Posts: 576
Joined: Sat Mar 27, 2004 6:22 am
Location: Fleet, UK
Has thanked: 0
Been thanked: 0
Contact:

Post 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).
Post Reply