[Ohrrpgce] rewriting isdir() function

James Paige Bob at HamsterRepublic.com
Thu Jul 30 08:38:23 PDT 2015


While I was messing around trying to figure out what was going on during my
kernel error crisis, I noticed the current implementation of the isdir()
function:

FUNCTION isdir (sDir as string) as integer
  dim ret as bool
#IFDEF __FB_ANDROID__
  '[ does not work in Android 2.2. I don't know how reliable this is
  ret = SHELL("ls " + escape_filename(sDir) + "/") = 0
#ELSEIF DEFINED(__UNIX__)
  'Special hack for broken Linux dir() behavior
  '(FIXME: is DIR still broken? Should investigate)
  ret = SHELL("[ -d " + escape_filename(sDir) + " ]") = 0
#ELSE
  'Windows just uses dir (ugh)
  'Have to remove trailing slash, otherwise dir always returns nothing
  dim temp as string = rtrim(sdir, any "\/")
  ret = dir(temp, 55) <> "" AND dir(temp, 39) = ""
#ENDIF
#IFDEF DEBUG_FILE_IO
  debuginfo "isdir(" & sDir & ") = " & ret
#ENDIF
  return ret
END FUNCTION

It has three different platform specific code-paths, and all of them seem
pretty dang hacky.

I realized that a C implementation of the same function would probably be
relatively simple, and might even be the same across all four supported
platforms.

After googling around a bit, I came up with something, but my C legs are
pretty shakey, so I was hoping somebody else could look at it before I go
and further along this path, especially if this implementation does
something dumb that I am missing or would not work on one of our platforms

int isdir(const char *dirname)
{
       struct stat sb;

       if (stat(dirname, &sb) == 0 && S_ISDIR(sb.st_mode)) {
               return 1;
       }
       return 0;
}

I am also unsure which c source file is the right place to put this. Is
miscc.c the right place?

It requires:

#include <sys/stat.h>
#include <stdio.h>

neither of which is currently included in miscc.c right now, and I have no
idea if sys.stat.h is a unix-only thing or not.

---
James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.motherhamster.org/pipermail/ohrrpgce-motherhamster.org/attachments/20150730/fe85fdb4/attachment.htm>


More information about the Ohrrpgce mailing list