diff --git a/cmake/sources.cmake b/cmake/sources.cmake
index 6dd7de3..d7b74d7 100644
--- a/cmake/sources.cmake
+++ b/cmake/sources.cmake
@@ -164,6 +164,7 @@ SET(sword_base_utilfns_SOURCES
 	src/utilfuns/ftpparse.c
 	src/utilfuns/url.cpp
 	src/utilfuns/roman.cpp
+        src/utilfuns/swopen.cpp
 )
 SOURCE_GROUP("src\\utilfns" FILES ${sword_base_utilfns_SOURCES})
 
diff --git a/include/swopen.h b/include/swopen.h
new file mode 100644
index 0000000..3b83ed2
--- /dev/null
+++ b/include/swopen.h
@@ -0,0 +1,48 @@
+/******************************************************************************
+*  swopen.h  - declarations for sword file handling methods
+*
+* $Id$
+*
+* Copyright 2013 CrossWire Bible Society (http://www.crosswire.org)
+*       CrossWire Bible Society
+*       P. O. Box 2528
+*       Tempe, AZ  85280-2528
+*
+* This program is free software; you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by the
+* Free Software Foundation version 2.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+* General Public License for more details.
+*
+*/
+#ifndef SWOPEN_H
+#define SWOPEN_H
+
+#include <stdio.h>
+
+typedef struct _SWDir SWDir;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int sw_open(const char* filename, int flags, int mode);
+int sw_access(const char* filename, int mode);
+int sw_remove(const char* filename);
+int sw_mkdir(const char* filename, int mode);
+FILE* sw_fopen(const char* filename, const char *mode);
+
+SWDir* sw_dir_open (const char* path);
+void sw_dir_rewind(SWDir *dir);
+void sw_dir_close(SWDir *dir);
+const char* sw_dir_read_name(SWDir *dir);
+
+const char* sw_getenv(const char* variable);
+#ifdef __cplusplus
+}
+#endif
+// swgetenv
+#endif // SWOPEN_H
diff --git a/src/mgr/curlftpt.cpp b/src/mgr/curlftpt.cpp
index 046291e..cd83e34 100644
--- a/src/mgr/curlftpt.cpp
+++ b/src/mgr/curlftpt.cpp
@@ -24,6 +24,7 @@
 #include <curlftpt.h>
 
 #include <fcntl.h>
+#include <swopen.h>
 
 #include <curl/curl.h>
 #include <curl/easy.h>
