Skip to content

Commit

Permalink
Moved error() to a library and wrote a Makefile.
Browse files Browse the repository at this point in the history
  • Loading branch information
takusuman committed Jun 1, 2023
1 parent e1a3fb2 commit b1795ea
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 135 deletions.
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2022-2023 Luiz Antônio Rangel <[email protected]>
# 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 *~
50 changes: 42 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
34 changes: 0 additions & 34 deletions do_install.ksh

This file was deleted.

63 changes: 63 additions & 0 deletions src/error.c
Original file line number Diff line number Diff line change
@@ -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 <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

// 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);
}
68 changes: 16 additions & 52 deletions src/error.h
Original file line number Diff line number Diff line change
@@ -1,60 +1,24 @@
#ifndef _ERROR_H_
#define _ERROR_H_

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#warning usage of non-standard #include <error.h> 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 <error.h>' 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_ */
24 changes: 1 addition & 23 deletions src/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
19 changes: 1 addition & 18 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,8 @@
* Copyright 2002 Niels Provos <[email protected]>
* 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_
Expand Down
5 changes: 5 additions & 0 deletions tests/LICENCE.FSFUL
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions tests/error_at_line_test.c
Original file line number Diff line number Diff line change
@@ -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 <error.h>
int
main ()
{
error_at_line (0, 0, "", 0, "an error occurred");
;
return 0;
}

0 comments on commit b1795ea

Please sign in to comment.