commit: 941c815f0ad8aef6b7e3cce18209abd42c39dc4c
parent 5d44cf475cc204a07d67a4f400b46f2f04603940
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 20 Sep 2024 04:28:13 +0200
cmd/time: unify error message formatting
Diffstat:
2 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/cmd/time.c b/cmd/time.c
@@ -6,6 +6,7 @@
#include <errno.h> // errno
#include <stdio.h> // perror, fprintf
#include <stdlib.h> // abort
+#include <string.h> // strerror
#include <sys/times.h> // times
#include <sys/wait.h> // waitpid
#include <unistd.h> // sysconf, fork, execvp, getopt
@@ -36,11 +37,11 @@ main(int argc, char *argv[])
case 'p': // POSIX format (default)
break;
case ':':
- fprintf(stderr, "time: Error: Missing operand for option: '-%c'\n", optopt);
+ fprintf(stderr, "time: error: Missing operand for option: '-%c'\n", optopt);
usage();
return 1;
case '?':
- fprintf(stderr, "time: Error: Unrecognised option: '-%c'\n", optopt);
+ fprintf(stderr, "time: error: Unrecognised option: '-%c'\n", optopt);
usage();
return 1;
}
@@ -53,14 +54,14 @@ main(int argc, char *argv[])
long ticks = sysconf(_SC_CLK_TCK);
if(ticks <= 0)
{
- perror("time: sysconf(_SC_CLK_TCK)");
+ perror("time: error: sysconf(_SC_CLK_TCK)");
return 1;
}
clock_t t0 = times(&tms);
if(t0 == (clock_t)-1)
{
- perror("time: times");
+ perror("time: error: times");
return 1;
}
@@ -69,12 +70,12 @@ main(int argc, char *argv[])
switch(pid)
{
case -1:
- perror("time: fork");
+ perror("time: error: fork");
return 1;
case 0:
execvp(argv[0], argv);
ret = 126 + (errno == ENOENT);
- perror("time: execvp");
+ fprintf(stderr, "time: error: Failed executing '%s': %s\n", argv[0], strerror(errno));
return ret;
default:
break;
@@ -86,13 +87,13 @@ main(int argc, char *argv[])
int t1 = times(&tms);
if(t1 == (clock_t)-1)
{
- perror("time: times");
+ perror("time: error: times");
return 1;
}
if(WIFSIGNALED(status))
{
- fprintf(stderr, "time: Command terminated by signal %d\n", WTERMSIG(status));
+ fprintf(stderr, "time: error: Command terminated by signal %d\n", WTERMSIG(status));
ret = 128 + WTERMSIG(status);
}
diff --git a/test-cmd/time.t b/test-cmd/time.t
@@ -8,12 +8,12 @@
Usage: time command [argument ...]
$ ./time -f
- time: Error: Unrecognised option: '-f'
+ time: error: Unrecognised option: '-f'
Usage: time command [argument ...]
[1]
$ ./time /var/empty/e/no/ent
- time: execvp: No such file or directory
+ time: error: Failed executing '/var/empty/e/no/ent': No such file or directory
real 0.[0-9]* (re)
user 0.[0-9]* (re)
sys 0.[0-9]* (re)