commit: 435781c2370fcf51ded746841896b58cf1811d50
parent 1b52c64ecaaa22fb28dc4cab35e0587fc9523dd1
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sun, 20 Jul 2025 07:36:23 +0200
cmd/time: switch to posix_spawnp
I guess I didn't read whole "Errors" section, posix_spawnp errors
similarly to fork()+exec().
Time statistics also don't end up printed on time(1) error anymore
since now the parent exits immediately instead of the child exiting
and then parent copying the return value.
Diffstat:
2 files changed, 10 insertions(+), 18 deletions(-)
diff --git a/cmd/time.c b/cmd/time.c
@@ -5,14 +5,17 @@
#define _POSIX_C_SOURCE 200809L
#include "../libutils/getopt_nolong.h"
-#include <errno.h> // errno
+#include <errno.h>
+#include <spawn.h>
#include <stdio.h> // perror, fprintf
#include <stdlib.h> // abort
#include <string.h> // strerror
#include <sys/resource.h> // getrusage
-#include <sys/times.h> // times
-#include <sys/wait.h> // waitpid
-#include <unistd.h> // sysconf, fork, execvp, getopt
+#include <sys/times.h>
+#include <sys/wait.h> // waitpid
+#include <unistd.h> // sysconf, getopt
+
+extern char **environ;
const char *argv0 = "time";
@@ -80,24 +83,16 @@ main(int argc, char *argv[])
return 1;
}
- // Note: posix_spawnp seems to lack enough error information
- pid_t pid = fork();
- switch(pid)
+ pid_t child = -1;
+ if(posix_spawnp(&child, argv[0], NULL, NULL, argv, environ) != 0)
{
- case -1:
- perror("time: error: fork");
- return 1;
- case 0:
- execvp(argv[0], argv);
ret = 126 + (errno == ENOENT);
fprintf(stderr, "time: error: Failed executing '%s': %s\n", argv[0], strerror(errno));
return ret;
- default:
- break;
}
int status = 0;
- waitpid(pid, &status, 0);
+ waitpid(child, &status, 0);
int t1 = times(&tms);
if(t1 == (clock_t)-1)
diff --git a/test-cmd/time.t b/test-cmd/time.t
@@ -14,9 +14,6 @@
$ ./time /var/empty/e/no/ent
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)
[127]
$ ./time false