<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 31 July 2015 at 03:38, James Paige <span dir="ltr"><<a href="mailto:Bob@hamsterrepublic.com" target="_blank">Bob@hamsterrepublic.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div><div>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:<br><br>FUNCTION isdir (sDir as string) as integer<br>  dim ret as bool<br>#IFDEF __FB_ANDROID__<br>  '[ does not work in Android 2.2. I don't know how reliable this is<br>  ret = SHELL("ls " + escape_filename(sDir) + "/") = 0<br>#ELSEIF DEFINED(__UNIX__)<br>  'Special hack for broken Linux dir() behavior<br>  '(FIXME: is DIR still broken? Should investigate)<br>  ret = SHELL("[ -d " + escape_filename(sDir) + " ]") = 0<br>#ELSE<br>  'Windows just uses dir (ugh)<br>  'Have to remove trailing slash, otherwise dir always returns nothing<br>  dim temp as string = rtrim(sdir, any "\/")<br>  ret = dir(temp, 55) <> "" AND dir(temp, 39) = ""<br>#ENDIF<br>#IFDEF DEBUG_FILE_IO<br>  debuginfo "isdir(" & sDir & ") = " & ret<br>#ENDIF<br>  return ret<br>END FUNCTION<br><br></div>It has three different platform specific code-paths, and all of them seem pretty dang hacky.<br><br></div>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.<br><br></div>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<br><br>int isdir(const char *dirname)<br>{<br>       struct stat sb;<br><br>       if (stat(dirname, &sb) == 0 && S_ISDIR(sb.st_mode)) {<br>               return 1;<br>       }<br>       return 0;<br>}<br><br></div>I am also unsure which c source file is the right place to put this. Is miscc.c the right place?<br><br></div>It requires:<br><br>#include <sys/stat.h>
<br>#include <stdio.h>
<br><br></div>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.<br><br></div>---<br></div>James<br></div></blockquote><div><br></div><div>It's probably about time that that was replaced, especially since the C equivalent is so simple.<br><br></div><div>That is unix-specific code, so should probably go in os_unix.c. Also, when calling POSIX functions I prefer to take a look at Apple's man page for them because there are always differences, however there wasn't anything relevant in this case.<br><br></div><div>I suggest checking for and printing errors:<br><br>int isdir(const char *dirname)<br>{<br>    struct stat sb;<br><br>    if (stat(dirname, &sb)) {<br>        // Ignore "A component of pathname does not exist, or pathname is an empty string."                                          <br>        if (errno != ENOENT)<br>            debug(errError, "isdir(%s) error: %s", dirname, strerror(errno));<br>        return 0;<br>    } else<br>        return S_ISDIR(sb.st_mode);<br>}<br><br></div><div>However Windows also tends to have versions of these sorts of POSIX functions, though it seems it is called _stat on Windows:<br><a href="https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx">https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx</a><br></div><div>Mingw also tends to define additional unix compatibility functions. In this case it looks like stat() is defined as an alias for _stat(). So try it out, if it works you can just use a single implementation and put it in miscc.c.<br></div><br></div></div></div>