@@ -57,7 +58,7 @@ int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) {
 	struct FtpFile *out=(struct FtpFile *)stream;
 	if (out && !out->stream && !out->destBuf) {
 		/* open file for writing */
-		out->stream=fopen(out->filename, "wb");
+        out->stream=sw_fopen(out->filename, "wb");
 		if (!out->stream)
 			return -1; /* failure, can't open file to write */
 	}
diff --git a/src/mgr/filemgr.cpp b/src/mgr/filemgr.cpp
index 5b45049..18aa469 100644
--- a/src/mgr/filemgr.cpp
+++ b/src/mgr/filemgr.cpp
@@ -22,6 +22,7 @@
 
 #include <filemgr.h>
 #include <utilstr.h>
+#include <swopen.h>
 
 #include <dirent.h>
 #include <fcntl.h>
@@ -225,14 +226,14 @@ int FileMgr::sysOpen(FileDesc *file) {
 				file->next = files;
 				files = file;
 			}
-			if ((!access(file->path, 04)) || ((file->mode & O_CREAT) == O_CREAT)) {	// check for at least file exists / read access before we try to open
+            if ((!sw_access(file->path, 04)) || ((file->mode & O_CREAT) == O_CREAT)) {	// check for at least file exists / read access before we try to open
 				char tries = (((file->mode & O_RDWR) == O_RDWR) && (file->tryDowngrade)) ? 2 : 1;  // try read/write if possible
 				for (int i = 0; i < tries; i++) {
 					if (i > 0) {
 						file->mode = (file->mode & ~O_RDWR);	// remove write access
 						file->mode = (file->mode | O_RDONLY);// add read access
 					}
-					file->fd = ::open(file->path, file->mode|O_BINARY, file->perms);
+                    file->fd = sw_open(file->path, file->mode|O_BINARY, file->perms);
 
 					if (file->fd >= 0)
 						break;
@@ -275,7 +276,7 @@ signed char FileMgr::trunc(FileDesc *file) {
 		if (i == 9999)
 			return -2;
 
-		int fd = ::open(buf, O_CREAT|O_RDWR, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
+        int fd = sw_open(buf, O_CREAT|O_RDWR, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
 		if (fd < 0)
 			return -3;
 	
@@ -289,7 +290,7 @@ signed char FileMgr::trunc(FileDesc *file) {
 		if (size < 1) {
 			// zero out the file
 			::close(file->fd);
-			file->fd = ::open(file->path, O_TRUNC, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
+            file->fd = sw_open(file->path, O_TRUNC, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
 			::close(file->fd);
 			file->fd = -77;	// force file open by filemgr
 			// copy tmp file back (dumb, but must preserve file permissions)
@@ -327,7 +328,7 @@ signed char FileMgr::existsFile(const char *ipath, const char *ifileName)
 		ch = path + strlen(path);
 		sprintf(ch, "/%s", ifileName);
 	}
-	signed char retVal = !access(path, 04);
+    signed char retVal = !sw_access(path, 04);
 	delete [] path;
 	return retVal;
 }
@@ -349,7 +350,7 @@ signed char FileMgr::existsDir(const char *ipath, const char *idirName)
 		ch = path + strlen(path);
 		sprintf(ch, "/%s", idirName);
 	}
-	signed char retVal = !access(path, 04);
+    signed char retVal = !sw_access(path, 04);
 	delete [] path;
 	return retVal;
 }
@@ -368,17 +369,13 @@ int FileMgr::createParent(const char *pName) {
 	}
 	buf[end] = 0;
 	if (strlen(buf)>0) {
-		if (access(buf, 02)) {  // not exists with write access?
-			if ((retCode = mkdir(buf
-#ifndef WIN32
+        if (sw_access(buf, 02)) {  // not exists with write access?
+            if ((retCode = sw_mkdir(buf
 					, 0755
-#endif
 					))) {
 				createParent(buf);
-				retCode = mkdir(buf
-#ifndef WIN32
+                retCode = sw_mkdir(buf
 					, 0755
-#endif
 					);
 			}
 		}
@@ -390,7 +387,7 @@ int FileMgr::createParent(const char *pName) {
 	
 
 int FileMgr::openFileReadOnly(const char *fName) {
-	int fd = ::open(fName, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
+    int fd = sw_open(fName, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
 	return fd;
 }
 
@@ -398,10 +395,10 @@ int FileMgr::openFileReadOnly(const char *fName) {
 int FileMgr::createPathAndFile(const char *fName) {
 	int fd;
 	
-	fd = ::open(fName, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
+    fd = sw_open(fName, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
 	if (fd < 1) {
 		createParent(fName);
-		fd = ::open(fName, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
+        fd = sw_open(fName, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
 	}
 	return fd;
 }
@@ -411,7 +408,7 @@ int FileMgr::copyFile(const char *sourceFile, const char *targetFile) {
 	int sfd, dfd, len;
 	char buf[4096];
 
-	if ((sfd = ::open(sourceFile, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH)) < 1)
+    if ((sfd = sw_open(sourceFile, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH)) < 1)
 		return -1;
 	if ((dfd = createPathAndFile(targetFile)) < 1)
 		return -1;
@@ -429,7 +426,7 @@ int FileMgr::copyFile(const char *sourceFile, const char *targetFile) {
 
 
 int FileMgr::removeFile(const char *fName) {
-	return ::remove(fName);
+    return sw_remove(fName);
 }
 
 char FileMgr::getLine(FileDesc *fDesc, SWBuf &line) {
@@ -506,14 +503,14 @@ char FileMgr::isDirectory(const char *path) {
 
 
 int FileMgr::copyDir(const char *srcDir, const char *destDir) {
-	DIR *dir;
-	struct dirent *ent;
-	if ((dir = opendir(srcDir))) {
-		rewinddir(dir);
-		while ((ent = readdir(dir))) {
-			if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
-				SWBuf srcPath  = (SWBuf)srcDir  + (SWBuf)"/" + ent->d_name;
-				SWBuf destPath = (SWBuf)destDir + (SWBuf)"/" + ent->d_name;
+    SWDir *dir;
+    const char *ent;
+    if ((dir = sw_dir_open(srcDir))) {
+        sw_dir_rewind(dir);
+        while ((ent = sw_dir_read_name(dir))) {
+            if ((strcmp(ent, ".")) && (strcmp(ent, ".."))) {
+                SWBuf srcPath  = (SWBuf)srcDir  + (SWBuf)"/" + ent;
+                SWBuf destPath = (SWBuf)destDir + (SWBuf)"/" + ent;
 				if (!isDirectory(srcPath.c_str())) {
 					copyFile(srcPath.c_str(), destPath.c_str());
 				}
@@ -522,20 +519,20 @@ int FileMgr::copyDir(const char *srcDir, const char *destDir) {
 				}
 			}
 		}
-		closedir(dir);
+        sw_dir_close(dir);
 	}
 	return 0;
 }
 
 
 int FileMgr::removeDir(const char *targetDir) {
-	DIR *dir = opendir(targetDir);
-	struct dirent *ent;
+    SWDir *dir = sw_dir_open(targetDir);
+    const char *ent;
 	if (dir) {
-		rewinddir(dir);
-		while ((ent = readdir(dir))) {
-			if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
-				SWBuf targetPath = (SWBuf)targetDir + (SWBuf)"/" + ent->d_name;
+        sw_dir_rewind(dir);
+        while ((ent = sw_dir_read_name(dir))) {
+            if ((strcmp(ent, ".")) && (strcmp(ent, ".."))) {
+                SWBuf targetPath = (SWBuf)targetDir + (SWBuf)"/" + ent;
 				if (!isDirectory(targetPath.c_str())) {
 					FileMgr::removeFile(targetPath.c_str());
 				}
@@ -544,7 +541,7 @@ int FileMgr::removeDir(const char *targetDir) {
 				}
 			}
 		}
-		closedir(dir);
+        sw_dir_close(dir);
 		FileMgr::removeFile(targetDir);
 /*
 		int status = FileMgr::removeFile(targetDir);
diff --git a/src/mgr/installmgr.cpp b/src/mgr/installmgr.cpp
index 183989d..88ffb27 100644
--- a/src/mgr/installmgr.cpp
+++ b/src/mgr/installmgr.cpp
@@ -33,12 +33,14 @@ extern "C" {
 #include <utilstr.h>
 
 #include <fcntl.h>
+#include <swopen.h>
 
 #include <swmgr.h>
 #include <swmodule.h>
 #include <swversion.h>
 #include <swlog.h>
 #include <dirent.h>
+#include <swopen.h>
 
 #include <stdio.h>
 #include <map>
@@ -266,29 +268,27 @@ int InstallMgr::removeModule(SWMgr *manager, const char *moduleName) {
 		}
 		else {	//remove all files in DataPath directory
 
-			DIR *dir;
-			struct dirent *ent;
+            SWDir *dir;
+            const char *ent;
 			ConfigEntMap::iterator entry;
 
 			FileMgr::removeDir(modDir.c_str());
 
-			if ((dir = opendir(manager->configPath))) {	// find and remove .conf file
-				rewinddir(dir);
-				while ((ent = readdir(dir))) {
-					if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
-						modFile = manager->configPath;
-						removeTrailingSlash(modFile);
-						modFile += "/";
-						modFile += ent->d_name;
-						SWConfig *config = new SWConfig(modFile.c_str());
-						if (config->Sections.find(modName) != config->Sections.end()) {
-							delete config;
-							FileMgr::removeFile(modFile.c_str());
-						}
-						else	delete config;
-					}
+            if ((dir = sw_dir_open(manager->configPath))) {	// find and remove .conf file
+                sw_dir_rewind(dir);
+                while ((ent = sw_dir_read_name(dir))) {
+                    modFile = manager->configPath;
+                    removeTrailingSlash(modFile);
+                    modFile += "/";
+                    modFile += ent;
+                    SWConfig *config = new SWConfig(modFile.c_str());
+                    if (config->Sections.find(modName) != config->Sections.end()) {
+                        delete config;
+                        FileMgr::removeFile(modFile.c_str());
+                    }
+                    else	delete config;
 				}
-				closedir(dir);
+                sw_dir_close(dir);
 			}
 		}
 		return 0;
@@ -397,8 +397,8 @@ int InstallMgr::installModule(SWMgr *destMgr, const char *fromLocation, const ch
 	SWBuf buffer;
 	bool aborted = false;
 	bool cipher = false;
-	DIR *dir;
-	struct dirent *ent;
+    SWDir *dir;
+    const char *ent;
 	SWBuf modFile;
 
 	SWLog::getSystemLog()->logDebug("***** InstallMgr::installModule\n");
@@ -510,18 +510,18 @@ int InstallMgr::installModule(SWMgr *destMgr, const char *fromLocation, const ch
 		}
 		if (!aborted) {
 			SWBuf confDir = sourceDir + "mods.d/";
-			if ((dir = opendir(confDir.c_str()))) {	// find and copy .conf file
-				rewinddir(dir);
-				while ((ent = readdir(dir))) {
-					if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
+            if ((dir = sw_dir_open(confDir.c_str()))) {	// find and copy .conf file
+                sw_dir_rewind(dir);
+                while ((ent = sw_dir_read_name(dir))) {
+                    if ((strcmp(ent, ".")) && (strcmp(ent, ".."))) {
 						modFile = confDir;
-						modFile += ent->d_name;
+                        modFile += ent;
 						SWConfig *config = new SWConfig(modFile.c_str());
 						if (config->Sections.find(modName) != config->Sections.end()) {
 							SWBuf targetFile = destMgr->configPath; //"./mods.d/";
 							removeTrailingSlash(targetFile);
 							targetFile += "/";
-							targetFile += ent->d_name;
+                            targetFile += ent;
 							FileMgr::copyFile(modFile.c_str(), targetFile.c_str());
 							if (cipher) {
 								if (getCipherCode(modName, config)) {
@@ -538,7 +538,7 @@ int InstallMgr::installModule(SWMgr *destMgr, const char *fromLocation, const ch
 						delete config;
 					}
 				}
-				closedir(dir);
+                sw_dir_close(dir);
 			}
 		}
 		return (aborted) ? -1 : 0;
diff --git a/src/mgr/swmgr.cpp b/src/mgr/swmgr.cpp
index bb065f7..2f2b764 100644
--- a/src/mgr/swmgr.cpp
+++ b/src/mgr/swmgr.cpp
@@ -82,6 +82,7 @@
 #include <swcipher.h>
 #include <swoptfilter.h>
 #include <rtfhtml.h>
+#include <swopen.h>
 
 #include <swlog.h>
 
@@ -270,10 +271,10 @@ void SWMgr::init() {
 SWBuf SWMgr::getHomeDir() {
 
 	// figure out 'home' directory for app data
-	SWBuf homeDir = getenv("HOME");
+    SWBuf homeDir = sw_getenv("HOME");
 	if (!homeDir.length()) {
 		// silly windows
-		homeDir = getenv("APPDATA");
+        homeDir = sw_getenv("APPDATA");
 	}
 	if (homeDir.length()) {
 		if ((homeDir[homeDir.length()-1] != '\\') && (homeDir[homeDir.length()-1] != '/')) {
@@ -456,7 +457,7 @@ void SWMgr::findConfig(char *configType, char **prefixPath, char **configPath, s
 			// check environment variable SWORD_PATH
 			SWLog::getSystemLog()->logDebug("Checking $SWORD_PATH...");
 
-			SWBuf envsworddir = getenv("SWORD_PATH");
+            SWBuf envsworddir = sw_getenv("SWORD_PATH");
 			if (envsworddir.length()) {
 				
 				SWLog::getSystemLog()->logDebug("found (%s).", envsworddir.c_str());
@@ -587,7 +588,7 @@ void SWMgr::findConfig(char *configType, char **prefixPath, char **configPath, s
 
 	SWLog::getSystemLog()->logDebug("Checking $ALLUSERSPROFILE/Application Data/sword/...");
 
-	SWBuf envallusersdir  = getenv("ALLUSERSPROFILE");
+    SWBuf envallusersdir  = sw_getenv("ALLUSERSPROFILE");
 	if (envallusersdir.length()) {
 		SWLog::getSystemLog()->logDebug("found (%s).", envallusersdir.c_str());
 		path = envallusersdir;
@@ -636,7 +637,11 @@ void SWMgr::findConfig(char *configType, char **prefixPath, char **configPath, s
 
 	if (homeDir.length()) {
 		path = homeDir;
+#ifdef WIN32
+        path += "sword/";
+#else
 		path += ".sword/";
+#endif
 		SWLog::getSystemLog()->logDebug("  Checking for %smods.conf...", path.c_str());
 		if (FileMgr::existsFile(path.c_str(), "mods.conf")) {
 			SWLog::getSystemLog()->logDebug("found.");
@@ -673,29 +678,29 @@ void SWMgr::findConfig(char *configType, char **prefixPath, char **configPath, s
 
 void SWMgr::loadConfigDir(const char *ipath)
 {
-	DIR *dir;
-	struct dirent *ent;
+    SWDir *dir;
+    const char *ent;
 	SWBuf newmodfile;
  
-	if ((dir = opendir(ipath))) {
-		rewinddir(dir);
-		while ((ent = readdir(dir))) {
+    if ((dir = sw_dir_open(ipath))) {
+        sw_dir_rewind(dir);
+        while ((ent = sw_dir_read_name(dir))) {
 			//check whether it ends with .conf, if it doesn't skip it!
-			if (!ent->d_name || (strlen(ent->d_name) <= 5) || strncmp(".conf", (ent->d_name + strlen(ent->d_name) - 5), 5 )) {
+            if (ent && (strlen(ent) > 5) && strncmp(".conf", (ent + strlen(ent) - 5), 5 )) {
 				continue;
 			}
 			
 			newmodfile = ipath;
 			if ((ipath[strlen(ipath)-1] != '\\') && (ipath[strlen(ipath)-1] != '/'))
 				newmodfile += "/";
-			newmodfile += ent->d_name;
+            newmodfile += ent;
 			if (config) {
 				SWConfig tmpConfig(newmodfile.c_str());
 				*config += tmpConfig;
 			}
 			else	config = myconfig = new SWConfig(newmodfile.c_str());
 		}
-		closedir(dir);
+        sw_dir_close(dir);
 		
 		if (!config) {	// if no .conf file exist yet, create a default
 			newmodfile = ipath;
@@ -1269,21 +1274,21 @@ void SWMgr::deleteModule(const char *modName) {
 
 void SWMgr::InstallScan(const char *dirname)
 {
-   DIR *dir;
-   struct dirent *ent;
+   SWDir *dir;
+   const char *ent;
    FileDesc *conffd = 0;
    SWBuf newmodfile;
    SWBuf targetName;
  
 	if (FileMgr::existsDir(dirname)) {
-		if ((dir = opendir(dirname))) {
-			rewinddir(dir);
-			while ((ent = readdir(dir))) {
-				if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
+        if ((dir = sw_dir_open(dirname))) {
+            sw_dir_rewind(dir);
+            while ((ent = sw_dir_read_name(dir))) {
+                if ((strcmp(ent, ".")) && (strcmp(ent, ".."))) {
 					newmodfile = dirname;
 					if ((dirname[strlen(dirname)-1] != '\\') && (dirname[strlen(dirname)-1] != '/'))
 						newmodfile += "/";
-					newmodfile += ent->d_name;
+                    newmodfile += ent;
 
 					// mods.d
 					if (configType) {
@@ -1292,7 +1297,7 @@ void SWMgr::InstallScan(const char *dirname)
 						targetName = configPath;
 						if ((configPath[strlen(configPath)-1] != '\\') && (configPath[strlen(configPath)-1] != '/'))
 							targetName += "/";
-						targetName += ent->d_name;
+                        targetName += ent;
 						conffd = FileMgr::getSystemFileMgr()->open(targetName.c_str(), FileMgr::WRONLY|FileMgr::CREAT, FileMgr::IREAD|FileMgr::IWRITE);
 					}
 
@@ -1314,7 +1319,7 @@ void SWMgr::InstallScan(const char *dirname)
 			}
 			if (conffd)
 				FileMgr::getSystemFileMgr()->close(conffd);
-			closedir(dir);
+            sw_dir_close(dir);
 		}
 	}
 }
diff --git a/src/utilfuns/swopen.cpp b/src/utilfuns/swopen.cpp
new file mode 100644
index 0000000..188c312
--- /dev/null
+++ b/src/utilfuns/swopen.cpp
@@ -0,0 +1,445 @@
+/******************************************************************************
+*  swopen.cpp  - code for swopen to wrap file opening
+*
+* $Id$
+*
+* Copyright 2013 CrossWire Bible Society (http://www.crosswire.org)
+*	CrossWire Bible Society
+*	P. O. Box 2528
+*	Tempe, AZ  85280-2528
+*
+* This program is free software; you can redistribute it and/or modify it
+* under the terms of the GNU General Public License as published by the
+* Free Software Foundation version 2.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+* General Public License for more details.
+*
+*/
+
+#include "swopen.h"
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+#ifdef WIN32
+#include <windows.h>
+#include <errno.h>
+#include <wchar.h>
+#include <direct.h>
+#include <io.h>
+#include <sys/utime.h>
+#else
+#include <unistd.h>
+#include <errno.h>
+#endif
+
+#include <dirent.h>
+
+#include <swbuf.h>
+#include <utf8utf16.h>
+#include <utf16utf8.h>
+
+struct _SWDir
+{
+#ifdef WIN32
+  _WDIR *wdirp;
+#else
+  DIR *dirp;
+#endif
+#ifdef WIN32
+  char utf8_buf[FILENAME_MAX*4];
+#endif
+};
+
+wchar_t* _str_to_utf16(const char *str) {
+    sword::UTF8UTF16 *filter = new sword::UTF8UTF16();
+    sword::SWBuf swStr = str;
+    filter->processText(swStr, NULL, NULL);
+    delete filter;
+
+    return (wchar_t*) swStr.c_str();
+}
+
+char* _utf16_to_utf8(const wchar_t *str) {
+    sword::UTF16UTF8 *filter = new sword::UTF16UTF8();
+    sword::SWBuf swStr = (char*)str;
+    filter->processText(swStr, NULL, NULL);
+    delete filter;
+
+    return (char*) swStr.c_str();
+}
+
+/**
+ * @brief cross-platform method for opening a file
+ *
+ * This wraps either a simple call to the clib to open a file or
+ * to the wide-character API on Windows to open a file
+ *
+ * Arguments and return are the same as the standard clib open
+ * arguments.
+ *
+ * @param filename
+ * @param flags
+ * @param mode
+ * @return
+ */
+extern "C" int
+sw_open(const char   *filename,
+       int          flags,
+       int          mode) {
+#ifdef WIN32
+    wchar_t *wfilename = _str_to_utf16 (filename);
+    int retval;
+    int save_errno;
+
+    if (wfilename == NULL)
+      {
+        errno = EINVAL;
+        return -1;
+      }
+
+    retval = _wopen (wfilename, flags, mode);
+    save_errno = errno;
+
+    errno = save_errno;
+    return retval;
+#else
+    int fd;
+    do
+      fd = open (filename, flags, mode);
+    while (fd == -1 && errno == EINTR);
+    return fd;
+#endif
+}
+
+/**
+ * @brief cross-platform method for determining read, write, execute, etc permission
+ *
+ * The arguments and return values are the same as they would be to the access method
+ * in your standard clib.
+ *
+ * This wraps the wide-character API calls on Windows.
+ *
+ * @param filename
+ * @param mode
+ * @return
+ */
+extern "C" int sw_access(const char *filename, int mode) {
+#ifdef WIN32
+    wchar_t *wfilename = _str_to_utf16 (filename);
+    int retval;
+    int save_errno;
+
+    if (wfilename == NULL)
+      {
+        errno = EINVAL;
+        return -1;
+      }
+
+  #ifndef X_OK
+  #define X_OK 1
+  #endif
+
+    retval = _waccess (wfilename, mode & ~X_OK);
+    save_errno = errno;
+
+    errno = save_errno;
+    return retval;
+#else
+    return access (filename, mode);
+#endif
+}
+
+/**
+ * @brief cross-platform method for removing a file or directory
+ *
+ * Arguments are the same as the standard clib arguments to the
+ * access method. Will either call that method in a UTF-8 friendly
+ * environment or call the wide character APIs on Windows.
+ *
+ * @param filename
+ * @return
+ */
+int sw_remove (const char *filename) {
+#ifdef WIN32
+    wchar_t *wfilename = _str_to_utf16 (filename);
+    int retval;
+    int save_errno;
+
+    if (wfilename == NULL)
+      {
+        errno = EINVAL;
+        return -1;
+      }
+
+    retval = _wremove (wfilename);
+    if (retval == -1)
+      retval = _wrmdir (wfilename);
+    save_errno = errno;
+
+    errno = save_errno;
+    return retval;
+#else
+    return remove (filename);
+#endif
+}
+
+/**
+ * @brief cross-platform method for creating a directory
+ *
+ * Accepts the same arguments as the clib mkdir method. On
+ * Windows this will invoke the wide character API.
+ *
+ * @param filename
+ * @param mode
+ * @return
+ */
+extern "C" int sw_mkdir(const char *filename, int mode) {
+#ifdef WIN32
+    wchar_t *wfilename = _str_to_utf16 (filename);
+    int retval;
+    int save_errno;
+
+    if (wfilename == NULL)
+      {
+        errno = EINVAL;
+        return -1;
+      }
+
+    retval = _wmkdir (wfilename);
+    save_errno = errno;
+
+    errno = save_errno;
+    return retval;
+#else
+    return mkdir (filename, mode);
+#endif
+}
+
+/**
+ * @brief cross-platform method for opening a file
+ *
+ * @param filename
+ * @param mode
+ * @return
+ */
+extern "C" FILE* sw_fopen(const char *filename, const char* mode) {
+#ifdef WIN32
+    wchar_t *wfilename = _str_to_utf16 (filename);
+    wchar_t *wmode;
+    FILE *retval;
+    int save_errno;
+
+    if (wfilename == NULL)
+      {
+        errno = EINVAL;
+        return NULL;
+      }
+
+    wmode = _str_to_utf16 (mode);
+
+    if (wmode == NULL)
+      {
+        delete wfilename;
+        errno = EINVAL;
+        return NULL;
+      }
+
+    retval = _wfopen (wfilename, wmode);
+    save_errno = errno;
+
+    errno = save_errno;
+    return retval;
+  #else
+    return fopen (filename, mode);
+  #endif
+}
+
+/**
+ * @brief cross-plaform method for opening a directory
+ */
+extern "C" SWDir *
+sw_dir_open (const char  *path)
+{
+  SWDir *dir;
+#ifdef WIN32
+  wchar_t *wpath;
+#endif
+
+  if(path == NULL) return NULL;
+
+#ifdef WIN32
+  wpath = _str_to_utf16 (path);
+
+  if (wpath == NULL)
+    return NULL;
+
+  dir = new SWDir;
+
+  dir->wdirp = _wopendir (wpath);
+
+  if (dir->wdirp)
+    return dir;
+
+  return NULL;
+#else
+  dir = new SWDir;
+
+  dir->dirp = opendir (path);
+
+  if (dir->dirp)
+    return dir;
+
+  return NULL;
+#endif
+}
+
+void
+sw_dir_rewind(SWDir *dir) {
+    if(dir == NULL) return;
+
+#ifdef WIN32
+  _wrewinddir (dir->wdirp);
+#else
+  rewinddir (dir->dirp);
+#endif
+}
+
+void
+sw_dir_close (SWDir *dir)
+{
+    if(dir == NULL) return;
+
+#ifdef WIN32
+  _wclosedir (dir->wdirp);
+#else
+  closedir (dir->dirp);
+#endif
+  delete dir;
+}
+
+const char *
+sw_dir_read_name (SWDir *dir)
+{
+#ifdef WIN32
+  char *utf8_name;
+  struct _wdirent *wentry;
+#else
+  struct dirent *entry;
+#endif
+
+  if(dir == NULL) return NULL;
+
+#ifdef WIN32
+  while (1)
+    {
+      wentry = _wreaddir (dir->wdirp);
+      while (wentry
+         && (0 == wcscmp (wentry->d_name, L".") ||
+         0 == wcscmp (wentry->d_name, L"..")))
+    wentry = _wreaddir (dir->wdirp);
+
+      if (wentry == NULL)
+    return NULL;
+
+      utf8_name = _utf16_to_utf8 (wentry->d_name);
+
+      if (utf8_name == NULL)
+    continue;		/* Huh, impossible? Skip it anyway */
+
+      strcpy (dir->utf8_buf, utf8_name);
+
+      return dir->utf8_buf;
+    }
+#else
+  entry = readdir (dir->dirp);
+  while (entry
+         && (0 == strcmp (entry->d_name, ".") ||
+             0 == strcmp (entry->d_name, "..")))
+    entry = readdir (dir->dirp);
+
+  if (entry)
+    return entry->d_name;
+  else
+    return NULL;
+#endif
+}
+
+const char* sw_getenv(const char *variable) {
+    if(variable == NULL) return NULL;
+#ifdef WIN32
+    char *value;
+    wchar_t dummy[2], *wname, *wvalue;
+    int len;
+
+    //g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
+
+    /* On Windows NT, it is relatively typical that environment
+     * variables contain references to other environment variables. If
+     * so, use ExpandEnvironmentStrings(). (In an ideal world, such
+     * environment variables would be stored in the Registry as
+     * REG_EXPAND_SZ type values, and would then get automatically
+     * expanded before a program sees them. But there is broken software
+     * that stores environment variables as REG_SZ values even if they
+     * contain references to other environment variables.)
+     */
+
+    wname = _str_to_utf16 (variable);
+
+    len = GetEnvironmentVariableW (wname, dummy, 2);
+
+    if (len == 0)
+      {
+        delete wname;
+        if (GetLastError () == ERROR_ENVVAR_NOT_FOUND)
+          return NULL;
+
+        return "";
+      }
+    else if (len == 1)
+      len = 2;
+
+    wvalue = new wchar_t[len];
+
+    if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
+      {
+        delete wname;
+        delete wvalue;
+        return NULL;
+      }
+
+    if (wcschr (wvalue, L'%') != NULL)
+      {
+        wchar_t *tem = wvalue;
+
+        len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
+
+        if (len > 0)
+          {
+            wvalue = new wchar_t[len];
+
+            if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
+              {
+                delete wvalue;
+                wvalue = tem;
+              }
+            else
+              delete tem;
+          }
+      }
+
+    value = _utf16_to_utf8 (wvalue);
+
+    delete wname;
+    delete wvalue;
+
+    return value;
+#else
+    return getenv(variable);
+#endif
+}
diff --git a/src/utilfuns/zlib/untgz.c b/src/utilfuns/zlib/untgz.c
index f3f3f56..d2bbc7c 100644
--- a/src/utilfuns/zlib/untgz.c
+++ b/src/utilfuns/zlib/untgz.c
@@ -5,6 +5,8 @@
  * adaptation to Unix by Jean-loup Gailly <jloup@gzip.org>
  */
 
+#include <swopen.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -210,7 +212,7 @@ int makedir (char *newdir)
   if (buffer[len-1] == '/') {
     buffer[len-1] = '\0';
   }
-  if (mkdir(buffer, 0775) == 0)
+  if (sw_mkdir(buffer, 0775) == 0)
     {
       free(buffer);
       return 1;
@@ -225,7 +227,7 @@ int makedir (char *newdir)
 	p++;
       hold = *p;
       *p = 0;
-      if ((mkdir(buffer, 0775) == -1) && (errno == ENOENT))
+      if ((sw_mkdir(buffer, 0775) == -1) && (errno == ENOENT))
 	{
 	  fprintf(stderr,"%s: couldn't create directory %s\n",prog,buffer);
 	  free(buffer);
@@ -294,13 +296,13 @@ int untar (gzFile in, const char *dest) {
 	  
 			switch (buffer.header.typeflag) {
 			case DIRTYPE:
-				makedir(fname);
+                sw_mkdir(fname, 0);
 				break;
 			case REGTYPE:
 			case AREGTYPE:
 				remaining = getoct(buffer.header.size,12);
 				if (remaining) {
-					outfile = fopen(fname,"wb");
+                    outfile = sw_fopen(fname,"wb");
 					if (outfile == NULL) {
 						// try creating directory
 						char *p = strrchr(fname, '/');
@@ -308,7 +310,7 @@ int untar (gzFile in, const char *dest) {
 							*p = '\0';
 							makedir(fname);
 							*p = '/';
-							outfile = fopen(fname,"wb");
+                            outfile = sw_fopen(fname,"wb");
 						}
 					}
 /*
@@ -353,7 +355,7 @@ int untar (gzFile in, const char *dest) {
 
 					localt = *localtime(&tartime);
 
-					hFile = CreateFile(fname, GENERIC_READ | GENERIC_WRITE,
+                    hFile = CreateFile(fname, GENERIC_READ | GENERIC_WRITE,
 					0, NULL, OPEN_EXISTING, 0, NULL);
 		  
 					st.wYear = (WORD)localt.tm_year+1900;
