Commit 1d082108 authored by Russ Allbery's avatar Russ Allbery

Import the standard basic C portability layer

Synchronized with rra-c-util 2.3-to-be.
parent 7ff8e23f
......@@ -11,10 +11,10 @@ EXTRA_DIST = LICENSE autogen patches/README patches/mit-krb5-1.4.4 \
patches/stanford-krb5-1.4.4 tools/krb5-sync.8 tools/krb5-sync.pod \
tools/krb5-sync-backend.8
# Handle the standard stuff that make maintainer-clean should probably remove
# but doesn't.
MAINTAINERCLEANFILES = Makefile.in aclocal.m4 config.h.in config.h.in~ \
configure tools/krb5-sync.8
noinst_LTLIBRARIES = portable/libportable.la
portable_libportable_la_SOURCES = portable/dummy.c portable/macros.h \
portable/stdbool.h portable/system.h
portable_libportable_la_LIBADD = $(LTLIBOBJS)
# Put the module into /usr/local/lib/kadmind by default, relative to --libdir.
moduledir = $(libdir)/kadmind
......@@ -27,6 +27,7 @@ plugin_passwd_update_la_SOURCES = plugin/ad.c plugin/api.c plugin/error.c \
if AFS
plugin_passwd_update_la_SOURCES += plugin/afs.c
endif
plugin_passwd_update_la_LIBADD = portable/libportable.la
# Rules for building the krb5-sync utility. We specify the CFLAGS to work
# around Automake's inability to link libtool objects into a program that
......@@ -36,6 +37,7 @@ endif
sbin_PROGRAMS = tools/krb5-sync
tools_krb5_sync_SOURCES = tools/krb5-sync.c $(plugin_passwd_update_la_SOURCES)
tools_krb5_sync_CFLAGS = $(AM_CFLAGS)
tools_krb5_sync_LDADD = portable/libportable.la
# Rules for the krb5-sync-backend script.
dist_sbin_SCRIPTS = tools/krb5-sync-backend
......@@ -43,6 +45,11 @@ dist_sbin_SCRIPTS = tools/krb5-sync-backend
# Rules for man pages.
man_MANS = tools/krb5-sync.8 tools/krb5-sync-backend.8
# Handle the standard stuff that make maintainer-clean should probably remove
# but doesn't.
MAINTAINERCLEANFILES = Makefile.in aclocal.m4 config.h.in config.h.in~ \
configure tools/krb5-sync.8
$(srcdir)/tools/krb5-sync.8: $(srcdir)/tools/krb5-sync.pod
pod2man --release=$(VERSION) --center="Administrative Commands" \
--section=8 $(srcdir)/tools/krb5-sync.pod > $@
......
dnl Process this file with autoconf to produce a configure script.
dnl Autoconf configuration for krb5-sync.
dnl
dnl Written by Russ Allbery <rra@stanford.edu>
dnl Copyright 2006, 2007, 2010
dnl Board of Trustees, Leland Stanford Jr. University.
dnl
dnl Copyright 2006, 2007 Board of Trustees, Leland Stanford Jr. University.
dnl See LICENSE for licensing terms.
AC_REVISION([$Revision$])
AC_PREREQ([2.61])
AC_PREREQ([2.64])
AC_INIT([krb5-sync], [1.2], [rra@stanford.edu])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_LIBOBJ_DIR([portable])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([1.10 -Wall -Werror])
AM_MAINTAINER_MODE
......@@ -15,6 +19,7 @@ AM_PROG_CC_C_O
AC_PROG_INSTALL
AM_DISABLE_STATIC
AC_PROG_LIBTOOL
RRA_LIB_KRB5
RRA_LIB_KRB5_SET
AC_CHECK_FUNCS([krb5_get_init_creds_opt_alloc \
......@@ -31,6 +36,14 @@ AS_IF([test x"$rra_afs" = xtrue],
AM_CONDITIONAL([AFS], [test x"$rra_afs" = xtrue])
AC_SEARCH_LIBS([ldap_initialize], [ldap])
AC_HEADER_STDBOOL
AC_CHECK_HEADERS([sys/bittypes.h])
AC_CHECK_DECLS([snprintf, vsnprintf])
AC_TYPE_LONG_LONG_INT
RRA_FUNC_SNPRINTF
AC_CHECK_FUNCS([setrlimit])
AC_REPLACE_FUNCS([strlcat strlcpy])
# Crank up the warnings if we're using GCC.
if test "x$GCC" = "xyes" ; then
CFLAGS="-Wall $CFLAGS"
......
dnl Test for a working C99 snprintf.
dnl
dnl Check for a working snprintf. Some systems have an snprintf that doesn't
dnl nul-terminate if the buffer isn't large enough. Others return -1 if the
dnl string doesn't fit into the buffer instead of returning the number of
dnl characters that would have been formatted. Still others don't support
dnl NULL as the buffer argument (just to get a count of the formatted length).
dnl
dnl Provides RRA_FUNC_SNPRINTF, which adds snprintf.o to LIBOBJS unless a
dnl fully working snprintf is found.
dnl
dnl Written by Russ Allbery <rra@stanford.edu>
dnl Copyright 2006, 2008, 2009
dnl Board of Trustees, Leland Stanford Jr. University
dnl
dnl See LICENSE for licensing terms.
dnl Source used by RRA_FUNC_SNPRINTF.
AC_DEFUN([_RRA_FUNC_SNPRINTF_SOURCE], [[
#include <stdio.h>
#include <stdarg.h>
char buf[2];
int
test(char *format, ...)
{
va_list args;
int count;
va_start(args, format);
count = vsnprintf(buf, sizeof buf, format, args);
va_end(args);
return count;
}
int
main()
{
return ((test("%s", "abcd") == 4 && buf[0] == 'a' && buf[1] == '\0'
&& snprintf(NULL, 0, "%s", "abcd") == 4) ? 0 : 1);
}
]])
dnl The user-callable test.
AC_DEFUN([RRA_FUNC_SNPRINTF],
[AC_CACHE_CHECK([for working snprintf], [rra_cv_func_snprintf_works],
[AC_RUN_IFELSE([AC_LANG_SOURCE([_RRA_FUNC_SNPRINTF_SOURCE])],
[rra_cv_func_snprintf_works=yes],
[rra_cv_func_snprintf_works=no],
[rra_cv_func_snprintf_works=no])])
AS_IF([test x"$rra_cv_func_snprintf_works" = xyes],
[AC_DEFINE([HAVE_SNPRINTF], 1,
[Define if your system has a working snprintf function.])],
[AC_LIBOBJ([snprintf])])])
......@@ -7,10 +7,14 @@
* Written by Russ Allbery <rra@stanford.edu>
* Based on code developed by Derrick Brashear and Ken Hornstein of Sine
* Nomine Associates, on behalf of Stanford University.
* Copyright 2006, 2007 Board of Trustees, Leland Stanford Jr. University
* Copyright 2006, 2007, 2010 Board of Trustees, Leland Stanford Jr. University
*
* See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/system.h>
/* Need to determine why this is deprecated. */
#define LDAP_DEPRECATED 1
......@@ -18,9 +22,6 @@
#include <errno.h>
#include <krb5.h>
#include <ldap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <plugin/internal.h>
......
......@@ -7,17 +7,16 @@
* Written by Russ Allbery <rra@stanford.edu>
* Based on code developed by Derrick Brashear and Ken Hornstein of Sine
* Nomine Associates, on behalf of Stanford University.
* Copyright 2006, 2007 Board of Trustees, Leland Stanford Jr. University
* Copyright 2006, 2007, 2010 Board of Trustees, Leland Stanford Jr. University
*
* See LICENSE for licensing terms.
*/
#include "config.h"
#include <config.h>
#include <portable/system.h>
#include <errno.h>
#include <krb5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#ifdef HAVE_KERBEROSIV_KRB_H
......
......@@ -13,16 +13,17 @@
* Written by Russ Allbery <rra@stanford.edu>
* Based on code developed by Derrick Brashear and Ken Hornstein of Sine
* Nomine Associates, on behalf of Stanford University.
* Copyright 2006, 2007 Board of Trustees, Leland Stanford Jr. University
* Copyright 2006, 2007, 2010 Board of Trustees, Leland Stanford Jr. University
*
* See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/system.h>
#include <com_err.h>
#include <errno.h>
#include <krb5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <plugin/internal.h>
......
......@@ -6,15 +6,13 @@
* calling com_err directly if necessary.
*
* Written by Russ Allbery <rra@stanford.edu>
* Copyright 2006, 2007 Board of Trustees, Leland Stanford Jr. University
* Copyright 2006, 2007, 2010 Board of Trustees, Leland Stanford Jr. University
*
* See LICENSE for licensing terms.
*/
#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include <sys/types.h>
#include <config.h>
#include <portable/system.h>
#include <krb5.h>
#if !defined(HAVE_KRB5_GET_ERROR_MESSAGE) && !defined(HAVE_KRB5_GET_ERR_TEXT)
......
......@@ -12,12 +12,13 @@
* See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/system.h>
#include <errno.h>
#include <kadm5/admin.h>
#include <kadm5/kadm5_err.h>
#include <krb5.h>
#include <stdio.h>
#include <string.h>
#include <plugin/internal.h>
......
......@@ -4,14 +4,16 @@
* Written by Russ Allbery <rra@stanford.edu>
* Based on code developed by Derrick Brashear and Ken Hornstein of Sine
* Nomine Associates, on behalf of Stanford University.
* Copyright 2006, 2007 Board of Trustees, Leland Stanford Jr. University
* Copyright 2006, 2007, 2010 Board of Trustees, Leland Stanford Jr. University
*
* See LICENSE for licensing terms.
*/
#ifndef INTERNAL_H
#define INTERNAL_H 1
#include "config.h"
#include <config.h>
#include <krb5.h>
#include <sys/types.h>
......
......@@ -9,20 +9,19 @@
* undone.
*
* Written by Russ Allbery <rra@stanford.edu>
* Copyright 2006, 2007 Board of Trustees, Leland Stanford Jr. University
* Copyright 2006, 2007, 2010 Board of Trustees, Leland Stanford Jr. University
*
* See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/system.h>
#include <dirent.h>
#include <fcntl.h>
#include <krb5.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <plugin/internal.h>
......
/*
* Dummy symbol to prevent an empty library.
*
* On platforms that already have all of the functions that libportable would
* supply, Automake builds an empty library and then calls ar with nonsensical
* arguments. Ensure that libportable always contains at least one symbol.
*
* Written by Russ Allbery <rra@stanford.edu>
* This work is hereby placed in the public domain by its author.
*/
/* Prototype to avoid gcc warnings. */
int portable_dummy(void);
int
portable_dummy(void)
{
return 42;
}
/*
* Portability macros used in include files.
*
* Written by Russ Allbery <rra@stanford.edu>
* This work is hereby placed in the public domain by its author.
*/
#ifndef PORTABLE_MACROS_H
#define PORTABLE_MACROS_H 1
/*
* __attribute__ is available in gcc 2.5 and later, but only with gcc 2.7
* could you use the __format__ form of the attributes, which is what we use
* (to avoid confusion with other macros).
*/
#ifndef __attribute__
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
# define __attribute__(spec) /* empty */
# endif
#endif
/*
* BEGIN_DECLS is used at the beginning of declarations so that C++
* compilers don't mangle their names. END_DECLS is used at the end.
*/
#undef BEGIN_DECLS
#undef END_DECLS
#ifdef __cplusplus
# define BEGIN_DECLS extern "C" {
# define END_DECLS }
#else
# define BEGIN_DECLS /* empty */
# define END_DECLS /* empty */
#endif
#endif /* !PORTABLE_MACROS_H */
This diff is collapsed.
/*
* Portability wrapper around <stdbool.h>.
*
* Provides the bool and _Bool types and the true and false constants,
* following the C99 specification, on hosts that don't have stdbool.h. This
* logic is based heavily on the example in the Autoconf manual.
*
* Written by Russ Allbery <rra@stanford.edu>
* This work is hereby placed in the public domain by its author.
*/
#ifndef PORTABLE_STDBOOL_H
#define PORTABLE_STDBOOL_H 1
#if HAVE_STDBOOL_H
# include <stdbool.h>
#else
# if HAVE__BOOL
# define bool _Bool
# else
# ifdef __cplusplus
typedef bool _Bool;
# elif _WIN32
# include <windef.h>
# define bool BOOL
# else
typedef unsigned char _Bool;
# define bool _Bool
# endif
# endif
# define false 0
# define true 1
# define __bool_true_false_are_defined 1
#endif
/*
* If we define bool and don't tell Perl, it will try to define its own and
* fail. Only of interest for programs that also include Perl headers.
*/
#ifndef HAS_BOOL
# define HAS_BOOL 1
#endif
#endif /* !PORTABLE_STDBOOL_H */
/*
* Replacement for a missing strlcat.
*
* Provides the same functionality as the *BSD function strlcat, originally
* developed by Todd Miller and Theo de Raadt. strlcat works similarly to
* strncat, except simpler. The result is always nul-terminated even if the
* source string is longer than the space remaining in the destination string,
* and the total space required is returned. The third argument is the total
* space available in the destination buffer, not just the amount of space
* remaining.
*
* Written by Russ Allbery <rra@stanford.edu>
* This work is hereby placed in the public domain by its author.
*/
#include <config.h>
#include <portable/system.h>
/*
* If we're running the test suite, rename strlcat to avoid conflicts with
* the system version.
*/
#if TESTING
# define strlcat test_strlcat
size_t test_strlcat(char *, const char *, size_t);
#endif
size_t
strlcat(char *dst, const char *src, size_t size)
{
size_t used, length, copy;
used = strlen(dst);
length = strlen(src);
if (size > 0 && used < size - 1) {
copy = (length >= size - used) ? size - used - 1 : length;
memcpy(dst + used, src, copy);
dst[used + copy] = '\0';
}
return used + length;
}
/*
* Replacement for a missing strlcpy.
*
* Provides the same functionality as the *BSD function strlcpy, originally
* developed by Todd Miller and Theo de Raadt. strlcpy works similarly to
* strncpy, except saner and simpler. The result is always nul-terminated
* even if the source string is longer than the destination string, and the
* total space required is returned. The destination string is not nul-filled
* like strncpy does, just nul-terminated.
*
* Written by Russ Allbery <rra@stanford.edu>
* This work is hereby placed in the public domain by its author.
*/
#include <config.h>
#include <portable/system.h>
/*
* If we're running the test suite, rename strlcpy to avoid conflicts with
* the system version.
*/
#if TESTING
# define strlcpy test_strlcpy
size_t test_strlcpy(char *, const char *, size_t);
#endif
size_t
strlcpy(char *dst, const char *src, size_t size)
{
size_t length, copy;
length = strlen(src);
if (size > 0) {
copy = (length >= size) ? size - 1 : length;
memcpy(dst, src, copy);
dst[copy] = '\0';
}
return length;
}
/*
* Standard system includes and portability adjustments.
*
* Declarations of routines and variables in the C library. Including this
* file is the equivalent of including all of the following headers,
* portably:
*
* #include <sys/types.h>
* #include <stdarg.h>
* #include <stdbool.h>
* #include <stdio.h>
* #include <stdlib.h>
* #include <stddef.h>
* #include <stdint.h>
* #include <string.h>
* #include <unistd.h>
*
* Missing functions are provided via #define or prototyped if available from
* the util helper library. Also provides some standard #defines.
*
* Written by Russ Allbery <rra@stanford.edu>
* This work is hereby placed in the public domain by its author.
*/
#ifndef PORTABLE_SYSTEM_H
#define PORTABLE_SYSTEM_H 1
/* Make sure we have our configuration information. */
#include <config.h>
/* BEGIN_DECL and __attribute__. */
#include <portable/macros.h>
/* A set of standard ANSI C headers. We don't care about pre-ANSI systems. */
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#if HAVE_STDINT_H
# include <stdint.h>
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
/* SCO OpenServer gets int32_t from here. */
#if HAVE_SYS_BITYPES_H
# include <sys/bitypes.h>
#endif
/* Get the bool type. */
#include <portable/stdbool.h>
BEGIN_DECLS
/* Default to a hidden visibility for all portability functions. */
#pragma GCC visibility push(hidden)
/*
* Provide prototypes for functions not declared in system headers. Use the
* HAVE_DECL macros for those functions that may be prototyped but implemented
* incorrectly or implemented without a prototype.
*/
#if !HAVE_DECL_SNPRINTF
extern int snprintf(char *, size_t, const char *, ...)
__attribute__((__format__(printf, 3, 4)));
#endif
#if !HAVE_DECL_VSNPRINTF
extern int vsnprintf(char *, size_t, const char *, va_list);
#endif
#if !HAVE_STRLCAT
extern size_t strlcat(char *, const char *, size_t);
#endif
#if !HAVE_STRLCPY
extern size_t strlcpy(char *, const char *, size_t);
#endif
/* Undo default visibility change. */
#pragma GCC visibility pop
END_DECLS
/* Windows provides snprintf under a different name. */
#ifdef _WIN32
# define snprintf _snprintf
#endif
/*
* POSIX requires that these be defined in <unistd.h>. If one of them has
* been defined, all the rest almost certainly have.
*/
#ifndef STDIN_FILENO
# define STDIN_FILENO 0
# define STDOUT_FILENO 1
# define STDERR_FILENO 2
#endif
/*
* C99 requires va_copy. Older versions of GCC provide __va_copy. Per the
* Autoconf manual, memcpy is a generally portable fallback.
*/
#ifndef va_copy
# ifdef __va_copy
# define va_copy(d, s) __va_copy((d), (s))
# else
# define va_copy(d, s) memcpy(&(d), &(s), sizeof(va_list))
# endif
#endif
#endif /* !PORTABLE_SYSTEM_H */
......@@ -10,20 +10,18 @@
* Written by Russ Allbery <rra@stanford.edu>
* Based on code developed by Derrick Brashear and Ken Hornstein of Sine
* Nomine Associates, on behalf of Stanford University.
* Copyright 2006, 2007 Board of Trustees, Leland Stanford Jr. University
* Copyright 2006, 2007, 2010 Board of Trustees, Leland Stanford Jr. University
*
* See LICENSE for licensing terms.
*/
#include "config.h"
#include <config.h>
#include <portable/system.h>
#include <com_err.h>
#include <errno.h>
#include <krb5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <plugin/internal.h>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment