# Creating a Multiarch Wrapper

The Multiarch Wrapper is used to wrap certain binaries that have hardcoded paths to libraries or are architecture specific.

## Installation of The Multiarch Wrapper

Create the source file:

    cat > multiarch_wrapper.c << "EOF"
    #define _GNU_SOURCE

    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>

    #ifndef DEF_SUFFIX
    #  define DEF_SUFFIX "64"
    #endif

    int main(int argc, char **argv)
    {
      char *filename;
      char *suffix;

      if(!(suffix = getenv("USE_ARCH")))
        if(!(suffix = getenv("BUILDENV")))
          suffix = DEF_SUFFIX;

      if (asprintf(&filename, "%s-%s", argv[0], suffix) < 0) {
        perror(argv[0]);
        return -1;
      }

      int status = EXIT_FAILURE;
      pid_t pid = fork();

      if (pid == 0) {
        execvp(filename, argv);
        perror(filename);
      } else if (pid < 0) {
        perror(argv[0]);
      } else {
        if (waitpid(pid, &status, 0) != pid) {
          status = EXIT_FAILURE;
          perror(argv[0]);
        } else {
          status = WEXITSTATUS(status);
        }
      }

      free(filename);

      return status;
    }

    EOF

Compile and Install the Multiarch Wrapper:

    gcc ${BUILD64} multiarch_wrapper.c -o /usr/bin/multiarch_wrapper

This multiarch wrapper is going to be used later on in the book with Perl. It will also be very useful outside of the base CLFS system.

Create a testcase:

    echo 'echo "32bit Version"' > test-32
    echo 'echo "64bit Version"' > test-64
    chmod 755 test-32 test-64
    ln -sv /usr/bin/multiarch_wrapper test

Test the wrapper:

    USE_ARCH=32 ./test
    USE_ARCH=64 ./test

The output of the above command should be:

    32bit Version
    64bit Version

## Contents of The Multiarch Wrapper

Installed programs
- multiarch_wrapper

Short Descriptions

`multiarch_wrapper`

- Will execute a different program based on the `USE_ARCH` variable. The `USE_ARCH` variable will be the suffix of the executed program.