From f3893c4da1fb937a0b5f388198cfe20b56f2a0bd Mon Sep 17 00:00:00 2001 From: DTB Date: Thu, 18 Apr 2024 22:45:11 -0600 Subject: [PATCH] retval(1): make C and Rust code similar --- retval/retval.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/retval/retval.c b/retval/retval.c index b04da71..6baca45 100644 --- a/retval/retval.c +++ b/retval/retval.c @@ -3,21 +3,22 @@ #include /* strtol(3) */ #include /* EX_USAGE */ -static char *program_name = "retval"; +int usage(char *s){ + fprintf(stderr, "Usage: %s [status]\n", s == NULL ? "retval" : s); + return EX_USAGE; +} int main(int argc, char *argv[]){ - unsigned int s; - if(argc < 2) - goto usage; - - errno = 0; - s = strtol(argv[1], &argv[1], 10); - if(*argv[1] != '\0' || errno != 0){ -usage: fprintf(stderr, "Usage: %s [status]\n", - argv[0] == NULL ? program_name : argv[0]); - return EX_USAGE; + switch(argc){ + case 2: { unsigned int s; + errno = 0; + s = strtol(argv[1], &argv[1], 10); + if(*argv[1] != '\0' || errno != 0){ + return usage(argv[0]); + } + return s; + } + default: return usage(argv[0]); } - - return s; }