diff --git a/c/builtins.c b/c/builtins.c index dd7e35c..0891ae2 100644 --- a/c/builtins.c +++ b/c/builtins.c @@ -27,7 +27,7 @@ #include "htable.h" #include "htableh_inc.h" #include "bitvector.h" -#include "dirpath.h" +#include "fs.h" #include "random.h" #include "llt.h" diff --git a/c/dirpath.h b/c/dirpath.h deleted file mode 100644 index b4e8444..0000000 --- a/c/dirpath.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifdef WIN32 -#define PATHSEP '\\' -#define PATHSEPSTRING "\\" -#define PATHLISTSEP ';' -#define PATHLISTSEPSTRING ";" -#define ISPATHSEP(c) ((c) == '/' || (c) == '\\') -#define MAXPATHLEN 1024 -#else -#define PATHSEP '/' -#define PATHSEPSTRING "/" -#define PATHLISTSEP ':' -#define PATHLISTSEPSTRING ":" -#define ISPATHSEP(c) ((c) == '/') -#endif - -void get_cwd(char *buf, size_t size); -int set_cwd(char *buf); -char *get_exename(char *buf, size_t size); -void path_to_dirname(char *path); diff --git a/c/equalhash.c b/c/equalhash.c index 6811138..3c9f813 100644 --- a/c/equalhash.c +++ b/c/equalhash.c @@ -16,7 +16,7 @@ #include "htable.h" #include "htableh_inc.h" #include "bitvector.h" -#include "dirpath.h" +#include "fs.h" #include "random.h" #include "llt.h" diff --git a/c/flisp.c b/c/flisp.c index eb29a27..3cb3cfd 100644 --- a/c/flisp.c +++ b/c/flisp.c @@ -55,7 +55,7 @@ #include "htable.h" #include "htableh_inc.h" #include "bitvector.h" -#include "dirpath.h" +#include "fs.h" #include "random.h" #include "llt.h" diff --git a/c/flmain.c b/c/flmain.c index 506fed8..7a6e05c 100644 --- a/c/flmain.c +++ b/c/flmain.c @@ -15,7 +15,7 @@ #include "htable.h" #include "htableh_inc.h" #include "bitvector.h" -#include "dirpath.h" +#include "fs.h" #include "random.h" #include "llt.h" @@ -50,8 +50,7 @@ int main(int argc, char *argv[]) if (exedir == NULL) { snprintf(fname_buf, sizeof(fname_buf), "%s", bootfile); } else { - snprintf(fname_buf, sizeof(fname_buf), "%s%s%s", exedir, - PATHSEPSTRING, bootfile); + snprintf(fname_buf, sizeof(fname_buf), "%s/%s", exedir, bootfile); } value_t args[2]; diff --git a/c/fs.h b/c/fs.h new file mode 100644 index 0000000..a73076a --- /dev/null +++ b/c/fs.h @@ -0,0 +1,4 @@ +void path_to_dirname(char *path); +void get_cwd(char *buf, size_t size); +int set_cwd(char *buf); +char *get_exename(char *buf, size_t size); diff --git a/c/fs_darwin.c b/c/fs_darwin.c new file mode 100644 index 0000000..da8f2db --- /dev/null +++ b/c/fs_darwin.c @@ -0,0 +1,11 @@ +#include + +#include "fs.h" + +char *get_exename(char *buf, size_t size) +{ + uint32_t bufsize = (uint32_t)size; + if (_NSGetExecutablePath(buf, &bufsize)) + return NULL; + return buf; +} diff --git a/c/fs_freebsd.c b/c/fs_freebsd.c new file mode 100644 index 0000000..47a78dc --- /dev/null +++ b/c/fs_freebsd.c @@ -0,0 +1,17 @@ +#include + +#include + +#include "fs.h" + +char *get_exename(char *buf, size_t size) +{ + int mib[4]; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PATHNAME; + mib[3] = -1; + sysctl(mib, 4, buf, &size, NULL, 0); + + return buf; +} diff --git a/c/fs_linux.c b/c/fs_linux.c new file mode 100644 index 0000000..faaa40a --- /dev/null +++ b/c/fs_linux.c @@ -0,0 +1,33 @@ +#include +#include + +#include "fs.h" + +char *get_exename(char *buf, size_t size) +{ + char linkname[64]; /* /proc//exe */ + pid_t pid; + ssize_t ret; + + /* Get our PID and build the name of the link in /proc */ + pid = getpid(); + + if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", pid) < 0) + return NULL; + + /* Now read the symbolic link */ + ret = readlink(linkname, buf, size); + + /* In case of an error, leave the handling up to the caller */ + if (ret == -1) + return NULL; + + /* Report insufficient buffer size */ + if ((size_t)ret >= size) + return NULL; + + /* Ensure proper NUL termination */ + buf[ret] = 0; + + return buf; +} diff --git a/c/dirpath.c b/c/fs_openbsd.c similarity index 62% rename from c/dirpath.c rename to c/fs_openbsd.c index 2550418..48e0d05 100644 --- a/c/dirpath.c +++ b/c/fs_openbsd.c @@ -1,98 +1,13 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "dtypes.h" - -#ifdef WIN32 -#include -#include -#include -#undef NO_ERROR -#undef MOD_SHIFT -#undef TRUE -#undef FALSE -#undef VOID -#else -#include -#include -#include -#endif - -#include "dirpath.h" - -void get_cwd(char *buf, size_t size) -{ -#ifndef WIN32 - getcwd(buf, size); -#else - GetCurrentDirectory(size, buf); -#endif -} - -int set_cwd(char *buf) -{ -#ifndef WIN32 - if (chdir(buf) == -1) - return 1; -#else - if (SetCurrentDirectory(buf) == 0) - return 1; -#endif - return 0; -} - -// destructively convert path to directory part -void path_to_dirname(char *path) -{ - char *sep = strrchr(path, PATHSEP); - if (sep != NULL) { - *sep = '\0'; - } else { - path[0] = '\0'; - } -} - -#ifdef LINUX -char *get_exename(char *buf, size_t size) -{ - char linkname[64]; /* /proc//exe */ - pid_t pid; - ssize_t ret; - - /* Get our PID and build the name of the link in /proc */ - pid = getpid(); - - if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", pid) < 0) - return NULL; - - /* Now read the symbolic link */ - ret = readlink(linkname, buf, size); - - /* In case of an error, leave the handling up to the caller */ - if (ret == -1) - return NULL; - - /* Report insufficient buffer size */ - if ((size_t)ret >= size) - return NULL; - - /* Ensure proper NUL termination */ - buf[ret] = 0; - - return buf; -} -#elif defined(OPENBSD) #include + +#include #include +#include +#include + +#include "fs.h" + char *get_exename(char *buf, size_t size) { int mib[4]; @@ -197,36 +112,3 @@ char *get_exename(char *buf, size_t size) return buf; } -#elif defined(FREEBSD) -#include -#include - -char *get_exename(char *buf, size_t size) -{ - int mib[4]; - mib[0] = CTL_KERN; - mib[1] = KERN_PROC; - mib[2] = KERN_PROC_PATHNAME; - mib[3] = -1; - sysctl(mib, 4, buf, &size, NULL, 0); - - return buf; -} -#elif defined(WIN32) -char *get_exename(char *buf, size_t size) -{ - if (GetModuleFileName(NULL, buf, size) == 0) - return NULL; - - return buf; -} -#elif defined(MACOSX) -#include -char *get_exename(char *buf, size_t size) -{ - uint32_t bufsize = (uint32_t)size; - if (_NSGetExecutablePath(buf, &bufsize)) - return NULL; - return buf; -} -#endif diff --git a/c/fs_unix.c b/c/fs_unix.c new file mode 100644 index 0000000..4415a39 --- /dev/null +++ b/c/fs_unix.c @@ -0,0 +1,23 @@ +#include +#include + +#include "fs.h" + +void path_to_dirname(char *path) +{ + char *p; + + if (!(p = strrchr(path, '/'))) { + p = path; + } + *p = '\0'; +} + +void get_cwd(char *buf, size_t size) { getcwd(buf, size); } + +int set_cwd(char *buf) +{ + if (chdir(buf) == -1) + return 1; + return 0; +} diff --git a/c/fs_windows.c b/c/fs_windows.c new file mode 100644 index 0000000..cc51c96 --- /dev/null +++ b/c/fs_windows.c @@ -0,0 +1,33 @@ +#include + +#include "fs.h" + +void path_to_dirname(char *path) +{ + char *p; + + p = strchr(path, 0); + while (p > path) { + p--; + if ((*p == '/') || (*p == '\\')) { + break; + } + } + *p = '\0'; +} + +void get_cwd(char *buf, size_t size) { GetCurrentDirectory(size, buf); } + +int set_cwd(char *buf) +{ + if (SetCurrentDirectory(buf) == 0) + return 1; + return 0; +} + +char *get_exename(char *buf, size_t size) +{ + if (GetModuleFileName(NULL, buf, size) == 0) + return NULL; + return buf; +} diff --git a/c/iostream.c b/c/iostream.c index 89eb8d4..6c0506f 100644 --- a/c/iostream.c +++ b/c/iostream.c @@ -17,7 +17,7 @@ #include "htable.h" #include "htableh_inc.h" #include "bitvector.h" -#include "dirpath.h" +#include "fs.h" #include "random.h" #include "llt.h" diff --git a/c/string.c b/c/string.c index 62a8a2f..736ef17 100644 --- a/c/string.c +++ b/c/string.c @@ -26,7 +26,7 @@ #include "htable.h" #include "htableh_inc.h" #include "bitvector.h" -#include "dirpath.h" +#include "fs.h" #include "random.h" #include "llt.h" diff --git a/c/table.c b/c/table.c index f8220fe..ea62200 100644 --- a/c/table.c +++ b/c/table.c @@ -17,7 +17,7 @@ #include "htable.h" #include "htableh_inc.h" #include "bitvector.h" -#include "dirpath.h" +#include "fs.h" #include "random.h" #include "llt.h" diff --git a/scripts/build.sh b/scripts/build.sh index f0178a1..e874771 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -2,10 +2,11 @@ set -eu CC="${CC:-clang}" CFLAGS="-Wall -Wextra -Wno-strict-aliasing -std=gnu99" -CFLAGS="$CFLAGS -O2" # -falign-functions +CFLAGS="$CFLAGS -O2" # -falign-functions CFLAGS="$CFLAGS -I ../c -D NDEBUG -D USE_COMPUTED_GOTO" LFLAGS="-lm" -builddir="build-$(uname | tr A-Z- a-z_)-$(uname -m | tr A-Z- a-z_)" +os="$(uname | tr A-Z- a-z_)" +builddir="build-$os-$(uname -m | tr A-Z- a-z_)" cd "$(dirname "$0")"/.. echo "Entering directory '$PWD'" set -x @@ -15,14 +16,16 @@ find "$builddir" -mindepth 1 -delete cd "$builddir" echo "Entering directory '$PWD'" set -x +ln -s ../scheme-boot/flisp.boot flisp.boot $CC $CFLAGS -c ../c/bitvector-ops.c $CC $CFLAGS -c ../c/bitvector.c $CC $CFLAGS -c ../c/builtins.c -$CC $CFLAGS -c ../c/dirpath.c $CC $CFLAGS -c ../c/dump.c $CC $CFLAGS -c ../c/equalhash.c $CC $CFLAGS -c ../c/flisp.c $CC $CFLAGS -c ../c/flmain.c +$CC $CFLAGS -c ../c/fs_"$os".c +$CC $CFLAGS -c ../c/fs_unix.c $CC $CFLAGS -c ../c/hashing.c $CC $CFLAGS -c ../c/htable.c $CC $CFLAGS -c ../c/int2str.c @@ -37,11 +40,11 @@ $CC $CFLAGS -c ../c/table.c $CC $CFLAGS -c ../c/timefuncs.c $CC $CFLAGS -c ../c/utf8.c $CC $LFLAGS -o flisp -lm \ - bitvector-ops.o bitvector.o builtins.o dirpath.o dump.o \ - equalhash.o flisp.o flmain.o hashing.o htable.o int2str.o \ - ios.o iostream.o lltinit.o ptrhash.o random.o socket.o \ - string.o table.o timefuncs.o utf8.o -ln -s ../scheme-boot/flisp.boot flisp.boot + bitvector-ops.o bitvector.o builtins.o dump.o \ + equalhash.o flisp.o flmain.o fs_"$os".o fs_unix.o \ + hashing.o htable.o int2str.o \ + ios.o iostream.o lltinit.o ptrhash.o random.o socket.o \ + string.o table.o timefuncs.o utf8.o { set +x; } 2>/dev/null cd ../scheme-core echo "Entering directory '$PWD'"