From b1795eaded1d1f4e3535641eaf482183e99c0bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Thu, 1 Jun 2023 18:08:48 -0300 Subject: [PATCH] Moved ``error()`` to a library and wrote a Makefile. --- Makefile | 50 ++++++++++++++++++++++++++++ README.md | 50 +++++++++++++++++++++++----- do_install.ksh | 34 ------------------- src/error.c | 63 +++++++++++++++++++++++++++++++++++ src/error.h | 68 +++++++++----------------------------- src/queue.h | 24 +------------- src/tree.h | 19 +---------- tests/LICENCE.FSFUL | 5 +++ tests/error_at_line_test.c | 13 ++++++++ 9 files changed, 191 insertions(+), 135 deletions(-) create mode 100644 Makefile delete mode 100755 do_install.ksh create mode 100644 src/error.c create mode 100644 tests/LICENCE.FSFUL create mode 100644 tests/error_at_line_test.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..66f447d --- /dev/null +++ b/Makefile @@ -0,0 +1,50 @@ +# Copyright (c) 2022-2023 Luiz Antônio Rangel +# SPDX-Licence-Identifier: BSD-2-Clause + +# Paths +ROOT ?= +DEFUSRBIN ?= /usr/bin/ +DEFSBIN ?= /sbin/ +MANDIR ?= /usr/share/man/ +STATIC_LIBDIR ?= /usr/lib/ +SYSINCLUDE_DIR ?= /usr/include/sys/ +INCLUDE_DIR ?= /usr/include/ + +# Programs +CC ?= gcc +LD ?= ld +AR ?= ar +INSTALL ?= /bin/install + +all: libegacy-compat test-libegacy-compat \ + src/cdefs.h src/error.h src/queue.h src/tree.h + +# Only error() and error_at_line() available for now. +libegacy-compat: error.o + $(AR) r libegacy-compat.a error.o + +error.o: src/error.c src/error.h + $(CC) -I src/ -c ./src/error.c -o error.o + +install: install-sysheaders install-headers install-libegacy-compat + +install-sysheaders: src/cdefs.h src/queue.h src/tree.h + for sys_header in src/cdefs.h src/queue.h src/tree.h; do \ + $(INSTALL) -c -m 644 $$sys_header $(ROOT)$(SYSINCLUDE_DIR) ; \ + done + +install-headers: src/error.h + for header in src/error.h; do \ + $(INSTALL) -c -m 644 $$header $(ROOT)$(INCLUDE_DIR) ; \ + done + +install-libegacy-compat: install-headers libegacy-compat + $(INSTALL) -c -m 664 ./libegacy-compat.a $(ROOT)$(STATIC_LIBDIR) + +test-libegacy-compat: libegacy-compat tests/error_at_line_test.c + $(CC) tests/error_at_line_test.c -Isrc/ -L. -legacy-compat + ./a.out + @echo 'The '\''error_at_line()'\'' function of libegacy-compat is working.' + +clean: + rm -f a.out *.o *.a *~ diff --git a/README.md b/README.md index 4135e11..6d8bc2f 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,47 @@ # musl-compat Legacy compatibility headers for the musl libc. -## Who's the responsible? -All kudos for the C language source code goes to the NetBSD and OpenBSD -team — read the copyright header of each file for more information. +## Build and install -All kudos for the original packaging (in fact, taking these headers, -testing it etc) goes to the Void Linux team. +If you wish to install everything, you can use the following: -And, for last but not least, the `do_install.ksh` script was made -entirely by me, so if you have any problems related specifically to -the script, blame it on me. :^) +```console +gmake && gmake install +``` + +If you wish to install just the system headers, you can use: + +```console +gmake install-sysheaders +``` + +And, if you wish to just install the ``error.h`` header along with the library +containing its functions, you can go with: + +```console +gmake && gmake test-libegacy-compat \ + && gmake install-libegacy-compat +``` + +That's pretty much about it. + +## How can I use the "libegacy-compat"? + +In your ``LDFLAGS``, add the value of ``-legacy-compat``. +Yeah, simple as that. + +## Licence + +``error.h`` and ``error.c``, part of ``libegacy-compat``, are covered by the +[BSD 2-Clause licence](./LICENCE), along with the Makefile itself. +Maybe this library can be updated and more compatibility functions be added +along the time, but there's no guarantee of that for now. + +``sys/tree.h`` was copied verbatim from the NetBSD source-tree by the Void Linux +project, so it's covered by the BSD 2-Clause licence too. +``sys/queue.h`` was also copied from NetBSD, but it's covered by the BSD 3-Clause +licence and its ownership is claimed by The Regents of the University of California. + +The test of ``error_at_line``(3) was taken from GNU m4 1.4.18 configure +Shell script, so it's covered --- or better, not covered --- by the +[FSF Unlimited License](./tests/LICENCE.FSFUL). diff --git a/do_install.ksh b/do_install.ksh deleted file mode 100755 index 80624f1..0000000 --- a/do_install.ksh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/ksh -# do_install.ksh - Install a set of files for a package. -# usage: DESTDIR=/custom/directory ./do_install.ksh -# Note: if DESTDIR is not set, it will default to /. - -# Copyright (C) 2022: Luiz Antônio Rangel (takusuman). -# SPDX-License-Identifier: BSD-2-Clause - -pkg=musl-compat -pkgver=0.5 -DESTDIR=${DESTDIR:-/} -sysinclude_sources=(src/cdefs.h src/queue.h src/tree.h) -include_sources=(src/error.h) -sources_licences=(LICENSE.BSD-3-Clause LICENSE.BSD-2-Clause) - -test -d ${DESTDIR}/usr/include/sys || mkdir -p ${DESTDIR}/usr/include/sys -test -d ${DESTDIR}/usr/share/doc/${pkg}-${pkgver}/LICENSE || mkdir -p ${DESTDIR}/usr/share/doc/${pkg}-${pkgver}/LICENSE - -for ((i=0; i<${#sysinclude_sources[@]}; i++)); do - f=${sysinclude_sources[$i]} - install -m 644 ${f} ${DESTDIR}/usr/include/sys -done -for ((i=0; i<${#include_sources[@]}; i++)); do - f=${include_sources[$i]} - install -m 644 ${f} ${DESTDIR}/usr/include -done - -sed -n '3,32p' < ${sysinclude_sources[1]} > ${sources_licences[0]} -sed -n '2,26p' < ${sysinclude_sources[2]} > ${sources_licences[1]} - -for ((i=0; i<${#sources_licences[@]}; i++)); do - f=${sources_licences[$i]} - install -m 644 ${f} ${DESTDIR}/usr/share/doc/${pkg}-${pkgver}/LICENSE -done diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..a1b3fb6 --- /dev/null +++ b/src/error.c @@ -0,0 +1,63 @@ +// Copyright (C) 2015 - 2022 Void Linux contributors +// Copyright (C) 2023 Pindorama +// Luiz Antônio Rangel +// SPDX-Licence-Identifier: BSD-2-Clause + +#include "error.h" +#include +#include +#include +#include +#include + +// The calling program shall set "program_invocation_name" to the name of the +// executing program itself. +extern char *program_invocation_name; +// Documented at the "error.h" header. +extern unsigned int error_message_count = 0; +extern int error_one_per_line = 0; + +extern void error(int status, int errnum, const char* format, ...) +{ + // Should be fflush(stdout), but that's unspecified if stdout has been closed; + // stick with fflush(NULL) for simplicity (GNU C library checks if the + // file descriptor is still valid). + fflush(NULL); + + va_list ap; + fprintf(stderr, "%s: ", program_invocation_name); + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); + if (errnum) + fprintf(stderr, ": %s", strerror(errnum)); + fprintf(stderr, "\n"); + error_message_count++; + if (status) + exit(status); +} + + +extern void error_at_line(int status, int errnum, const char *filename, + unsigned int linenum, const char *format, ...) +{ + va_list ap; + if (error_one_per_line) { + static const char *old_filename; + static int old_linenum; + if (linenum == old_linenum && filename == old_filename) + return; + old_filename = filename; + old_linenum = linenum; + } + fprintf(stderr, "%s: %s:%u: ", program_invocation_name, filename, linenum); + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); + if (errnum) + fprintf(stderr, ": %s", strerror(errnum)); + fprintf(stderr, "\n"); + error_message_count++; + if (status) + exit(status); +} diff --git a/src/error.h b/src/error.h index 9a4e1f8..5e8ad6a 100644 --- a/src/error.h +++ b/src/error.h @@ -1,60 +1,24 @@ -#ifndef _ERROR_H_ -#define _ERROR_H_ - -#include -#include -#include -#include -#include - -#warning usage of non-standard #include is deprecated +// Copyright (C) 2015 - 2022 Void Linux contributors +// Copyright (C) 2023 Pindorama +// Luiz Antônio Rangel +// SPDX-Licence-Identifier: BSD-2-Clause -static unsigned int error_message_count = 0; +#if !defined(_ERROR_H_) +#define _ERROR_H_ -static inline void error(int status, int errnum, const char* format, ...) -{ - /* should be fflush(stdout), but that's unspecified if stdout has been closed; - * stick with fflush(NULL) for simplicity (glibc checks if the fd is still valid) */ - fflush(NULL); +#warning usage of non-standard `#include ' is deprecated - va_list ap; - fprintf(stderr, "%s: ", program_invocation_name); - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); - if (errnum) - fprintf(stderr, ": %s", strerror(errnum)); - fprintf(stderr, "\n"); - error_message_count++; - if (status) - exit(status); -} +// As it suggests, it counts how many times error() was called, by being +// incremented every call. +static unsigned int error_message_count; -static int error_one_per_line = 0; +// If this is set non-zero, a sequence of error_at_line(3) calls with the +// same *filename and linenum values as input will result in only one entry +// on the specified file. +static int error_one_per_line; +static inline void error(int status, int errnum, const char* format, ...); static inline void error_at_line(int status, int errnum, const char *filename, - unsigned int linenum, const char *format, ...) -{ - va_list ap; - if (error_one_per_line) { - static const char *old_filename; - static int old_linenum; - if (linenum == old_linenum && filename == old_filename) - return; - old_filename = filename; - old_linenum = linenum; - } - fprintf(stderr, "%s: %s:%u: ", program_invocation_name, filename, linenum); - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); - if (errnum) - fprintf(stderr, ": %s", strerror(errnum)); - fprintf(stderr, "\n"); - error_message_count++; - if (status) - exit(status); -} - + unsigned int linenum, const char *format, ...); #endif /* _ERROR_H_ */ diff --git a/src/queue.h b/src/queue.h index 99d01a5..3998bf9 100644 --- a/src/queue.h +++ b/src/queue.h @@ -4,29 +4,7 @@ * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * SPDX-Licence-Identifier: BSD-3-Clause * * @(#)queue.h 8.5 (Berkeley) 8/20/94 */ diff --git a/src/tree.h b/src/tree.h index eaea56a..e6531b2 100644 --- a/src/tree.h +++ b/src/tree.h @@ -4,25 +4,8 @@ * Copyright 2002 Niels Provos * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * SPDX-Licence-Identifier: BSD-2-Clause * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _SYS_TREE_H_ diff --git a/tests/LICENCE.FSFUL b/tests/LICENCE.FSFUL new file mode 100644 index 0000000..bca2b2a --- /dev/null +++ b/tests/LICENCE.FSFUL @@ -0,0 +1,5 @@ +Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc. + + +This configure script is free software; the Free Software Foundation +gives unlimited permission to copy, distribute and modify it. diff --git a/tests/error_at_line_test.c b/tests/error_at_line_test.c new file mode 100644 index 0000000..a1f595f --- /dev/null +++ b/tests/error_at_line_test.c @@ -0,0 +1,13 @@ +// Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc. +// From GNU m4 1.4.18 configure Shell script. +// +// SPDX-Licence-Identifier: FSFUL + +#include +int +main () +{ +error_at_line (0, 0, "", 0, "an error occurred"); + ; + return 0; +}