diff --git a/Makefile b/Makefile index 23b0a6fb..52f6075a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -include vars.mk +-include config.mk SUBDIRS = include lib bin @@ -35,3 +35,4 @@ clean: @for dir in $(SUBDIRS); do \ $(MAKE) -C $$dir clean || exit 1; \ done + -rm -f config.h config.mk diff --git a/NEWS b/NEWS index 5b884e5b..917315a7 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,11 @@ +xbps-0.6.0 (2010-07-01): + + * Added strlcpy() and strlcat() from OpenBSD. Use them if they weren't + found by the configure script on the system. + + * Added a configure script that emulates GNU autoconf but simplified, to + ease settings for build options, flags, etc. + xbps-0.5.2.2 (2010-05-18): * libxbps: do not forget to reset a variable to 0 when removing obsolete files diff --git a/README b/README index bf92f892..92c47808 100644 --- a/README +++ b/README @@ -7,14 +7,15 @@ To build this you'll need: - openssl (development package with static libs) - libarchive >= 2.8.0 (development package with static libs) -Optionally to build the API documentation (enabled with BUILD_API_DOCS -make(1) argument): +Optionally to build the API documentation: - graphviz and doxygen. -By default it will be installed into /usr/local, can be changed -by setting PREFIX and DESTDIR vars as make(1) arguments. +Just run ./configure && make && make install. By default PREFIX is set +to `/usr/local', can be changed by setting --prefix. The DESTDIR variable +is also supported for package managers. -Just run "make" and "make DESTDIR=~/xbps install". +There are some more options that can be tweaked, see them with +`./configure --help'. Good luck! diff --git a/bin/Makefile b/bin/Makefile index 789233a7..a159e54d 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -1,4 +1,4 @@ -include ../vars.mk +-include ../config.mk SUBDIRS = xbps-uhelper SUBDIRS += xbps-repo diff --git a/bin/xbps-bin/Makefile b/bin/xbps-bin/Makefile index a6e4f6bc..95234bfc 100644 --- a/bin/xbps-bin/Makefile +++ b/bin/xbps-bin/Makefile @@ -1,5 +1,5 @@ TOPDIR = ../.. -include $(TOPDIR)/vars.mk +-include $(TOPDIR)/config.mk BIN = xbps-bin OBJS = check.o install.o main.o remove.o show-deps.o diff --git a/bin/xbps-repo/Makefile b/bin/xbps-repo/Makefile index de93b3dd..15015bae 100644 --- a/bin/xbps-repo/Makefile +++ b/bin/xbps-repo/Makefile @@ -1,5 +1,5 @@ TOPDIR = ../.. -include $(TOPDIR)/vars.mk +-include $(TOPDIR)/config.mk BIN = xbps-repo OBJS = main.o util.o index.o repository.o diff --git a/bin/xbps-repo/repository.c b/bin/xbps-repo/repository.c index 526fc939..c9eb6a31 100644 --- a/bin/xbps-repo/repository.c +++ b/bin/xbps-repo/repository.c @@ -33,6 +33,7 @@ #include #include "defs.h" +#include "config.h" struct repoinfo { char *pkgidxver; @@ -98,54 +99,13 @@ out: return rpi; } -static bool -sanitize_url(char *buf, const char *path) -{ - char *dirnp, *basenp, *dir, *base, *tmp; - bool rv = false; - - dir = strdup(path); - if (dir == NULL) - return false; - - base = strdup(path); - if (base == NULL) { - free(dir); - return false; - } - - dirnp = dirname(dir); - if (strcmp(dirnp, ".") == 0) - goto out; - - basenp = basename(base); - if (strcmp(basenp, base) == 0) - goto out; - - tmp = strncpy(buf, dirnp, PATH_MAX - 1); - if (sizeof(*tmp) >= PATH_MAX) - goto out; - - buf[strlen(buf) + 1] = '\0'; - if (strcmp(dirnp, "/")) - strncat(buf, "/", 1); - strncat(buf, basenp, PATH_MAX - strlen(buf) - 1); - rv = true; - -out: - free(dir); - free(base); - - return rv; -} - int unregister_repository(const char *uri) { char idxstr[PATH_MAX]; int rv = 0; - if (!sanitize_url(idxstr, uri)) + if (!realpath(uri, idxstr)) return errno; if ((rv = xbps_repository_unregister(idxstr)) != 0) { @@ -168,7 +128,7 @@ register_repository(const char *uri) int rv = 0; if (xbps_check_is_repo_string_remote(uri)) { - if (!sanitize_url(idxstr, uri)) + if (!realpath(uri, idxstr)) return errno; printf("Fetching remote package index at %s...\n", uri); @@ -186,7 +146,7 @@ register_repository(const char *uri) plist = xbps_get_pkg_index_plist(idxstr); } else { - if (!sanitize_url(idxstr, uri)) + if (!realpath(uri, idxstr)) return errno; /* @@ -226,7 +186,7 @@ register_repository(const char *uri) } printf("Added package index at %s (v%s) with %ju packages.\n", - uri, rpi->pkgidxver, rpi->totalpkgs); + idxstr, rpi->pkgidxver, rpi->totalpkgs); out: if (rpi != NULL) diff --git a/bin/xbps-uhelper/Makefile b/bin/xbps-uhelper/Makefile index b385d43b..64c06d33 100644 --- a/bin/xbps-uhelper/Makefile +++ b/bin/xbps-uhelper/Makefile @@ -1,5 +1,5 @@ TOPDIR = ../.. -include $(TOPDIR)/vars.mk +-include $(TOPDIR)/config.mk BIN = xbps-uhelper diff --git a/bin/xbps-uhelper/main.c b/bin/xbps-uhelper/main.c index 89f8f6eb..57b6138a 100644 --- a/bin/xbps-uhelper/main.c +++ b/bin/xbps-uhelper/main.c @@ -102,9 +102,9 @@ main(int argc, char **argv) { prop_dictionary_t dict; const char *version; - char *plist, *pkgname, *pkgver, *in_chroot_env, *root = ""; + char *plist, *pkgname, *pkgver, *in_chroot_env, *root = "", *hash; bool in_chroot = false; - int c, rv = 0; + int i, c, rv = 0; while ((c = getopt(argc, argv, "Var:")) != -1) { switch (c) { @@ -299,9 +299,6 @@ main(int argc, char **argv) if (argc < 2) usage(); - char *hash; - int i; - for (i = 1; i < argc; i++) { hash = xbps_get_file_hash(argv[i]); if (hash == NULL) { @@ -320,7 +317,6 @@ main(int argc, char **argv) usage(); xbps_fetch_set_cache_connection(0, 0); - int i; for (i = 1; i < argc; i++) { rv = xbps_fetch_file(argv[i], ".", false, "v"); diff --git a/configure b/configure new file mode 100755 index 00000000..a1eafe29 --- /dev/null +++ b/configure @@ -0,0 +1,418 @@ +#!/bin/sh +# Try and be like autotools configure, but without autotools + +# Ensure that we do not inherit these from env +STRLCPY= +STRLCAT= +OS= +BUILD= +HOST= +TARGET= +DEBUG= +BUILD_API_DOCS= +BUILD_PIE= +EXTERNAL_PROPLIB= + +usage() +{ + cat <<_EOF +\`configure' configures XBPS to adapt to many kinds of systems. + +By default, \`make install' will install all the files in +\`/usr/local/sbin', \`/usr/local/lib' etc. You can specify +an installation prefix other than \`/usr/local' using \`--prefix', +for instance \`--prefix=\$HOME'. + +--prefix=DIR install architecture-independent files in PREFIX +--sbindir=DIR system admin executables [PREFIX/sbin] +--libdir=DIR object code libraries [PREFIX/lib] +--includedir=DIR C header files [PREFIX/include] +--mandir=DIR man documentation [PREFIX/share/man] +--datadir=DIR read-only architecture-independent data [PREFIX/share] + +--debug Build with debugging code and symbols +--with-pie Build XBPS programs as PIE (default disabled) +--with-api-docs install XBPS API Library documentation (default disabled) +--with-external-proplib Use external proplib [default disabled] + +_EOF + exit 1 +} + +for x; do + opt=${x%%=*} + var=${x#*=} + case "$opt" in + --debug) DEBUG=yes;; + --prefix) PREFIX=$var;; + --sbindir) SBINDIR=$var;; + --mandir) MANDIR=$var;; + --datadir) SHAREDIR=$var;; + --build) BUILD=$var;; + --host) HOST=$var;; + --target) TARGET=$var;; + --includedir) INCLUDEDIR=$var;; + --libdir) LIBDIR=$var;; + --datadir|--infodir) ;; # ignore autotools + --with-api-docs) BUILD_API_DOCS=$var;; + --with-pie) BUILD_PIE=$var;; + --with-external-proplib) EXTERNAL_PROPLIB=$var;; + --help) usage;; + *) echo "$0: WARNING: unknown option $opt" >&2;; + esac +done + +: ${SED:=sed} + +: ${PREFIX:=/usr/local} +: ${SYSCONFDIR:=${PREFIX}/etc} +: ${SBINDIR:=${PREFIX}/sbin} +: ${LIBDIR:=${PREFIX}/lib} +: ${SHAREDIR:=${PREFIX}/share} +: ${MANDIR:=${PREFIX}/share/man} +: ${INCLUDEDIR:=${PREFIX}/include} +: ${TOPDIR:=..} + +_which() +{ + x="$(which "$1" 2>/dev/null)" + if [ -n "$x" ]; then + echo "$x" + return 0 + fi + for x in /sbin/"$1" /usr/sbin/"$1" \ + /usr/pkg/sbin/"$1" /usr/local/sbin/"$1" + do + if [ -e "$x" ]; then + echo "$x" + return 0 + fi + done + return 1 +} + +CONFIG_H=config.h +CONFIG_MK=config.mk + +if [ -z "$BUILD" ]; then + BUILD=`uname -m`-unknown-`uname -s | tr '[:upper:]' '[:lower:]'` +fi +if [ -z "$HOST" ]; then + [ -z "$TARGET" ] && TARGET=$BUILD + HOST=$TARGET +fi +if [ -z "$TARGET" ]; then + [ -z "$HOST" ] && HOST=$BUILD + TARGET=$HOST +fi + +if [ -z "$OS" ]; then + # Derive OS from cpu-manufacturer-os-kernel + CPU=${TARGET%%-*} + REST=${TARGET#*-} + MANU=${REST%%-*} + REST=${REST#*-} + OS=${REST%%-*} + REST=${REST#*-} + KERNEL=${REST%%-*} +fi + +echo "Configuring xbps for ... $OS" +rm -f $CONFIG_H $CONFIG_MK +echo "# Common vars used by XBPS on $OS." >$CONFIG_MK +echo "/* $OS */" >$CONFIG_H + +echo "TOPDIR ?= $TOPDIR" >>$CONFIG_MK +echo "PREFIX ?= $PREFIX" >>$CONFIG_MK +echo "SBINDIR ?= $SBINDIR" >>$CONFIG_MK +echo "INCLUDEDIR ?= $INCLUDEDIR" >>$CONFIG_MK +echo "LIBDIR ?= $LIBDIR" >>$CONFIG_MK +echo "MANDIR ?= $MANDIR" >>$CONFIG_MK +echo "SHAREDIR ?= $SHAREDIR" >>$CONFIG_MK + +[ -z "$DEBUG" ] && DEBUG=no +[ -z "$BUILD_PIE" ] && BUILD_PIE_VAL=no + +if [ -z "$CC" ]; then + printf "Looking for compiler ... " + for b in $TARGET- ""; do + for cc in gcc pcc icc cc clang; do + if type $b$cc >/dev/null 2>&1; then + CC=$b$cc + echo "$CC" + break + fi + done + [ -n "$CC" ] && break + done + if [ -z "$CC" ]; then + echo + echo "no suitable compiler found - aborting" >&2 + exit 1 + fi +else + echo "Using compiler $CC" +fi +echo "CC = $CC" >>$CONFIG_MK +echo "CFLAGS = -fPIC -DPIC" >>$CONFIG_MK +echo "LDFLAGS = -L\$(TOPDIR)/lib -L\$(LIBDIR)" >>$CONFIG_MK +echo "CPPFLAGS = -I. -I\$(TOPDIR) -I\$(TOPDIR)/include" >>$CONFIG_MK +echo "CPPFLAGS += -DHAVE_CONFIG_H" >>$CONFIG_MK + +if [ -n "$DEBUG" -a "$DEBUG" != no -a "$DEBUG" != false ]; then + echo "Building with debugging symbols." + echo "INSTALL_STRIPPED =" >>$CONFIG_MK + echo "CFLAGS += -g" >>$CONFIG_MK + echo "CPPFLAGS += -DDEBUG" >>$CONFIG_MK +else + echo "INSTALL_STRIPPED = -s" >>$CONFIG_MK +fi + +case "$OS" in +linux) + echo "CPPFLAGS += -D_XOPEN_SOURCE=600 -D_GNU_SOURCE" >>$CONFIG_MK + echo "CPPFLAGS += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE" >> $CONFIG_MK + echo "CPPFLAGS += -D_LARGE_FILES" >> $CONFIG_MK + ;; +*) + ;; +esac + + +# +# Check if external proplib should be used instead. +# +if [ -z "$EXTERNAL_PROPLIB" ]; then + EXTERNAL_PROPLIB_VALUE=no + echo "CPPFLAGS += -I\$(TOPDIR)/lib/portableproplib" >>$CONFIG_MK +else + EXTERNAL_PROPLIB_VALUE=yes + echo "USE_EXTERNAL_PROPLIB = 1" >>$CONFIG_MK + echo "LDFLAGS += -lprop" >>$CONFIG_MK + echo "STATIC_PROPLIB = -lprop" >> $CONFIG_MK +fi + + +# Add CPPFLAGS and CFLAGS to CC for testing features +XCC="$CC `$SED -n -e 's/CPPLAGS+=*\(.*\)/\1/p' $CONFIG_MK`" +XCC="$XCC `$SED -n -e 's/CFLAGS+=*\(.*\)/\1/p' $CONFIG_MK`" + +check_compiler_flag() +{ + local flag="$1" + local mode="$2" + local var="$3" + local rv=0 + + if [ -z "$var" ]; then + var="CFLAGS" + fi + + printf "Checking if $CC supports -${mode}${flag} ... " + cat <_ccflag.c +#include +int main(void) { return 0; } +EOF + if $XCC -${mode}${flag} _ccflag.c -o _ccflag 2>_ccflag.err; then + if ! test -s _ccflag.err; then + if [ "$mode" = "W" ]; then + echo "CPPFLAGS += -${mode}${flag}" >>$CONFIG_MK + else + echo "$var += -${mode}${flag}" >>$CONFIG_MK + fi + echo "yes." + else + rv=1 + echo "no." + fi + else + rv=1 + echo "no." + fi + rm -f _ccflag.c _ccflag _ccflag.err + return $rv +} + +# +# Check for some compiler warning flags. +# +for f in all extra error shadow "format=2" missing-prototypes \ + missing-declarations nested-externs \ + cast-align cast-qual pointer-arith comment unused-macros \ + declaration-after-statement stack-protector; do + check_compiler_flag ${f} W +done + +# +# Check for some compiler flags. +# +check_compiler_flag stack-protector-all f +if [ $? -ne 0 ]; then + check_compiler_flag stack-protector f +fi +if [ $? -eq 0 ]; then + check_compiler_flag "param ssp-buffer-size=1" - +fi +if [ $? -eq 0 ]; then + echo "CPPFLAGS += -D_FORTIFY_SOURCE=2" >>$CONFIG_MK +fi +check_compiler_flag "visibility=default" f SHAREDLIB_CFLAGS +check_compiler_flag "std=c99" + +# +# Check if -fPIE and -pie are supported if --build-pie is set. +# +if [ -n "$BUILD_PIE" ]; then + BUILD_PIE_VAL=yes + echo "Building programs as PIE (Position Independent Executable)." + check_compiler_flag PIE f PROG_CFLAGS + check_compiler_flag pie "" PROG_LDFLAGS +fi + +# +# Check for strlcpy(). +# +printf "Checking for strlcpy() ... " +cat <_strlcpy.c +#include +int main(void) { + const char s1[] = "foo"; + char s2[10]; + strlcpy(s2, s1, sizeof(s2)); + return 0; +} +EOF +if $XCC _strlcpy.c -o _strlcpy 2>/dev/null; then + STRLCPY=yes +else + STRLCPY=no +fi +echo "$STRLCPY." +rm -f _strlcpy.c _strlcpy +if [ "$STRLCPY" = no ]; then + echo "COMPAT_SRCS += compat/strlcpy.o" >>$CONFIG_MK + echo "#include \"strlcpy.h\"" >>$CONFIG_H + echo "CPPFLAGS += -DHAVE_STRLCPY" >> $CONFIG_MK +fi + +# +# Check for strlcat(). +printf "Checking for strlcat() ... " +cat < _strlcat.c +#include +int main(void) { + const char src[] = "foo" + char dst[10]; + strlcat(dst, src, sizeof(dst)); + return 0; +} +EOF +if $XCC _strlcat.c -o _strlcat 2>/dev/null; then + STRLCAT=yes +else + STRLCAT=no +fi +echo "$STRLCAT." +rm -f _strlcat.c _strlcat +if [ "$STRLCAT" = no ]; then + echo "COMPAT_SRCS += compat/strlcat.o" >>$CONFIG_MK + echo "#include \"strlcat.h\"" >>$CONFIG_H + echo "CPPFLAGS += -DHAVE_STRLCAT" >>$CONFIG_MK +fi + +# +# If building API library documentation, doxygen and graphviz are required. +# +if [ -n "$BUILD_API_DOCS" ]; then + echo "Building API documentation via doxygen and graphviz." + printf "Checking for doxygen ..." + DOXYGEN_BIN=$(_which doxygen) + if [ -z "$DOXYGEN_BIN" ]; then + echo "not found, exiting." + exit 1 + else + echo yes + fi + printf "Checking for graphviz ... " + DOT_BIN=$(_which dot) + if [ -z "$DOT_BIN" ]; then + echo "dot(1) command not found, exiting." + exit 1 + else + echo yes + fi + + echo "BUILD_API_DOCS= yes" >> $CONFIG_MK + BUILD_API_DOCS_VALUE=yes +else + BUILD_API_DOCS_VALUE=no +fi + +# +# pkg-config is required to know dependencies for static linking. +# +printf "Checking for pkg-config ... " +PKGCONFIG_BIN=$(_which pkg-config) +if [ -z "$PKGCONFIG_BIN" ]; then + echo "not found, exiting." + exit 1 +else + echo yes +fi + +# +# zlib with pkg-config support is required. +# +printf "Checking for zlib via pkg-config ..." +if ! $PKGCONFIG_BIN --exists zlib; then + echo "zlib.pc file not found, exiting." + exit 1 +else + echo "found version $($PKGCONFIG_BIN --modversion zlib)." +fi + +echo "STATIC_LIBS = -lz \$(STATIC_PROPLIB) -lpthread" >>$CONFIG_MK + +# +# OpenSSL libssl with pkg-config support is required. +# +printf "Checking for OpenSSL via pkg-config ..." +if ! $PKGCONFIG_BIN --exists libssl; then + echo "libssl.pc file not found, exiting." + exit 1 +else + echo "found version $($PKGCONFIG_BIN --modversion libssl)." +fi + +echo "STATIC_LIBS += $($PKGCONFIG_BIN --libs --static libssl)" >>$CONFIG_MK + +# +# libarchive >= 2.8.0 with pkg-config support is required. +# +printf "Checking for libarchive via pkg-config ... " +if ! $PKGCONFIG_BIN --exists libarchive; then + echo "libarchive.pc file not found, exiting." + exit 1 +else + echo "found version $($PKGCONFIG_BIN --modversion libarchive)." +fi + +echo "STATIC_LIBS += $($PKGCONFIG_BIN --libs --static libarchive)" >>$CONFIG_MK + +echo +echo " XBPS has been configured with the following options:" +echo +echo " SBINDIR = $SBINDIR" +echo " LIBDIR = $LIBDIR" +echo " INCLUDEDIR = $INCLUDEDIR" +echo " SHAREDIR = $SHAREDIR" +echo " MANDIR = $MANDIR" +echo " BUILD_API_DOCS = $BUILD_API_DOCS_VALUE" +echo " BUILD_PIE = $BUILD_PIE_VAL" +echo " DEBUG = $DEBUG" +echo " EXTERNAL PROPLIB = $EXTERNAL_PROPLIB_VALUE" +echo +echo " You can now run make && make install clean." +echo + +exit 0 diff --git a/doc/Makefile b/doc/Makefile index 6cf1a3e5..7696c642 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -1,4 +1,5 @@ -# +-include ../config.mk + # Makefile to build the libxbps API documentation. # DOXYF ?= xbps_api_doxyfile @@ -22,3 +23,8 @@ doxygendocs: $(FILES) clean: -rm -f images/*.$(FORMAT) -rm -rf ../api + +.PHONY: install +install: + install -d $(DESTDIR)$(SHAREDIR)/doc/xbps + cp -r ../api $(DESTDIR)$(SHAREDIR)/doc/xbps diff --git a/include/Makefile b/include/Makefile index db569079..2223d124 100644 --- a/include/Makefile +++ b/include/Makefile @@ -1,4 +1,4 @@ -include ../vars.mk +-include ../config.mk INCS = xbps_api.h diff --git a/include/strlcat.h b/include/strlcat.h new file mode 100644 index 00000000..f3817268 --- /dev/null +++ b/include/strlcat.h @@ -0,0 +1,8 @@ +#ifndef STRLCAT_H +#define STRLCAT_H + +#include + +size_t strlcat(char *, const char *, size_t); + +#endif diff --git a/include/strlcpy.h b/include/strlcpy.h new file mode 100644 index 00000000..887f175c --- /dev/null +++ b/include/strlcpy.h @@ -0,0 +1,8 @@ +#ifndef STRLCPY_H +#define STRLCPY_H + +#include + +size_t strlcpy(char *, const char *, size_t); + +#endif diff --git a/lib/Makefile b/lib/Makefile index 8aac64fe..cb7d3969 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,4 +1,4 @@ -include ../vars.mk +-include ../config.mk LIBXBPS_MAJOR = 0 LIBXBPS_MINOR = 0 @@ -15,7 +15,7 @@ LIBPROP_OBJS += portableproplib/prop_stack.o portableproplib/prop_string.o LIBPROP_OBJS += portableproplib/prop_array_util.o portableproplib/prop_number.o LIBPROP_OBJS += portableproplib/prop_dictionary_util.o LIBPROP_OBJS += portableproplib/prop_data.o -LIBPROP_CFLAGS = -Wno-cast-qual -Wno-unused-parameter -Wno-stack-protector +LIBPROP_CFLAGS = -Wno-cast-qual -Wno-unused-parameter ifdef USE_EXTERNAL_PROPLIB LIBPROP_OBJS = @@ -26,7 +26,7 @@ endif LIBFETCH_OBJS = fetch/common.o fetch/fetch.o fetch/file.o LIBFETCH_OBJS += fetch/ftp.o fetch/http.o LIBFETCH_CPPFLAGS = -DFTP_COMBINE_CWDS -DNETBSD -DINET6 -DWITH_SSL -LIBFETCH_CFLAGS = -Wno-unused-macros -Wno-conversion -Wno-stack-protector +LIBFETCH_CFLAGS = -Wno-unused-macros -Wno-conversion LIBFETCH_SHLIBCFLAGS = -fvisibility=hidden LIBFETCH_INCS = fetch/common.h LIBFETCH_GEN = fetch/ftperr.h fetch/httperr.h @@ -38,6 +38,7 @@ OBJS += regpkgs_dictionary.o remove.o remove_obsoletes.o repository.o OBJS += repository_finddeps.o repository_findpkg.o repository_plist.o OBJS += repository_pool.o repository_sync_index.o requiredby.o sha256.o OBJS += sortdeps.o state.o unpack.o util.o pkgmatch.o mkpath.o +OBJS += $(COMPAT_SRCS) .PHONY: all all: libxbps.so libxbps.a diff --git a/lib/compat/strlcat.c b/lib/compat/strlcat.c new file mode 100644 index 00000000..48334aff --- /dev/null +++ b/lib/compat/strlcat.c @@ -0,0 +1,57 @@ +/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "strlcat.h" + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return(dlen + (s - src)); /* count does not include NUL */ +} diff --git a/lib/compat/strlcpy.c b/lib/compat/strlcpy.c new file mode 100644 index 00000000..d50a2606 --- /dev/null +++ b/lib/compat/strlcpy.c @@ -0,0 +1,53 @@ +/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "strlcpy.h" + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} diff --git a/lib/pkgmatch.c b/lib/pkgmatch.c index ce70e556..62ea455a 100644 --- a/lib/pkgmatch.c +++ b/lib/pkgmatch.c @@ -26,6 +26,7 @@ #include #include +#include "config.h" /** * @file lib/pkgmatch.c @@ -116,7 +117,8 @@ int xbps_pkgpattern_match(const char *instpkg, char *pattern) { const char *fname = instpkg; - char basefname[PATH_MAX], condchar = '\0', *condition; + char *basefname, condchar = '\0', *condition; + size_t len = 0; int rv = 0; memset(&basefname, 0, sizeof(basefname)); @@ -134,7 +136,11 @@ xbps_pkgpattern_match(const char *instpkg, char *pattern) *condition = '\0'; ch = strrchr(fname, '-'); if (ch && ch - fname < PATH_MAX) { - strncpy(basefname, fname, ch - fname); + len = ch - fname + 1; + basefname = malloc(len); + if (basefname == NULL) + return -1; + strlcpy(basefname, fname, len); fname = basefname; } } @@ -177,6 +183,8 @@ xbps_pkgpattern_match(const char *instpkg, char *pattern) break; } } + if (basefname) + free(basefname); return rv; } diff --git a/lib/portableproplib/prop_object.c b/lib/portableproplib/prop_object.c index fab7f555..d70b67db 100644 --- a/lib/portableproplib/prop_object.c +++ b/lib/portableproplib/prop_object.c @@ -41,6 +41,10 @@ #include +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + /* * _prop_object_init -- * Initialize an object. Called when sub-classes create @@ -812,7 +816,10 @@ _prop_object_externalize_write_file(const char *fname, const char *xml, size_t len, bool do_compress) { gzFile *gzf = NULL; - char tname[PATH_MAX], *otname; + char tname[PATH_MAX]; +#ifndef HAVE_STRLCAT + char *otname; +#endif int fd; int save_errno; mode_t myumask; diff --git a/lib/util.c b/lib/util.c index 5171e33b..51c1b7d1 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2008-2009 Juan Romero Pardines. + * Copyright (c) 2008-2010 Juan Romero Pardines. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -37,6 +37,7 @@ #include #include "sha256.h" +#include "config.h" /** * @file lib/util.c @@ -242,10 +243,11 @@ xbps_get_pkg_name(const char *pkg) return NULL; len = strlen(pkg) - strlen(tmp) + 1; - pkgname = malloc(len); - strncpy(pkgname, pkg, len); - pkgname[len - 1] = '\0'; + if (pkgname == NULL) + return NULL; + + strlcpy(pkgname, pkg, len); return pkgname; } @@ -267,8 +269,7 @@ xbps_get_pkgpattern_name(const char *pkg) if (pkgname == NULL) return NULL; - strncpy(pkgname, pkg, len); - pkgname[len - 1] = '\0'; + strlcpy(pkgname, pkg, len); return pkgname; } diff --git a/prog.mk b/prog.mk index 9cf8ac98..6bcebc6f 100644 --- a/prog.mk +++ b/prog.mk @@ -1,5 +1,7 @@ +-include $(TOPDIR)/config.mk + OBJS ?= main.o -CFLAGS += -fPIE +CFLAGS += $(PROG_CFLAGS) LDFLAGS += -lxbps .PHONY: all @@ -39,5 +41,5 @@ $(BIN).static: $(OBJS) $(BIN): $(OBJS) @printf " [CCLD]\t\t$@\n" - @$(CC) $^ $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -pie -o $@ + @$(CC) $^ $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(PROG_LDFLAGS) -o $@ diff --git a/vars.mk b/vars.mk deleted file mode 100644 index 6730f7a6..00000000 --- a/vars.mk +++ /dev/null @@ -1,43 +0,0 @@ -# Common variables. - -PREFIX ?= /usr/local -SBINDIR ?= $(PREFIX)/sbin -LIBDIR ?= $(PREFIX)/lib -INCLUDEDIR ?= $(PREFIX)/include -MANDIR ?= $(PREFIX)/share/man -TOPDIR ?= .. -INSTALL_STRIPPED ?= -s - -# To build the libxbps API documentation, requires graphviz and doxygen. -# Uncomment this to enable. -#BUILD_API_DOCS = 1 - -# Uncomment this to build and link from an external proplib. -#USE_EXTERNAL_PROPLIB = 1 - -LDFLAGS = -L$(TOPDIR)/lib -CPPFLAGS = -I$(TOPDIR)/include -D_XOPEN_SOURCE=600 -D_GNU_SOURCE -CPPFLAGS += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGE_FILES - -ifndef USE_EXTERNAL_PROPLIB -CPPFLAGS += -I$(TOPDIR)/lib/portableproplib -else -STATIC_PROPLIB = -lprop -endif - -ifdef DEBUG -INSTALL_STRIPPED = -DEBUG_FLAGS = -g -CPPFLAGS += -DDEBUG -endif - -WARNFLAGS = -std=c99 -Wall -Wextra -Werror -Wshadow -Wformat=2 -WARNFLAGS += -Wmissing-declarations -Wcomment -Wunused-macros -Wendif-labels -WARNFLAGS += -Wcast-qual -Wcast-align -Wstack-protector -CFLAGS = $(DEBUG_FLAGS) $(WARNFLAGS) -fPIC -DPIC -fstack-protector-all -SHAREDLIB_CFLAGS = -fvisibility=default - -# Grr, hate the static libs! -STATIC_LIBS = -lz $(STATIC_PROPLIB) -lpthread -STATIC_LIBS += `pkg-config openssl --libs --static` -STATIC_LIBS += `pkg-config libarchive --libs --static`