Break down dirpath.c into fs_*.c by OS

I much prefer this to using ifdefs. Requires a little help from the build
system but I find it worth it.
This commit is contained in:
Lassi Kortela 2019-08-09 23:12:19 +03:00
parent 99feb308bd
commit 1d96278313
16 changed files with 147 additions and 161 deletions

View File

@ -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"

View File

@ -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);

View File

@ -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"

View File

@ -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"

View File

@ -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];

4
c/fs.h Normal file
View File

@ -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);

11
c/fs_darwin.c Normal file
View File

@ -0,0 +1,11 @@
#include <mach-o/dyld.h>
#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;
}

17
c/fs_freebsd.c Normal file
View File

@ -0,0 +1,17 @@
#include <sys/types.h>
#include <sys/sysctl.h>
#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;
}

33
c/fs_linux.c Normal file
View File

@ -0,0 +1,33 @@
#include <string.h>
#include <unistd.h>
#include "fs.h"
char *get_exename(char *buf, size_t size)
{
char linkname[64]; /* /proc/<pid>/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;
}

View File

@ -1,98 +1,13 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "dtypes.h"
#ifdef WIN32
#include <malloc.h>
#include <sys/timeb.h>
#include <windows.h>
#undef NO_ERROR
#undef MOD_SHIFT
#undef TRUE
#undef FALSE
#undef VOID
#else
#include <sys/time.h>
#include <sys/poll.h>
#include <unistd.h>
#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/<pid>/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 <sys/param.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <stdlib.h>
#include <string.h>
#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 <sys/types.h>
#include <sys/sysctl.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;
}
#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 <mach-o/dyld.h>
char *get_exename(char *buf, size_t size)
{
uint32_t bufsize = (uint32_t)size;
if (_NSGetExecutablePath(buf, &bufsize))
return NULL;
return buf;
}
#endif

23
c/fs_unix.c Normal file
View File

@ -0,0 +1,23 @@
#include <string.h>
#include <unistd.h>
#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;
}

33
c/fs_windows.c Normal file
View File

@ -0,0 +1,33 @@
#include <windows.h>
#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;
}

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -5,7 +5,8 @@ CFLAGS="-Wall -Wextra -Wno-strict-aliasing -std=gnu99"
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 \
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
ln -s ../scheme-boot/flisp.boot flisp.boot
{ set +x; } 2>/dev/null
cd ../scheme-core
echo "Entering directory '$PWD'"