Skip to content

Commit

Permalink
Port libcork to FreeBSD
Browse files Browse the repository at this point in the history
This patch should work for any 4.3 Net2 code base or newer, but we
are only testing on FreeBSD.
  • Loading branch information
John Zachary committed Mar 7, 2013
1 parent 62aaad8 commit 29a6467
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 29 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Expand Up @@ -21,7 +21,8 @@ execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/version.sh
OUTPUT_VARIABLE VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(VERSION_RESULT)
message(FATAL_ERROR "Cannot determine version number")
message(FATAL_ERROR
"Cannot determine version number: " ${VERSION_RESULT})
endif(VERSION_RESULT)
# This causes an annoying extra prompt in ccmake.
# message("Current version: " ${VERSION})
Expand Down
34 changes: 34 additions & 0 deletions include/libcork/config/bsd.h
@@ -0,0 +1,34 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/

#ifndef LIBCORK_CONFIG_BSD_H
#define LIBCORK_CONFIG_BSD_H

/*-----------------------------------------------------------------------
* Endianness
*/

#include <sys/endian.h>

#if BYTE_ORDER == BIG_ENDIAN
#define CORK_CONFIG_IS_BIG_ENDIAN 1
#define CORK_CONFIG_IS_LITTLE_ENDIAN 0
#elif BYTE_ORDER == LITTLE_ENDIAN
#define CORK_CONFIG_IS_BIG_ENDIAN 0
#define CORK_CONFIG_IS_LITTLE_ENDIAN 1
#else
#error "Cannot determine system endianness"
#endif

#define CORK_HAVE_REALLOCF 1
#define CORK_HAVE_PTHREADS 1


#endif /* LIBCORK_CONFIG_BSD_H */
11 changes: 10 additions & 1 deletion include/libcork/config/config.h
@@ -1,6 +1,6 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2011-2012, RedJack, LLC.
* Copyright © 2011-2013, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
Expand Down Expand Up @@ -34,6 +34,11 @@


/**** PLATFORMS ****/
#if (defined(__unix__) || defined(unix)) && !defined(USG)
/* We need this to test for BSD, but it's a good idea to have for
* any brand of Unix.*/
#include <sys/param.h>
#endif

#if defined(__linux)
/* Do some Linux-specific autodetection. */
Expand All @@ -43,6 +48,10 @@
/* Do some Mac OS X-specific autodetection. */
#include <libcork/config/macosx.h>

#elif defined(BSD) && (BSD >= 199103)
/* Do some BSD (4.3 code base or newer)specific autodetection. */
#include <libcork/config/bsd.h>

#endif /* platforms */


Expand Down
2 changes: 1 addition & 1 deletion make-dist.sh
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

PROJECT=libcork

Expand Down
2 changes: 1 addition & 1 deletion run.sh
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
# Usage: run.sh [debug|release] program arguments
#
# Runs a program from one of the build directories, with
Expand Down
42 changes: 19 additions & 23 deletions src/libcork/posix/env.c
Expand Up @@ -17,7 +17,21 @@
#include "libcork/os/subprocess.h"
#include "libcork/helpers/errors.h"

extern const char **environ;
#if defined(__APPLE__)
/* Apple doesn't provide access to the "environ" variable from a shared library.
* There's a workaround function to grab the environ pointer described at [1].
*
* [1] http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man7/environ.7.html
*/
#include <crt_externs.h>
#define environ (*_NSGetEnviron())

#else
/* On all other POSIX platforms, we assume that environ is available in shared
* libraries. */
extern char **environ;

#endif


struct cork_env_var {
Expand Down Expand Up @@ -74,7 +88,7 @@ cork_env_add_internal(struct cork_env *env, const char *name, const char *value)
struct cork_env *
cork_env_clone_current(void)
{
const char **curr;
char **curr;
struct cork_env *env = cork_env_new();

for (curr = environ; *curr != NULL; curr++) {
Expand Down Expand Up @@ -157,33 +171,15 @@ cork_env_set_vars(struct cork_hash_table_entry *entry, void *user_data)
return CORK_HASH_TABLE_MAP_CONTINUE;
}

#if defined(__APPLE__)
/* Apple doesn't provide clearenv(), and it also doesn't let us access the
* "environ" variable from a shared library. There's a workaround function to
* grab the environ pointer described at [1].
*
* [1] http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man7/environ.7.html
*/

#include <crt_externs.h>

static void
clearenv(void)
{
char ***ns_environ = _NSGetEnviron();
**ns_environ = NULL;
}

#elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__OpenBSD__)
/* A handful of other platforms [1] don't provide clearenv(), but do allow us to
* access the environ pointer directly.
#if defined(__APPLE__) || (defined(BSD) && (BSD >= 199103))
/* A handful of platforms [1] don't provide clearenv(), so we must implement our
* own version that clears the environ array directly.
*
* [1] http://www.gnu.org/software/gnulib/manual/html_node/clearenv.html
*/
static void
clearenv(void)
{
extern char **environ;
*environ = NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions version.sh
@@ -1,6 +1,6 @@
#!/bin/bash
#!/bin/sh
# ----------------------------------------------------------------------
# Copyright © 2011, RedJack, LLC.
# Copyright © 2011-2013, RedJack, LLC.
# All rights reserved.
#
# Please see the COPYING file in this distribution for license
Expand Down

0 comments on commit 29a6467

Please sign in to comment.