Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- compile:
- WINE_SUFFIX=-unstable
- WINE_PROXY=wine${WINE_SUFFIX}
- gcc -std=c11 -O3 -g0 wine-proxy.c -ldpkg -o ${WINE_PROXY} -DSUFFIX=${WINE_SUFFIX}
- sudo install ${WINE_PROXY} /usr/bin
- */
- #define _GNU_SOURCE
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #define LIBDPKG_VOLATILE_API
- #include <dpkg/arch.h> // libdpkg-dev
- // --------------------------
- // configuration
- // --------------------------
- #ifndef SUFFIX
- // #define WINE_SUFFIX "-unstable"
- #define WINE_SUFFIX ""
- #else
- /*
- two-pass stringification according to
- http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
- */
- #define S0(a) #a
- #define S1(a) S0(a)
- #define WINE_SUFFIX S1(SUFFIX)
- #endif
- #ifndef WINE_FULLNAME
- #define WINE_FULLNAME "wine"WINE_SUFFIX
- #endif
- #ifndef WINE_BINDIR
- #define WINE_BINDIR "/usr/lib/"WINE_FULLNAME
- #endif
- #ifndef WINE32
- #define WINE32 WINE_BINDIR"/wine"
- #endif
- #ifndef WINE64
- #define WINE64 WINE_BINDIR"/wine64"
- #endif
- // --------------------------
- #define TRUE 1
- #define FALSE 0
- #define TEST(a,b) (((a)&(b))==(b))
- void echo(const char * string) {
- printf("%s\n", string);
- }
- void env_default(const char * name, const char * value) {
- char * curr = getenv(name);
- if ((curr != NULL) && (strlen(curr) > 0)) {
- return;
- }
- setenv(name, value, TRUE);
- }
- int is_binary(char * path) {
- int file;
- struct stat file_stat;
- file = open(path, O_NOATIME | O_NOCTTY | O_PATH, O_ACCMODE);
- if (file == -1) { return FALSE; }
- while (TRUE) {
- if (0 != fstat(file, &file_stat)) { break; }
- if (!S_ISREG(file_stat.st_mode)) { break; }
- if (!TEST(file_stat.st_mode, S_IROTH | S_IXOTH)) { break; }
- close(file);
- return TRUE;
- }
- return FALSE;
- }
- // never returns if succeed
- int try_exec(char * binary_path, char ** argv, char ** env) {
- int file = open(binary_path, O_NOATIME | O_NOCTTY | O_CLOEXEC, O_RDONLY);
- if (file == -1) { return 1; }
- int result = fexecve(file, argv, env);
- close(file);
- return result;
- }
- // --------------------------
- char * wine32 = WINE32;
- char * wine64 = WINE64;
- extern char ** environ;
- // --------------------------
- int main(int argc, char ** argv) {
- char * wine = NULL;
- if (is_binary(wine32)) {
- wine = wine32;
- } else if (is_binary(wine64)) {
- wine = wine64;
- struct dpkg_arch * a_amd64 = dpkg_arch_find("amd64");
- struct dpkg_arch * a_i386 = dpkg_arch_find("i386");
- if (
- (a_amd64->type == DPKG_ARCH_NATIVE)
- &&
- (a_i386->type != DPKG_ARCH_FOREIGN)
- ) {
- echo("It looks like multiarch needs to be enabled!");
- echo("As root, please execute following:");
- echo(" dpkg --add-architecture i386 && apt-get install wine32"WINE_SUFFIX);
- }
- } else {
- echo("error: unable to find wine executable. this shouldn't happen.");
- return 1;
- }
- env_default("WINELOADER", wine);
- env_default("WINEDEBUG", "-all");
- argv[0] = wine;
- // return try_exec(wine, argv, environ);
- return execve(wine, argv, environ);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement