commit: 06f8c6580928c47958549885b1c5ca28c3e34f6c
parent 1b0ada13e1d439b12ad0d8653d698f0a155e9063
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 7 Dec 2024 04:49:17 +0100
cmd/expr: switch from <err.h> to ./lib/err.h
Diffstat:
2 files changed, 21 insertions(+), 20 deletions(-)
diff --git a/Makefile b/Makefile
@@ -180,9 +180,9 @@ cmd/realpath: cmd/realpath.c lib/fs.c lib/offline_realpath.c lib/fs.h Makefile
cmd/expr.tab.c: cmd/expr.y Makefile
$(YACC) -b cmd/expr cmd/expr.y
-cmd/expr: cmd/expr.tab.c Makefile
+cmd/expr: cmd/expr.tab.c lib/err.c lib/err.h Makefile
$(RM) -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno}
- $(CC) -std=c99 $(CFLAGS) -D_POSIX_C_SOURCE=200809L -o $@ cmd/expr.tab.c $(LDFLAGS) $(LDSTATIC)
+ $(CC) -std=c99 $(CFLAGS) -D_POSIX_C_SOURCE=200809L -o $@ cmd/expr.tab.c lib/err.c $(LDFLAGS) $(LDSTATIC)
cmd/cat: cmd/cat.c lib/fs.c lib/fs.h Makefile config.mk
$(RM) -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno}
diff --git a/cmd/expr.y b/cmd/expr.y
@@ -18,7 +18,7 @@
#include <assert.h>
#include <sys/types.h>
#include <ctype.h>
-#include <err.h>
+#include "../lib/err.h"
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
@@ -83,6 +83,8 @@ static void to_string(struct val *);
static int yyerror(const char *);
static int yylex(void);
+const char *argv0 = "expr";
+
%}
%union
@@ -131,7 +133,7 @@ make_integer(intmax_t i)
vp = (struct val *)malloc(sizeof(*vp));
if (vp == NULL)
- errx(ERR_EXIT, "error: malloc(vp) failed");
+ errx(ERR_EXIT, "malloc(vp) failed");
vp->type = integer;
vp->u.i = i;
@@ -145,10 +147,10 @@ make_str(const char *s)
vp = (struct val *)malloc(sizeof(*vp));
if (vp == NULL)
- errx(ERR_EXIT, "error: malloc(vp) failed");
+ errx(ERR_EXIT, "malloc(vp) failed");
if ((vp->u.s = strdup(s)) == NULL)
- errx(ERR_EXIT, "error: strdup(s) failed");
+ errx(ERR_EXIT, "strdup(s) failed");
if (is_integer(s))
vp->type = numeric_string;
@@ -191,9 +193,9 @@ static void
assert_to_integer(struct val *vp)
{
if (vp->type == string)
- errx(ERR_EXIT, "error: not a decimal number: '%s'", vp->u.s);
+ errx(ERR_EXIT, "not a decimal number: '%s'", vp->u.s);
if (!to_integer(vp))
- errx(ERR_EXIT, "error: operand too large: '%s'", vp->u.s);
+ errx(ERR_EXIT, "operand too large: '%s'", vp->u.s);
}
static void
@@ -212,7 +214,7 @@ to_string(struct val *vp)
#define NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1)
tmp = malloc(NDIGITS(vp->u.i));
if (tmp == NULL)
- errx(ERR_EXIT, "error: malloc() failed");
+ errx(ERR_EXIT, "malloc() failed");
sprintf(tmp, "%jd", vp->u.i);
vp->type = string;
@@ -295,14 +297,13 @@ main(int argc, char *argv[])
nonposix = 1;
break;
case ':':
- fprintf(stderr, "expr: error: Missing operand for option: '-%c'\n", optopt);
+ errx(1, "Missing operand for option: '-%c'\n", optopt);
break;
case '?':
- fprintf(stderr, "expr: error: Unrecognised option: '-%c'\n", optopt);
+ errx(1, "Unrecognised option: '-%c'\n", optopt);
break;
default:
- errx(ERR_EXIT,
- "usage: expr [-e] expression\n");
+ abort();
}
}
av = argv + optind;
@@ -326,7 +327,7 @@ main(int argc, char *argv[])
static int
yyerror(const char *s)
{
- errx(ERR_EXIT, "error: syntax error: %s", s);
+ errx(ERR_EXIT, "syntax error: %s", s);
}
static struct val *
@@ -426,7 +427,7 @@ assert_plus(intmax_t a, intmax_t b, intmax_t r)
*/
if ((a > 0 && b > 0 && r <= 0) ||
(a < 0 && b < 0 && r >= 0))
- errx(ERR_EXIT, "error: overflow");
+ errx(ERR_EXIT, "overflow");
}
static struct val *
@@ -449,7 +450,7 @@ assert_minus(intmax_t a, intmax_t b, intmax_t r)
{
if ((a >= 0 && b < 0 && r <= 0) ||
(a < 0 && b > 0 && r >= 0))
- errx(ERR_EXIT, "error: overflow");
+ errx(ERR_EXIT, "overflow");
}
static struct val *
@@ -487,7 +488,7 @@ assert_times(intmax_t a, intmax_t b, volatile intmax_t r)
* instead of a SIGFPE.
*/
if ((a == -1 && b == INTMAX_MIN) || (a != 0 && r / a != b))
- errx(ERR_EXIT, "error: overflow");
+ errx(ERR_EXIT, "overflow");
}
static struct val *
@@ -509,10 +510,10 @@ static void
assert_div(intmax_t a, intmax_t b)
{
if (b == 0)
- errx(ERR_EXIT, "error: division by zero");
+ errx(ERR_EXIT, "division by zero");
/* only INTMAX_MIN / -1 causes overflow */
if (a == INTMAX_MIN && b == -1)
- errx(ERR_EXIT, "error: overflow");
+ errx(ERR_EXIT, "overflow");
}
static struct val *
@@ -563,7 +564,7 @@ op_colon(struct val *a, struct val *b)
/* compile regular expression */
if ((eval = regcomp(&rp, b->u.s, 0)) != 0) {
regerror(eval, &rp, errbuf, sizeof(errbuf));
- errx(ERR_EXIT, "error: %s", errbuf);
+ errx(ERR_EXIT, "%s", errbuf);
}
/* compare string against pattern */