commit: d3e79ea42c4d17fde77f719c79d4763ddc74dad8
parent 43095555a518d8245c42efb53140438f96a35a06
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 20 Sep 2024 03:26:15 +0200
cmd/nohup: unify error message formatting
Diffstat:
1 file changed, 21 insertions(+), 12 deletions(-)
diff --git a/cmd/nohup.c b/cmd/nohup.c
@@ -16,6 +16,8 @@
#include <sys/stat.h> // S_IRUSR, S_IWUSR
#include <unistd.h> // isatty
+const char *argv0 = "nohup";
+
static void
usage(void)
{
@@ -35,7 +37,7 @@ main(int argc, char *argv[])
if(signal(SIGHUP, SIG_IGN) == SIG_ERR)
{
- fprintf(stderr, "nohup: Failed to set SIGHUP as ignored: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed to set SIGHUP as ignored: %s\n", argv0, strerror(errno));
return 127;
}
@@ -47,38 +49,44 @@ main(int argc, char *argv[])
if(nohup_fd < 0)
{
fprintf(stderr,
- "nohup: Warning: Failed opening ./nohup.out (%s), fallbacking to ~/nohup.out\n",
+ "%s: warning: Failed opening ./nohup.out (%s), fallbacking to ~/nohup.out\n",
+ argv0,
strerror(errno));
char *home = getenv("HOME");
if(!home)
{
- fprintf(
- stderr,
- "nohup: $HOME is undefined, can't fallback writing from ./nohup.out to ~/nohup.out\n");
+ fprintf(stderr,
+ "%s: error: $HOME is undefined, can't fallback writing from ./nohup.out to "
+ "~/nohup.out\n",
+ argv0);
return 127;
}
char home_nohup[PATH_MAX];
if(snprintf(home_nohup, sizeof(home_nohup), "%s/nohup.out", home) < 0)
{
- fprintf(
- stderr, "nohup: Failed concatenating $HOME and '/nohup.out': %s\n", strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed concatenating $HOME and '/nohup.out': %s\n",
+ argv0,
+ strerror(errno));
return 127;
}
nohup_fd = open(home_nohup, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
if(nohup_fd < 0)
{
- fprintf(stderr, "nohup: Error: Failed opening ~/nohup.out: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed opening ~/nohup.out: %s\n", argv0, strerror(errno));
return 127;
}
}
if(dup2(nohup_fd, 1) < 0)
{
- fprintf(
- stderr, "nohup: Error: Failed assigning 'nohup.out' to stdout: %s\n", strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed assigning 'nohup.out' to stdout: %s\n",
+ argv0,
+ strerror(errno));
return 127;
}
@@ -87,7 +95,8 @@ main(int argc, char *argv[])
assert(errno == 0);
if(dup2(1, 2))
{
- fprintf(stderr, "nohup: Error: Failed assigning stdout to stderr: %s\n", strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed assigning stdout to stderr: %s\n", argv0, strerror(errno));
return 127;
}
}
@@ -105,7 +114,7 @@ main(int argc, char *argv[])
assert(errno == 0);
if(execvp(argv[0], argv) < 0)
{
- fprintf(stderr, "nohup: execvp(\"%s\", ...): %s\n", argv[0], strerror(errno));
+ fprintf(stderr, "%s: error: Failed executing '%s': %s\n", argv0, argv[0], strerror(errno));
return (errno == ENOENT) ? 127 : 126;
}