initial commit

This commit is contained in:
Gregory Pakosz 2015-03-05 20:15:02 +01:00
commit 76a7a85f8b
25 changed files with 2149 additions and 0 deletions

30
.gitattributes vendored Normal file
View File

@ -0,0 +1,30 @@
* text=auto
# sources
*.h text diff=cpp
*.c text diff=cpp
*.cpp text diff=cpp
*.rb text diff=ruby
*.html text diff=html
*.m text diff=objc
# shell scripts
*.sh eol=lf
# GNU Makefile
Makefile text eol=lf
# Autotools
*.am text eol=lf
# Android
*.mk text eol=lf
# Xcode files
*.pbxproj text eol=lf merge=union
# Visual Studio files
*.sln text eol=crlf merge=union
*.vcxproj text eol=crlf merge=union
*.vcxproj.filters text eol=crlf merge=union
*.props text eol=crlf

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.DS_Store
*~
*.swp
/bin

25
LICENSE Normal file
View File

@ -0,0 +1,25 @@
--------------------------------------------------------------------------------
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
1. Bla bla bla
2. Montesqieu et camembert, vive la France, zut alors!
--------------------------------------------------------------------------------
WTFPLv2 is very permissive, see http://www.wtfpl.net/faq/
However, if this WTFPLV2 is REALLY a blocker and is the reason you can't use
this project, contact me and I'll dual license it.
--------------------------------------------------------------------------------

117
README.md Normal file
View File

@ -0,0 +1,117 @@
# Where Am I?
A drop-in two files library to locate the current executable and the current
module on the file system.
Supported platforms:
- Windows
- Linux
- Mac
- iOS
- Android
- QNX Neutrino
Just drop `whereami.h` and `whereami.c` into your build and get started. (see
also [customizing compilation])
[customizing compilation]: #customizing-compilation
--------------------------------------------------------------------------------
## Usage
- `wai_getExecutablePath()` returns the path of the enclosing executable
- `wai_getModulePath()` returns the path of the enclosing module
Example usage:
- first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to retrieve
the length of the path
- allocate the destination buffer with `path = (char*)malloc(length + 1);`
- call `wai_getExecutablePath(path, length, &dirname_length)` again to retrieve
the path
- add a terminal `NUL` character with `path[length] = '\0';`
Here is the output of the example:
$ make -C _gnu-make
$ cp ./bin/mac-x86_64/library.dylib /tmp/
$ ./bin/mac-x86_64/executable --load-library=/tmp/library.dylib
executable path: /Users/gregory/Projects/whereami/bin/mac-x86_64/executable
dirname: /Users/gregory/Projects/whereami/bin/mac-x86_64
basename: executable
module path: /Users/gregory/Projects/whereami/bin/mac-x86_64/executable
dirname: /Users/gregory/Projects/whereami/bin/mac-x86_64
basename: executable
library loaded
executable path: /Users/gregory/Projects/whereami/bin/mac-x86_64/executable
dirname: /Users/gregory/Projects/whereami/bin/mac-x86_64
basename: executable
module path: /private/tmp/library.dylib
dirname: /private/tmp
basename: library.dylib
library unloaded
--------------------------------------------------------------------------------
## Customizing compilation
You can customize the library's behavior by defining the following macros:
- `WAI_FUNCSPEC`
- `WAI_PREFIX`
- `WAI_MALLOC`
- `WAI_REALLOC`
- `WAI_FREE`
## Compiling for Windows
There is a Visual Studio 2013 solution in the `_win-vs12/` folder.
## Compiling for Linux or Mac
There is a GNU Make 3.81 `MakeFile` in the `_gnu-make/` folder:
$ make -C _gnu-make/
## Compiling for Mac
See above if you want to compile from command line. Otherwise there is an Xcode
project located in the `_mac-xcode/` folder.
## Compiling for iOS
There is an Xcode project located in the `_ios-xcode/` folder.
If you prefer compiling from command line and deploying to a jailbroken device
through SSH, use:
$ make -C _gnu-make/ binsubdir=ios CC="$(xcrun --sdk iphoneos --find clang) -isysroot $(xcrun --sdk iphoneos --show-sdk-path) -arch armv7 -arch armv7s -arch arm64" postbuild="codesign -s 'iPhone Developer'"
## Compiling for Android
You will have to install the Android NDK, and point the `$NDK_ROOT` environment
variable to the NDK path: e.g. `export NDK_ROOT=/opt/android-ndk` (without a
trailing `/` character).
Next, the easy way is to make a standalone Android toolchain with the following
command:
$ $NDK_ROOT/build/tools/make-standalone-toolchain.sh --system=$(uname -s | tr [A-Z] [a-z])-$(uname -m) --platform=android-3 --arch=arm --install-dir=/tmp/android
Now you can compile the self test and self benchmark programs by running:
$ make -C _gnu-make/ libsuffix=.so binsubdir=android CC=/tmp/android/bin/arm-linux-androideabi-gcc CFLAGS='-march=armv7-a -mfloat-abi=softfp -O2'
--------------------------------------------------------------------------------
If you find this library useful and decide to use it in your own projects please
drop me a line [@gpakosz].
If you use it in a commercial project, consider using [Gittip].
[@gpakosz]: https://twitter.com/gpakosz
[Gittip]: https://www.gittip.com/gpakosz/

65
_gnu-make/Makefile Normal file
View File

