logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: 4585f387e2fde87a7cfbf56dfc9b9d6defcad175
parent 31c3b457c8c47b3fc56d998b9f57b875e7e28d03
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 17 Jan 2025 12:14:52 +0100

cmd/date: use `return 1` instead of exit()

Diffstat:

Mcmd/date.c47+++++++++++++++++++++++------------------------
1 file changed, 23 insertions(+), 24 deletions(-)

diff --git a/cmd/date.c b/cmd/date.c @@ -12,7 +12,7 @@ #include <locale.h> /* setlocale() */ #include <stdbool.h> #include <stdio.h> /* BUFSIZ, fprintf(), puts() */ -#include <stdlib.h> /* exit(), strtol() */ +#include <stdlib.h> /* strtol() */ #include <string.h> /* strerror */ #include <time.h> /* time, localtime, tm, strftime, strptime, clock_settime */ #include <unistd.h> /* getopt(), optarg, optind */ @@ -53,7 +53,6 @@ main(int argc, char *argv[]) const char *input_format = NULL; int uflag = 0; int dflag = 0, rflag = 0; - int c; bool jflag = false; bool settime = false; @@ -64,7 +63,7 @@ main(int argc, char *argv[]) if(tp.tv_sec == (time_t)-1) { fprintf(stderr, "%s: error: Failed getting current time: %s\n", argv0, strerror(errno)); - exit(EXIT_FAILURE); + return 1; } for(int c = -1; (c = getopt(argc, argv, ":d:f:jr:Ru")) != -1;) @@ -76,12 +75,12 @@ main(int argc, char *argv[]) if(input_format != NULL) { fprintf(stderr, "%s: error: Cannot both use '-d' and '-f'\n", argv0); - exit(EXIT_FAILURE); + return 1; } if(rflag == 1) { fprintf(stderr, "%s: error: Cannot both use '-d' and '-r'\n", argv0); - exit(EXIT_FAILURE); + return 1; } iso_parse(optarg, &tm, &tp.tv_nsec, &errstr); @@ -89,14 +88,14 @@ main(int argc, char *argv[]) if(errstr != NULL) { fprintf(stderr, "%s: error: iso_parse(\"%s\", …): %s\n", argv0, optarg, errstr); - exit(EXIT_FAILURE); + return 1; } tp.tv_sec = mktime_tz(&tm); if(tp.tv_sec == (time_t)-1) { fprintf(stderr, "%s: error: mktime: %s\n", argv0, strerror(errno)); - exit(EXIT_FAILURE); + return 1; } errno = 0; @@ -105,12 +104,12 @@ main(int argc, char *argv[]) if(dflag == 1) { fprintf(stderr, "%s: error: Cannot both use '-d' and '-f'\n", argv0); - exit(EXIT_FAILURE); + return 1; } if(rflag == 1) { fprintf(stderr, "%s: error: Cannot both use '-f' and '-r'\n", argv0); - exit(EXIT_FAILURE); + return 1; } input_format = optarg; @@ -121,12 +120,12 @@ main(int argc, char *argv[]) if(input_format != NULL) { fprintf(stderr, "%s: error: Cannot both use '-f' and '-r'\n", argv0); - exit(EXIT_FAILURE); + return 1; } if(dflag == 1) { fprintf(stderr, "%s: error: Cannot both use '-d' and '-r'\n", argv0); - exit(EXIT_FAILURE); + return 1; } char *endptr = NULL; @@ -135,12 +134,12 @@ main(int argc, char *argv[]) if(errno != 0) { fprintf(stderr, "%s: error: Failed parsing '-r %s'\n", argv0, optarg); - exit(EXIT_FAILURE); + return 1; } if(!(endptr == NULL || *endptr == '\0')) { fprintf(stderr, "%s: error: Invalid characters in '-r %s': %s\n", argv0, optarg, endptr); - exit(EXIT_FAILURE); + return 1; } break; } @@ -174,7 +173,7 @@ main(int argc, char *argv[]) if(gmtime_r(&tp.tv_sec, &tm) == NULL) { fprintf(stderr, "%s: error: gmtime_r: %s\n", argv0, strerror(errno)); - exit(EXIT_FAILURE); + return 1; } } else @@ -182,7 +181,7 @@ main(int argc, char *argv[]) if(localtime_r(&tp.tv_sec, &tm) == NULL) { fprintf(stderr, "%s: error: localtime_r: %s\n", argv0, strerror(errno)); - exit(EXIT_FAILURE); + return 1; } } @@ -197,7 +196,7 @@ main(int argc, char *argv[]) argv0, argv[0], input_format); - exit(EXIT_FAILURE); + return 1; } tp.tv_sec = mktime_tz(&tm); @@ -205,7 +204,7 @@ main(int argc, char *argv[]) if(tp.tv_sec == (time_t)-1) { fprintf(stderr, "%s: error: mktime: %s\n", argv0, strerror(errno)); - exit(EXIT_FAILURE); + return 1; } errno = 0; @@ -220,7 +219,7 @@ main(int argc, char *argv[]) if(s == NULL) { fprintf(stderr, "%s: error: strptime(\"%s\", \"%s\", …) returned NULL\n", argv0, *argv, fmt); - exit(EXIT_FAILURE); + return 1; } size_t rem = strlen(s); @@ -240,7 +239,7 @@ main(int argc, char *argv[]) argv0, rem, fmt); - exit(EXIT_FAILURE); + return 1; } s = strptime(s, fmt, &tm); @@ -248,7 +247,7 @@ main(int argc, char *argv[]) { fprintf( stderr, "%s: error: strptime(\"%s\", \"%s\", …) returned NULL\n", argv0, *argv, fmt); - exit(EXIT_FAILURE); + return 1; } } @@ -257,7 +256,7 @@ main(int argc, char *argv[]) if(tp.tv_sec == (time_t)-1) { fprintf(stderr, "%s: error: mktime: %s\n", argv0, strerror(errno)); - exit(EXIT_FAILURE); + return 1; } errno = 0; @@ -276,7 +275,7 @@ main(int argc, char *argv[]) tp.tv_sec, tp.tv_nsec, strerror(errno)); - exit(EXIT_FAILURE); + return 1; } } @@ -292,13 +291,13 @@ main(int argc, char *argv[]) if(strftime(outstr, sizeof(outstr), format, &tm) == 0 && errno != 0) { fprintf(stderr, "%s: error: Failed formatting time: %s\n", argv0, strerror(errno)); - exit(EXIT_FAILURE); + return 1; } if(puts(outstr) < 0) { fprintf(stderr, "%s: error: Failed writing time: %s\n", argv0, strerror(errno)); - exit(EXIT_FAILURE); + return 1; } return 0;