commit: c40198a25ff835e4daab2401b1f260919aff052c
parent fda61c4a476450193bb0baf6f55c09f69ee680d8
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 7 Dec 2024 04:33:56 +0100
cmd/printf: switch from <err.h> to ./lib/err.h
Diffstat:
5 files changed, 133 insertions(+), 19 deletions(-)
diff --git a/Makefile b/Makefile
@@ -210,3 +210,7 @@ test-cmd/pathchk-getlimits: test-cmd/pathchk-getlimits.c Makefile
test-cmd/getpriority: test-cmd/getpriority.c Makefile
$(CC) -std=c99 $(CFLAGS) -o $@ test-cmd/getpriority.c $(LDFLAGS) $(LDSTATIC)
+
+cmd/printf: cmd/printf.c lib/err.c lib/err.h Makefile config.mk
+ $(RM) -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno}
+ $(CC) -std=c99 $(CFLAGS) -o cmd/printf cmd/printf.c lib/err.c $(LDFLAGS) $(LDSTATIC)
diff --git a/bootstrap.sh b/bootstrap.sh
@@ -10,7 +10,7 @@ set -ex
$CC -std=c99 $CFLAGS -o cmd/cat cmd/cat.c lib/fs.c $LDFLAGS $LDSTATIC
$CC -std=c99 $CFLAGS -o cmd/echo cmd/echo.c $LDFLAGS $LDSTATIC
-$CC -std=c99 $CFLAGS -o cmd/printf cmd/printf.c $LDFLAGS $LDSTATIC
+$CC -std=c99 $CFLAGS -o cmd/printf cmd/printf.c lib/err.c $LDFLAGS $LDSTATIC
$CC -std=c99 $CFLAGS -o cmd/rm cmd/rm.c lib/consent.c $LDFLAGS $LDSTATIC
$CC -std=c99 $CFLAGS -o cmd/test cmd/test.c $LDFLAGS $LDSTATIC
$CC -std=c99 $CFLAGS -o cmd/tr cmd/tr.c lib/tr_str.c $LDFLAGS $LDSTATIC
diff --git a/cmd/printf.c b/cmd/printf.c
@@ -38,9 +38,10 @@
#define _POSIX_C_SOURCE 200809L
+#include "../lib/err.h"
+
#include <assert.h>
#include <ctype.h>
-#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
@@ -92,6 +93,8 @@ static char **myargv;
static char **gargv;
static char **maxargv;
+const char *argv0 = "printf";
+
int
main(int argc, char *argv[])
{
@@ -184,7 +187,7 @@ main(int argc, char *argv[])
if(end == 1)
{
- warnx("warning: missing format character");
+ warnx("missing format character");
#ifdef SHELL
INTON;
#endif
@@ -263,7 +266,7 @@ printf_doformat(char *fmt, int *rval)
int idx = atoi(fmt);
if(fargv == NULL)
{
- warnx("warning: incomplete use of n$");
+ warnx("incomplete use of n$");
return (NULL);
}
if(idx <= myargc)
@@ -278,7 +281,7 @@ printf_doformat(char *fmt, int *rval)
}
else if(fargv != NULL)
{
- warnx("warning: incomplete use of n$");
+ warnx("incomplete use of n$");
return (NULL);
}
@@ -317,7 +320,7 @@ printf_doformat(char *fmt, int *rval)
int idx = atoi(fmt);
if(fargv == NULL)
{
- warnx("warning: incomplete use of n$");
+ warnx("incomplete use of n$");
return (NULL);
}
if(idx <= myargc)
@@ -332,7 +335,7 @@ printf_doformat(char *fmt, int *rval)
}
else if(fargv != NULL)
{
- warnx("warning: incomplete use of n$");
+ warnx("incomplete use of n$");
return (NULL);
}
@@ -358,7 +361,7 @@ printf_doformat(char *fmt, int *rval)
haveprec = 0;
if(!*fmt)
{
- warnx("warning: missing format character");
+ warnx("missing format character");
return (NULL);
}
*dptr++ = *fmt;
@@ -380,7 +383,7 @@ printf_doformat(char *fmt, int *rval)
fmt++;
if(!strchr("aAeEfFgG", *fmt))
{
- warnx("warning: bad modifier L for %%%c", *fmt);
+ warnx("bad modifier L for %%%c", *fmt);
return (NULL);
}
}
@@ -411,7 +414,7 @@ printf_doformat(char *fmt, int *rval)
start[strlen(start) - 1] = 's';
if((p = strdup(getstr())) == NULL)
{
- warnx("warning: %s", strerror(ENOMEM));
+ warnx("%s", strerror(ENOMEM));
return (NULL);
}
getout = escape(p, 0, &len);
@@ -478,7 +481,7 @@ printf_doformat(char *fmt, int *rval)
break;
}
default:
- warnx("warning: illegal format character %c", convch);
+ warnx("illegal format character %c", convch);
return (NULL);
}
*fmt = nextch;
@@ -501,7 +504,7 @@ mknum(char *str, char ch)
assert(newlen != 0);
if((newcopy = realloc(copy, newlen)) == NULL)
{
- warnx("warning: %s", strerror(ENOMEM));
+ warnx("%s", strerror(ENOMEM));
return (NULL);
}
copy = newcopy;
@@ -644,7 +647,7 @@ getint(int *ip)
rval = 0;
if(val < INT_MIN || val > INT_MAX)
{
- warnx("warning: %s: %s", *gargv, strerror(ERANGE));
+ warnx("%s: %s", *gargv, strerror(ERANGE));
rval = 1;
}
*ip = (int)val;
@@ -678,17 +681,17 @@ getnum(intmax_t *ip, uintmax_t *uip, int signedconv)
*uip = strtoumax(*gargv, &ep, 0);
if(ep == *gargv)
{
- warnx("warning: %s: expected numeric value", *gargv);
+ warnx("%s: expected numeric value", *gargv);
rval = 1;
}
else if(*ep != '\0')
{
- warnx("warning: %s: not completely converted", *gargv);
+ warnx("%s: not completely converted", *gargv);
rval = 1;
}
if(errno == ERANGE)
{
- warnx("warning: %s: %s", *gargv, strerror(ERANGE));
+ warnx("%s: %s", *gargv, strerror(ERANGE));
rval = 1;
}
++gargv;
@@ -719,17 +722,17 @@ getfloating(long double *dp, int mod_ldbl)
*dp = strtod(*gargv, &ep);
if(ep == *gargv)
{
- warnx("warning: %s: expected numeric value", *gargv);
+ warnx("%s: expected numeric value", *gargv);
rval = 1;
}
else if(*ep != '\0')
{
- warnx("warning: %s: not completely converted", *gargv);
+ warnx("%s: not completely converted", *gargv);
rval = 1;
}
if(errno == ERANGE)
{
- warnx("warning: %s: %s", *gargv, strerror(ERANGE));
+ warnx("%s: %s", *gargv, strerror(ERANGE));
rval = 1;
}
++gargv;
diff --git a/lib/err.c b/lib/err.c
@@ -0,0 +1,88 @@
+// Collection of Unix tools, comparable to coreutils
+// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-License-Identifier: MPL-2.0
+
+#define _POSIX_C_SOURCE 200809L
+
+#include "err.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void
+vwarn(const char *fmt, va_list ap)
+{
+ fprintf(stderr, "%s: warning: ", argv0);
+ if(fmt)
+ {
+ vfprintf(stderr, fmt, ap);
+ fputs(": ", stderr);
+ }
+ perror(NULL);
+}
+
+void
+vwarnx(const char *fmt, va_list ap)
+{
+ fprintf(stderr, "%s: warning: ", argv0);
+ if(fmt) vfprintf(stderr, fmt, ap);
+ putc('\n', stderr);
+}
+
+_Noreturn void
+verr(int status, const char *fmt, va_list ap)
+{
+ fprintf(stderr, "%s: error: ", argv0);
+ if(fmt)
+ {
+ vfprintf(stderr, fmt, ap);
+ fputs(": ", stderr);
+ }
+ perror(NULL);
+ exit(status);
+}
+
+_Noreturn void
+verrx(int status, const char *fmt, va_list ap)
+{
+ fprintf(stderr, "%s: error: ", argv0);
+ if(fmt) vfprintf(stderr, fmt, ap);
+ putc('\n', stderr);
+ exit(status);
+}
+
+void
+warn(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarn(fmt, ap);
+ va_end(ap);
+}
+
+void
+warnx(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnx(fmt, ap);
+ va_end(ap);
+}
+
+_Noreturn void
+err(int status, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ verr(status, fmt, ap);
+ va_end(ap);
+}
+
+_Noreturn void
+errx(int status, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ verrx(status, fmt, ap);
+ va_end(ap);
+}
diff --git a/lib/err.h b/lib/err.h
@@ -0,0 +1,19 @@
+// Collection of Unix tools, comparable to coreutils
+// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-License-Identifier: MPL-2.0
+
+#include <stdarg.h>
+
+extern const char *argv0;
+
+void vwarn(const char *fmt, va_list ap);
+void vwarnx(const char *fmt, va_list ap);
+
+_Noreturn void verr(int status, const char *fmt, va_list ap);
+_Noreturn void verrx(int status, const char *fmt, va_list ap);
+
+void warn(const char *fmt, ...);
+void warnx(const char *fmt, ...);
+
+_Noreturn void err(int status, const char *fmt, ...);
+_Noreturn void errx(int status, const char *fmt, ...);