@ -0,0 +1,65 @@
.PHONY: build clean
# directories
ifeq ($(realpath .),)
$(error your version of Make doesn't support $$(realpath names...) - please use GNU Make 3.81 or later)
endif
ifeq ($(platform),)
__uname_s := $(shell sh -c 'uname -s 2>/dev/null | tr [A-Z] [a-z] || echo unknown-platform')
__uname_m := $(shell sh -c 'uname -m 2>/dev/null | tr [A-Z] [a-z] || echo unknown-architecture')
ifeq ($(__uname_s),linux)
override platform := linux
override architecture := $(__uname_m)
endif
ifeq ($(__uname_s),darwin)
override platform := mac
override architecture := $(__uname_m)
endif
endif
ifeq ($(architecture),)
override architecture := unknown-architecture
endif
prefix := $(realpath ..)
srcdir := $(realpath ../src)
exampledir := $(realpath ../example)
testdir := $(realpath ../test)
buildir := $(realpath .)/build
binsubdir := $(platform)-$(architecture)
bindir := $(prefix)/bin/$(binsubdir)
CFLAGS := -O2 -g
ifeq ($(platform),linux)
override LDFLAGS := $(LDFLAGS) -ldl
endif
ifeq ($(platform),mac)
libsuffix := .dylib
else
libsuffix := .so
endif
.PHONY: build-executable
build: build-executable
build-executable: $(bindir)/executable
$(bindir)/executable: $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/executable.c
mkdir -p $(@D)
$(CC) -o $@ -I $(srcdir) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -fpic $(filter-out %.h,$^)
$(if $(postbuild),$(postbuild) $@)
.PHONY: build-library
build: build-library
build-library: $(bindir)/library$(libsuffix)
$(bindir)/library$(libsuffix): $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/library.c
mkdir -p $(@D)
$(CC) -shared -o $@ -I $(srcdir) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -fpic $(filter-out %.h,$^)
$(if $(postbuild),$(postbuild) $@)
clean:
rm -rf $(buildir)
rm -rf $(bindir)

2
_ios-xcode/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
xcuserdata/
xcshareddata/

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>net.pempek.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>

View File

@ -0,0 +1,327 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
1A1E0A8D1A4486A300611947 /* whereami.c in Sources */ = {isa = PBXBuildFile; fileRef = 1A1E0A8B1A4486A300611947 /* whereami.c */; };
1A1E0A8F1A4486A900611947 /* executable.c in Sources */ = {isa = PBXBuildFile; fileRef = 1A1E0A8E1A4486A900611947 /* executable.c */; };
1A1E0AAC1A4486FB00611947 /* library.c in Sources */ = {isa = PBXBuildFile; fileRef = 1A1E0AAB1A4486FB00611947 /* library.c */; };
1A1E0AAD1A44880600611947 /* whereami.c in Sources */ = {isa = PBXBuildFile; fileRef = 1A1E0A8B1A4486A300611947 /* whereami.c */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
1A1E0A921A4486E800611947 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "include/$(PRODUCT_NAME)";
dstSubfolderSpec = 16;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
1A1E0A891A44868400611947 /* WhereAmI-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "WhereAmI-Info.plist"; sourceTree = "<group>"; };
1A1E0A8B1A4486A300611947 /* whereami.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = whereami.c; sourceTree = "<group>"; };
1A1E0A8C1A4486A300611947 /* whereami.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = whereami.h; sourceTree = "<group>"; };
1A1E0A8E1A4486A900611947 /* executable.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = executable.c; sourceTree = "<group>"; };
1A1E0A941A4486E800611947 /* library.dylib */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = library.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
1A1E0AAB1A4486FB00611947 /* library.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = library.c; sourceTree = "<group>"; };
1A98F9C917A4018400BF09FF /* executable.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = executable.app; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
1A1E0A911A4486E800611947 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
1A98F9C617A4018400BF09FF /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
1A7335C3182EC2E000691A33 /* src */ = {
isa = PBXGroup;
children = (
1A1E0A8B1A4486A300611947 /* whereami.c */,
1A1E0A8C1A4486A300611947 /* whereami.h */,
);
name = src;
path = ../src;
sourceTree = "<group>";
};
1A7335C4182EC30100691A33 /* example */ = {
isa = PBXGroup;
children = (
1A1E0AAB1A4486FB00611947 /* library.c */,
1A1E0A8E1A4486A900611947 /* executable.c */,
);
name = example;
path = ../example;
sourceTree = "<group>";
};
1A98F9C017A4018400BF09FF = {
isa = PBXGroup;
children = (
1A1E0A891A44868400611947 /* WhereAmI-Info.plist */,
1A7335C3182EC2E000691A33 /* src */,
1A7335C4182EC30100691A33 /* example */,
1A98F9CA17A4018400BF09FF /* Products */,
);
sourceTree = "<group>";
};
1A98F9CA17A4018400BF09FF /* Products */ = {
isa = PBXGroup;
children = (
1A98F9C917A4018400BF09FF /* executable.app */,
1A1E0A941A4486E800611947 /* library.dylib */,
);
name = Products;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
1A1E0A931A4486E800611947 /* library */ = {
isa = PBXNativeTarget;
buildConfigurationList = 1A1E0AA51A4486E900611947 /* Build configuration list for PBXNativeTarget "library" */;
buildPhases = (
1A1E0A901A4486E800611947 /* Sources */,
1A1E0A911A4486E800611947 /* Frameworks */,
1A1E0A921A4486E800611947 /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = library;
productName = library;
productReference = 1A1E0A941A4486E800611947 /* library.dylib */;
productType = "com.apple.product-type.library.static";
};
1A98F9C817A4018400BF09FF /* executable */ = {
isa = PBXNativeTarget;
buildConfigurationList = 1A98F9E617A4018400BF09FF /* Build configuration list for PBXNativeTarget "executable" */;
buildPhases = (
1A98F9C517A4018400BF09FF /* Sources */,
1A98F9C617A4018400BF09FF /* Frameworks */,
1A98F9C717A4018400BF09FF /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = executable;
productName = WhereAmI;
productReference = 1A98F9C917A4018400BF09FF /* executable.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
1A98F9C117A4018400BF09FF /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0460;
ORGANIZATIONNAME = "Gregory Pakosz";
TargetAttributes = {
1A1E0A931A4486E800611947 = {
CreatedOnToolsVersion = 6.1.1;
};
};
};
buildConfigurationList = 1A98F9C417A4018400BF09FF /* Build configuration list for PBXProject "WhereAmI" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 1A98F9C017A4018400BF09FF;
productRefGroup = 1A98F9CA17A4018400BF09FF /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
1A98F9C817A4018400BF09FF /* executable */,
1A1E0A931A4486E800611947 /* library */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
1A98F9C717A4018400BF09FF /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
1A1E0A901A4486E800611947 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1A1E0AAC1A4486FB00611947 /* library.c in Sources */,
1A1E0AAD1A44880600611947 /* whereami.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
1A98F9C517A4018400BF09FF /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1A1E0A8D1A4486A300611947 /* whereami.c in Sources */,
1A1E0A8F1A4486A900611947 /* executable.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
1A1E0AA61A4486E900611947 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
EXECUTABLE_EXTENSION = dylib;
EXECUTABLE_PREFIX = "";
MACH_O_TYPE = mh_dylib;
};
name = Debug;
};
1A1E0AA71A4486E900611947 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
EXECUTABLE_EXTENSION = dylib;
EXECUTABLE_PREFIX = "";
MACH_O_TYPE = mh_dylib;
};
name = Release;
};
1A98F9E417A4018400BF09FF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "c++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
COPY_PHASE_STRIP = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = (
"$(inherited)",
../src,
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
);
INFOPLIST_FILE = "WhereAmI-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
1A98F9E517A4018400BF09FF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "c++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
COPY_PHASE_STRIP = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_OPTIMIZATION_LEVEL = 2;
GCC_PREPROCESSOR_DEFINITIONS = NDEBUG;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = (
"$(inherited)",
../src,
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
);
INFOPLIST_FILE = "WhereAmI-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
name = Release;
};
1A98F9E717A4018400BF09FF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
};
name = Debug;
};
1A98F9E817A4018400BF09FF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
1A1E0AA51A4486E900611947 /* Build configuration list for PBXNativeTarget "library" */ = {
isa = XCConfigurationList;
buildConfigurations = (
1A1E0AA61A4486E900611947 /* Debug */,
1A1E0AA71A4486E900611947 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
1A98F9C417A4018400BF09FF /* Build configuration list for PBXProject "WhereAmI" */ = {
isa = XCConfigurationList;
buildConfigurations = (
1A98F9E417A4018400BF09FF /* Debug */,
1A98F9E517A4018400BF09FF /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
1A98F9E617A4018400BF09FF /* Build configuration list for PBXNativeTarget "executable" */ = {
isa = XCConfigurationList;
buildConfigurations = (
1A98F9E717A4018400BF09FF /* Debug */,
1A98F9E817A4018400BF09FF /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 1A98F9C117A4018400BF09FF /* Project object */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:WhereAmI.xcodeproj">
</FileRef>
</Workspace>

2
_mac-xcode/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
xcuserdata/
xcshareddata/

View File

@ -0,0 +1,333 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
1A1E0A801A4484DC00611947 /* whereami.c in Sources */ = {isa = PBXBuildFile; fileRef = 1A1E0A7E1A4484DC00611947 /* whereami.c */; };
1A1E0A811A4484DC00611947 /* whereami.c in Sources */ = {isa = PBXBuildFile; fileRef = 1A1E0A7E1A4484DC00611947 /* whereami.c */; };
1A1E0A821A4484DC00611947 /* whereami.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A1E0A7F1A4484DC00611947 /* whereami.h */; };
1A1E0A851A4484ED00611947 /* executable.c in Sources */ = {isa = PBXBuildFile; fileRef = 1A1E0A831A4484EA00611947 /* executable.c */; };
1A1E0A861A4484EF00611947 /* library.c in Sources */ = {isa = PBXBuildFile; fileRef = 1A1E0A841A4484EA00611947 /* library.c */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
1A98F9FF17A4249200BF09FF /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
1A1E0A7A1A44841F00611947 /* library.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = library.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
1A1E0A7E1A4484DC00611947 /* whereami.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = whereami.c; sourceTree = "<group>"; };
1A1E0A7F1A4484DC00611947 /* whereami.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = whereami.h; sourceTree = "<group>"; };
1A1E0A831A4484EA00611947 /* executable.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = executable.c; sourceTree = "<group>"; };
1A1E0A841A4484EA00611947 /* library.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = library.c; sourceTree = "<group>"; };
1A98FA0117A4249200BF09FF /* executable */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = executable; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
1A1E0A771A44841F00611947 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
1A98F9FE17A4249200BF09FF /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
1A42F178182EB9EF00F3880C /* src */ = {
isa = PBXGroup;
children = (
1A1E0A7E1A4484DC00611947 /* whereami.c */,
1A1E0A7F1A4484DC00611947 /* whereami.h */,
);
name = src;
path = ../src;
sourceTree = "<group>";
};
1A42F17A182EB9FE00F3880C /* example */ = {
isa = PBXGroup;
children = (
1A1E0A831A4484EA00611947 /* executable.c */,
1A1E0A841A4484EA00611947 /* library.c */,
);
name = example;
path = ../example;
sourceTree = "<group>";
};
1A98F9F817A4249200BF09FF = {
isa = PBXGroup;
children = (
1A42F178182EB9EF00F3880C /* src */,
1A42F17A182EB9FE00F3880C /* example */,
1A98FA0217A4249200BF09FF /* Products */,
);
sourceTree = "<group>";
};
1A98FA0217A4249200BF09FF /* Products */ = {
isa = PBXGroup;
children = (
1A98FA0117A4249200BF09FF /* executable */,
1A1E0A7A1A44841F00611947 /* library.dylib */,
);
name = Products;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXHeadersBuildPhase section */
1A1E0A781A44841F00611947 /* Headers */ = {
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
1A1E0A821A4484DC00611947 /* whereami.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXHeadersBuildPhase section */
/* Begin PBXNativeTarget section */
1A1E0A791A44841F00611947 /* library */ = {
isa = PBXNativeTarget;
buildConfigurationList = 1A1E0A7B1A44841F00611947 /* Build configuration list for PBXNativeTarget "library" */;
buildPhases = (
1A1E0A761A44841F00611947 /* Sources */,
1A1E0A771A44841F00611947 /* Frameworks */,
1A1E0A781A44841F00611947 /* Headers */,
);
buildRules = (
);
dependencies = (
);
name = library;
productName = library;
productReference = 1A1E0A7A1A44841F00611947 /* library.dylib */;
productType = "com.apple.product-type.library.dynamic";
};
1A98FA0017A4249200BF09FF /* executable */ = {
isa = PBXNativeTarget;
buildConfigurationList = 1A98FA0A17A4249200BF09FF /* Build configuration list for PBXNativeTarget "executable" */;
buildPhases = (
1A98F9FD17A4249200BF09FF /* Sources */,
1A98F9FE17A4249200BF09FF /* Frameworks */,
1A98F9FF17A4249200BF09FF /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = executable;
productName = WhereAmI;
productReference = 1A98FA0117A4249200BF09FF /* executable */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
1A98F9F917A4249200BF09FF /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0610;
ORGANIZATIONNAME = "Gregory Pakosz";
TargetAttributes = {
1A1E0A791A44841F00611947 = {
CreatedOnToolsVersion = 6.1.1;
};
};
};
buildConfigurationList = 1A98F9FC17A4249200BF09FF /* Build configuration list for PBXProject "WhereAmI" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 1A98F9F817A4249200BF09FF;
productRefGroup = 1A98FA0217A4249200BF09FF /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
1A98FA0017A4249200BF09FF /* executable */,
1A1E0A791A44841F00611947 /* library */,
);
};
/* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */
1A1E0A761A44841F00611947 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1A1E0A811A4484DC00611947 /* whereami.c in Sources */,
1A1E0A861A4484EF00611947 /* library.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
1A98F9FD17A4249200BF09FF /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1A1E0A801A4484DC00611947 /* whereami.c in Sources */,
1A1E0A851A4484ED00611947 /* executable.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
1A1E0A7C1A44841F00611947 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
COMBINE_HIDPI_IMAGES = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
1A1E0A7D1A44841F00611947 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
COMBINE_HIDPI_IMAGES = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
};
1A98FA0817A4249200BF09FF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "compiler-default";
CLANG_CXX_LIBRARY = "compiler-default";
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = (
"$(inherited)",
../src,
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
);
MACOSX_DEPLOYMENT_TARGET = 10.8;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
};
name = Debug;
};
1A98FA0917A4249200BF09FF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "compiler-default";
CLANG_CXX_LIBRARY = "compiler-default";
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_OPTIMIZATION_LEVEL = 2;
GCC_PREPROCESSOR_DEFINITIONS = NDEBUG;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = (
"$(inherited)",
../src,
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
);
MACOSX_DEPLOYMENT_TARGET = 10.8;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
};
name = Release;
};
1A98FA0B17A4249200BF09FF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
};
name = Debug;
};
1A98FA0C17A4249200BF09FF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
1A1E0A7B1A44841F00611947 /* Build configuration list for PBXNativeTarget "library" */ = {
isa = XCConfigurationList;
buildConfigurations = (
1A1E0A7C1A44841F00611947 /* Debug */,
1A1E0A7D1A44841F00611947 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
1A98F9FC17A4249200BF09FF /* Build configuration list for PBXProject "WhereAmI" */ = {
isa = XCConfigurationList;
buildConfigurations = (
1A98FA0817A4249200BF09FF /* Debug */,
1A98FA0917A4249200BF09FF /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
1A98FA0A17A4249200BF09FF /* Build configuration list for PBXNativeTarget "executable" */ = {
isa = XCConfigurationList;
buildConfigurations = (
1A98FA0B17A4249200BF09FF /* Debug */,
1A98FA0C17A4249200BF09FF /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 1A98F9F917A4249200BF09FF /* Project object */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:WhereAmI.xcodeproj">
</FileRef>
</Workspace>

8
_win-vs12/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
/build
/ipch
*.suo
*.sdf
*.opensdf
*.user
*.sln.docstates

40
_win-vs12/Common.props Normal file
View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="UserMacros">
<BIN_DIR>..\bin\Windows$(PLATFORM_SUFFIX)-$(ARCH)$(CONF_SUFFIX)</BIN_DIR>
<LIB_DIR>..\lib\Windows$(PLATFORM_SUFFIX)-$(ARCH)-vs11$(CONF_SUFFIX)</LIB_DIR>
<BUILD_DIR>build\$(ProjectName)-win$(PLATFORM_SUFFIX)-$(ARCH)-vs11$(CONF_SUFFIX)</BUILD_DIR>
</PropertyGroup>
<PropertyGroup>
<IntDir>$(BUILD_DIR)\</IntDir>
<OutDir>$(BIN_DIR)\</OutDir>
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<AdditionalOptions>/we4013 /wd4820 /we4289 /wd4342 /wd4347 /wd4514 /we4545 /we4546 /we4547 /we4548 /we4549 /we4619 /we4623 /we4625 /we4626 /wd4710 /we4836 /we4905 /we4906 /we4928 /we4946 /wd4986 /wd4711 /wd4350 /wd4255 /wd4668</AdditionalOptions>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<FunctionLevelLinking>true</FunctionLevelLinking>
<WarningLevel>EnableAllWarnings</WarningLevel>
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<AdditionalOptions>/time %(AdditionalOptions)</AdditionalOptions>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<BuildMacro Include="BIN_DIR">
<Value>$(BIN_DIR)</Value>
</BuildMacro>
<BuildMacro Include="LIB_DIR">
<Value>$(LIB_DIR)</Value>
</BuildMacro>
<BuildMacro Include="BUILD_DIR">
<Value>$(BUILD_DIR)</Value>
</BuildMacro>
</ItemGroup>
</Project>

21
_win-vs12/Debug.props Normal file
View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="UserMacros">
<CONF_SUFFIX>-Debug</CONF_SUFFIX>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<Optimization>Disabled</Optimization>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<IntrinsicFunctions>true</IntrinsicFunctions>
<InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<BuildMacro Include="CONF_SUFFIX">
<Value>$(CONF_SUFFIX)</Value>
</BuildMacro>
</ItemGroup>
</Project>

View File

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\example\executable.c" />
<ClCompile Include="..\src\whereami.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\whereami.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<Keyword>Win32Proj</Keyword>
<RootNamespace>WhereAmI</RootNamespace>
<ProjectGuid>{4F614870-9A09-4661-9963-7DF73803240B}</ProjectGuid>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120_xp</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120_xp</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="x86.props" />
<Import Project="Debug.props" />
<Import Project="Common.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="x64.props" />
<Import Project="Debug.props" />
<Import Project="Common.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="x86.props" />
<Import Project="Release.props" />
<Import Project="Common.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="x64.props" />
<Import Project="Release.props" />
<Import Project="Common.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<AdditionalIncludeDirectories>../src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<AdditionalIncludeDirectories>../src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<AdditionalOptions>/we4013 /wd4820 /we4289 /wd4342 /wd4347 /wd4514 /we4545 /we4546 /we4547 /we4548 /we4549 /we4619 /we4623 /we4625 /we4626 /wd4710 /we4836 /we4905 /we4906 /we4928 /we4946 /wd4986 /wd4711 /wd4350 /Qvec-report:2</AdditionalOptions>
<AdditionalIncludeDirectories>../src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/we4013 /wd4820 /we4289 /wd4342 /wd4347 /wd4514 /we4545 /we4546 /we4547 /we4548 /we4549 /we4619 /we4623 /we4625 /we4626 /wd4710 /we4836 /we4905 /we4906 /we4928 /we4946 /wd4986 /wd4711 /wd4350 /wd4255 /wd4668 /Qvec-report:2</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<AdditionalIncludeDirectories>../src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/we4013 /wd4820 /we4289 /wd4342 /wd4347 /wd4514 /we4545 /we4546 /we4547 /we4548 /we4549 /we4619 /we4623 /we4625 /we4626 /wd4710 /we4836 /we4905 /we4906 /we4928 /we4946 /wd4986 /wd4711 /wd4350 /wd4255 /wd4668 /Qvec-report:2</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

113
_win-vs12/Library.vcxproj Normal file
View File

@ -0,0 +1,113 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\example\library.c" />
<ClCompile Include="..\src\whereami.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\whereami.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<Keyword>Win32Proj</Keyword>
<RootNamespace>WhereAmI</RootNamespace>
<ProjectGuid>{D53572AD-B43A-4507-9BB2-F330F942912B}</ProjectGuid>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v120_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v120_xp</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="x86.props" />
<Import Project="Debug.props" />
<Import Project="Common.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="x64.props" />
<Import Project="Debug.props" />
<Import Project="Common.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="x86.props" />
<Import Project="Release.props" />
<Import Project="Common.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="x64.props" />
<Import Project="Release.props" />
<Import Project="Common.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<AdditionalIncludeDirectories>../src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<AdditionalIncludeDirectories>../src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<AdditionalIncludeDirectories>../src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/we4013 /wd4820 /we4289 /wd4342 /wd4347 /wd4514 /we4545 /we4546 /we4547 /we4548 /we4549 /we4619 /we4623 /we4625 /we4626 /wd4710 /we4836 /we4905 /we4906 /we4928 /we4946 /wd4986 /wd4711 /wd4350 /wd4255 /wd4668 /Qvec-report:2</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<AdditionalIncludeDirectories>../src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/we4013 /wd4820 /we4289 /wd4342 /wd4347 /wd4514 /we4545 /we4546 /we4547 /we4548 /we4549 /we4619 /we4623 /we4625 /we4626 /wd4710 /we4836 /we4905 /we4906 /we4928 /we4946 /wd4986 /wd4711 /wd4350 /wd4255 /wd4668 /Qvec-report:2</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

39
_win-vs12/Release.props Normal file
View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="UserMacros">
<CONF_SUFFIX />
</PropertyGroup>
<PropertyGroup>
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<Optimization>MaxSpeed</Optimization>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<IntrinsicFunctions>true</IntrinsicFunctions>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<OmitFramePointers>true</OmitFramePointers>
<EnableFiberSafeOptimizations>true</EnableFiberSafeOptimizations>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<StringPooling>true</StringPooling>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<BufferSecurityCheck>false</BufferSecurityCheck>
<AdditionalOptions>/d2Zi+ %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
</Link>
<Lib>
<LinkTimeCodeGeneration>true</LinkTimeCodeGeneration>
</Lib>
</ItemDefinitionGroup>
<ItemGroup>
<BuildMacro Include="CONF_SUFFIX">
<Value>$(CONF_SUFFIX)</Value>
</BuildMacro>
</ItemGroup>
</Project>

38
_win-vs12/WhereAmI.sln Normal file
View File

@ -0,0 +1,38 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Executable", "Executable.vcxproj", "{4F614870-9A09-4661-9963-7DF73803240B}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Library", "Library.vcxproj", "{D53572AD-B43A-4507-9BB2-F330F942912B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4F614870-9A09-4661-9963-7DF73803240B}.Debug|Win32.ActiveCfg = Debug|Win32
{4F614870-9A09-4661-9963-7DF73803240B}.Debug|Win32.Build.0 = Debug|Win32
{4F614870-9A09-4661-9963-7DF73803240B}.Debug|x64.ActiveCfg = Debug|x64
{4F614870-9A09-4661-9963-7DF73803240B}.Debug|x64.Build.0 = Debug|x64
{4F614870-9A09-4661-9963-7DF73803240B}.Release|Win32.ActiveCfg = Release|Win32
{4F614870-9A09-4661-9963-7DF73803240B}.Release|Win32.Build.0 = Release|Win32
{4F614870-9A09-4661-9963-7DF73803240B}.Release|x64.ActiveCfg = Release|x64
{4F614870-9A09-4661-9963-7DF73803240B}.Release|x64.Build.0 = Release|x64
{D53572AD-B43A-4507-9BB2-F330F942912B}.Debug|Win32.ActiveCfg = Debug|Win32
{D53572AD-B43A-4507-9BB2-F330F942912B}.Debug|Win32.Build.0 = Debug|Win32
{D53572AD-B43A-4507-9BB2-F330F942912B}.Debug|x64.ActiveCfg = Debug|x64
{D53572AD-B43A-4507-9BB2-F330F942912B}.Debug|x64.Build.0 = Debug|x64
{D53572AD-B43A-4507-9BB2-F330F942912B}.Release|Win32.ActiveCfg = Release|Win32
{D53572AD-B43A-4507-9BB2-F330F942912B}.Release|Win32.Build.0 = Release|Win32
{D53572AD-B43A-4507-9BB2-F330F942912B}.Release|x64.ActiveCfg = Release|x64
{D53572AD-B43A-4507-9BB2-F330F942912B}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

16
_win-vs12/x64.props Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="UserMacros">
<ARCH>x64</ARCH>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<TargetMachine>MachineX64</TargetMachine>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<BuildMacro Include="ARCH">
<Value>$(ARCH)</Value>
</BuildMacro>
</ItemGroup>
</Project>

19
_win-vs12/x86.props Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="UserMacros">
<ARCH>x86</ARCH>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<TargetMachine>MachineX86</TargetMachine>
</Link>
<ClCompile>
<EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<BuildMacro Include="ARCH">
<Value>$(ARCH)</Value>
</BuildMacro>
</ItemGroup>
</Project>

123
example/executable.c Normal file
View File

@ -0,0 +1,123 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <whereami.h>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#if defined(_MSC_VER)
#pragma warning(push, 3)
#endif
#include <windows.h>
#define RTLD_LAZY 1
#define RTLD_NOW 2
#define RTLD_GLOBAL 4
#define RTLD_LOCAL 8
static void* dlopen(const char* fileName, int mode)
{
wchar_t buffer[MAX_PATH];
if (MultiByteToWideChar(CP_UTF8, 0, fileName, -1, buffer, sizeof(buffer) / sizeof(*buffer)))
{
wchar_t buffer_[MAX_PATH];
GetFullPathNameW(buffer, sizeof(buffer_) / sizeof(*buffer_), buffer_, NULL);
return (void*)LoadLibraryW(buffer_);
}
return NULL;
}
static int dlclose(void* handle)
{
return FreeLibrary((HMODULE)handle) ? 0 : -1;
}
static const char* dlerror(void)
{
DWORD error;
error = GetLastError();
if (error)
{
static char message[1024];
DWORD length;
length = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), message, sizeof(message), NULL);
return message;
}
return "no error";
}
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#else
#include <dlfcn.h>
#endif
int main(int argc, char** argv)
{
char* path = NULL;
int length, dirname_length;
int i;
length = wai_getExecutablePath(NULL, 0, &dirname_length);
if (length > 0)
{
path = malloc(length + 1);
wai_getExecutablePath(path, length, &dirname_length);
path[length] = '\0';
printf("executable path: %s\n", path);
path[dirname_length] = '\0';
printf(" dirname: %s\n", path);
printf(" basename: %s\n", path + dirname_length + 1);
free(path);
}
length = wai_getModulePath(NULL, 0, &dirname_length);
if (length > 0)
{
path = malloc(length + 1);
wai_getModulePath(path, length, &dirname_length);
path[length] = '\0';
printf("module path: %s\n", path);
path[dirname_length] = '\0';
printf(" dirname: %s\n", path);
printf(" basename: %s\n", path + dirname_length + 1);
free(path);
}
for (i = 1; i < argc; ++i)
{
if (strncmp(argv[i], "--load-library=", 15) == 0)
{
char* name = argv[i] + 15;
void* handle;
printf("\n");
handle = dlopen(name, RTLD_NOW);
if (!handle)
printf("failed to load library: %s\n", dlerror());
if (handle)
dlclose(handle);
}
}
return 0;
}

83
example/library.c Normal file
View File

@ -0,0 +1,83 @@
#include <stdlib.h>
#include <stdio.h>
#include <whereami.h>
#if defined(__GNUC__)
__attribute__((constructor))
#endif
static void load()
{
char* path = NULL;
int length, dirname_length;
printf("library loaded\n");
length = wai_getExecutablePath(NULL, 0, &dirname_length);
if (length > 0)
{
path = malloc(length + 1);
wai_getExecutablePath(path, length, &dirname_length);
path[length] = '\0';
printf("executable path: %s\n", path);
path[dirname_length] = '\0';
printf(" dirname: %s\n", path);
printf(" basename: %s\n", path + dirname_length + 1);
free(path);
}
length = wai_getModulePath(NULL, 0, &dirname_length);
if (length > 0)
{
path = malloc(length + 1);
wai_getModulePath(path, length, &dirname_length);
path[length] = '\0';
printf("module path: %s\n", path);
path[dirname_length] = '\0';
printf(" dirname: %s\n", path);
printf(" basename: %s\n", path + dirname_length + 1);
free(path);
}
}
#if defined(__GNUC__)
__attribute__((destructor))
#endif
static void unload()
{
printf("library unloaded\n");
}
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#if defined(_MSC_VER)
#pragma warning(push, 3)
#include <windows.h>
#endif
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
load();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
unload();
break;
}
return TRUE;
}
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif

506
src/whereami.c Normal file
View File

@ -0,0 +1,506 @@
// (‑●‑●)> released under the WTFPL v2 license, by Gregory Pakosz (@gpakosz)
// in case you want to #include "whereami.c" in a larger compilation unit
#if !defined(WHEREAMI_H)
#include <whereami.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC)
#include <stdlib.h>
#endif
#if !defined(WAI_MALLOC)
#define WAI_MALLOC(size) malloc(size)
#endif
#if !defined(WAI_FREE)
#define WAI_FREE(p) free(p)
#endif
#if !defined(WAI_REALLOC)
#define WAI_REALLOC(p, size) realloc(p, size)
#endif
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#if defined(_MSC_VER)
#pragma warning(push, 3)
#endif
#include <windows.h>
#include <intrin.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#ifndef WAI_NOINLINE
#ifdef _MSC_VER
#define WAI_NOINLINE __declspec(noinline)
#endif
#endif
static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, int* dirname_length)
{
wchar_t buffer1[MAX_PATH];
wchar_t buffer2[MAX_PATH];
wchar_t* path = NULL;
int length = -1;
for (;;)
{
DWORD size;
int length_;
size = GetModuleFileNameW(module, buffer1, sizeof(buffer1) / sizeof(buffer1[0]));
if (size == 0)
break;
else if (size == (DWORD)(sizeof(buffer1) / sizeof(buffer1[0])))
{
DWORD size_ = size;
do
{
size_ *= 2;
path = (wchar_t*)WAI_REALLOC(path, sizeof(wchar_t) * size_);
size = GetModuleFileNameW(NULL, path, size_);
} while (size == size_);
}
else
path = buffer1;
_wfullpath(buffer2, path, MAX_PATH);
length_ = WideCharToMultiByte(CP_UTF8, 0, buffer2, -1, out, (DWORD)capacity, NULL, NULL);
if (length_ == 0)
length_ = WideCharToMultiByte(CP_UTF8, 0, buffer2, -1, NULL, (DWORD)0, NULL, NULL);
if (length_ == 0)
break;
if (length_ <= capacity && dirname_length)
{
int i;
for (i = length_ - 1; i >= 0; --i)
{
if (out[i] == '\\')
{
*dirname_length = i;
break;
}
}
}
length = length_;
break;
}
if (path != buffer1)
WAI_FREE(path);
return length;
}
WAI_NOINLINE
WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
{
return WAI_PREFIX(getModulePath_)(NULL, out, capacity, dirname_length);
}
WAI_NOINLINE
WAI_FUNCSPEC
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
{
HMODULE module;
int length = -1;
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4054)
#endif
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)_ReturnAddress(), &module))
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
{
length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length);
}
return length;
}
#elif defined(__linux__)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#if !defined(WAI_PROC_SELF_EXE)
#define WAI_PROC_SELF_EXE "/proc/self/exe"
#endif
#ifndef WAI_NOINLINE
#ifdef __GNUC__
#define WAI_NOINLINE __attribute__((noinline))
#endif
#endif
WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
{
char buffer[PATH_MAX];
char* resolved = NULL;
int length = -1;
for (;;)
{
resolved = realpath(WAI_PROC_SELF_EXE, buffer);
if (!resolved)
break;
length = (int)strlen(resolved);
if (length <= capacity)
{
memcpy(out, resolved, length);
if (dirname_length)
{
int i;
for (i = length - 1; i >= 0; --i)
{
if (out[i] == '/')
{
*dirname_length = i;
break;
}
}
}
}
break;
}
return length;
}
#if !defined(WAI_PROC_SELF_MAPS_RETRY)
#define WAI_PROC_SELF_MAPS_RETRY 5
#endif
#if !defined(WAI_PROC_SELF_MAPS)
#define WAI_PROC_SELF_MAPS "/proc/self/maps"
#endif
WAI_NOINLINE
WAI_FUNCSPEC
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
{
int length = -1;
FILE* maps = NULL;
int i;
for (i = 0; i < WAI_PROC_SELF_MAPS_RETRY; ++i)
{
maps = fopen(WAI_PROC_SELF_MAPS, "r");
if (!maps)
break;
for (;;)
{
char buffer[PATH_MAX < 1024 ? 1024 : PATH_MAX];
uint64_t low, high;
char perms[5];
uint64_t offset;
uint32_t major, minor;
char path[PATH_MAX];
uint32_t inode;
int count;
if (!fgets(buffer, sizeof(buffer), maps))
break;
if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8)
{
uint64_t addr = (uint64_t)(uintptr_t) __builtin_extract_return_addr(__builtin_return_address(0));
if (low <= addr && addr <= high)
{
char* resolved;
resolved = realpath(path, buffer);
if (!resolved)
break;
length = (int)strlen(resolved);
if (length <= capacity)
{
memcpy(out, resolved, length);
if (dirname_length)
{
int i;
for (i = length - 1; i >= 0; --i)
{
if (out[i] == '/')
{
*dirname_length = i;
break;
}
}
}
}
break;
}
}
}
fclose(maps);
if (length != -1)
break;
}
return length;
}
#elif defined(__APPLE__)
#define _DARWIN_BETTER_REALPATH
#include <mach-o/dyld.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#ifndef WAI_NOINLINE
#ifdef __GNUC__
#define WAI_NOINLINE __attribute__((noinline))
#endif
#endif
WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
{
char buffer1[PATH_MAX];
char buffer2[PATH_MAX];
char* path = buffer1;
char* resolved = NULL;
int length = -1;
for (;;)
{
uint32_t size = (uint32_t)sizeof(buffer1);
if (_NSGetExecutablePath(path, &size) == -1)
{
path = (char*)WAI_MALLOC(size);
if (!_NSGetExecutablePath(path, &size))
break;
}
resolved = realpath(path, buffer2);
if (!resolved)
break;
length = (int)strlen(resolved);
if (length <= capacity)
{
memcpy(out, resolved, length);
if (dirname_length)
{
int i;
for (i = length - 1; i >= 0; --i)
{
if (out[i] == '/')
{
*dirname_length = i;
break;
}
}
}
}
break;
}
if (path != buffer1)
WAI_FREE(path);
return length;
}
WAI_NOINLINE
WAI_FUNCSPEC
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
{
char buffer[PATH_MAX];
char* resolved = NULL;
int length = -1;
for(;;)
{
Dl_info info;
if (dladdr(__builtin_extract_return_addr(__builtin_return_address(0)), &info))
{
resolved = realpath(info.dli_fname, buffer);
if (!resolved)
break;
length = (int)strlen(resolved);
if (length <= capacity)
{
memcpy(out, resolved, length);
if (dirname_length)
{
int i;
for (i = length - 1; i >= 0; --i)
{
if (out[i] == '/')
{
*dirname_length = i;
break;
}
}
}
}
}
break;
}
return length;
}
#elif defined(__QNXNTO__)
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#ifndef WAI_NOINLINE
#ifdef __GNUC__
#define WAI_NOINLINE __attribute__((noinline))
#endif
#endif
#if !defined(WAI_PROC_SELF_EXE)
#define WAI_PROC_SELF_EXE "/proc/self/exefile"
#endif
WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
{
char buffer1[PATH_MAX];
char buffer2[PATH_MAX];
char* resolved = NULL;
FILE* self_exe = NULL;
int length = -1;
for (;;)
{
self_exe = fopen(WAI_PROC_SELF_EXE, "r");
if (!self_exe)
break;
if (!fgets(buffer1, sizeof(buffer1), self_exe))
break;
resolved = realpath(buffer1, buffer2);
if (!resolved)
break;
length = (int)strlen(resolved);
if (length <= capacity)
{
memcpy(out, resolved, length);
if (dirname_length)
{
int i;
for (i = length - 1; i >= 0; --i)
{
if (out[i] == '/')
{
*dirname_length = i;
break;
}
}
}
}
break;
}
fclose(self_exe);
return length;
}
WAI_FUNCSPEC
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
{
char buffer[PATH_MAX];
char* resolved = NULL;
int length = -1;
for(;;)
{
Dl_info info;
if (dladdr(__builtin_extract_return_addr(__builtin_return_address(0)), &info))
{
resolved = realpath(info.dli_fname, buffer);
if (!resolved)
break;
length = (int)strlen(resolved);
if (length <= capacity)
{
memcpy(out, resolved, length);
if (dirname_length)
{
int i;
for (i = length - 1; i >= 0; --i)
{
if (out[i] == '/')
{
*dirname_length = i;
break;
}
}
}
}
}
break;
}
return length;
}
#endif
#ifdef __cplusplus
}
#endif

64
src/whereami.h Normal file
View File

@ -0,0 +1,64 @@
// (‑●‑●)> released under the WTFPL v2 license, by Gregory Pakosz (@gpakosz)
#ifndef WHEREAMI_H
#define WHEREAMI_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef WAI_FUNCSPEC
#define WAI_FUNCSPEC
#endif
#ifndef WAI_PREFIX
#define WAI_PREFIX(function) wai_##function
#endif
/**
* Returns the path to the current executable.
*
* Usage:
* - first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to
* retrieve the length of the path
* - allocate the destination buffer with `path = (char*)malloc(length + 1);`
* - call `wai_getExecutablePath(path, length, NULL)` again to retrieve the
* path
* - add a terminal NUL character with `path[length] = '\0';`
*
* @param out destination buffer, optional
* @param capacity destination buffer capacity
* @param dirname_length optional recipient for the length of the dirname part
* of the path.
*
* @return the length of the executable path on success (without a terminal NUL
* character), otherwise `-1`
*/
WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length);
/**
* Returns the path to the current module
*
* Usage:
* - first call `int length = wai_getModulePath(NULL, 0, NULL);` to retrieve
* the length of the path
* - allocate the destination buffer with `path = (char*)malloc(length + 1);`
* - call `wai_getModulePath(path, length, NULL)` again to retrieve the path
* - add a terminal NUL character with `path[length] = '\0';`
*
* @param out destination buffer, optional
* @param capacity destination buffer capacity
* @param dirname_length optional recipient for the length of the dirname part
* of the path.
*
* @return the length of the module path on success (without a terminal NUL
* character), otherwise `-1`
*/
WAI_FUNCSPEC
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length);
#ifdef __cplusplus
}
#endif
#endif // #ifndef WHEREAMI_H