From 2b86712890c5f94677c7efc5ca43c6aff9b174a9 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sat, 25 Nov 2017 01:27:42 +0100 Subject: [PATCH] util: Add a (long) integer-only seq command Signed-off-by: Tormod Volden --- Applications/util/Makefile.6809 | 1 + Applications/util/Makefile.z80 | 1 + Applications/util/fuzix-util.pkg | 1 + Applications/util/seq.c | 102 +++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 Applications/util/seq.c diff --git a/Applications/util/Makefile.6809 b/Applications/util/Makefile.6809 index db28d7da..264606a3 100644 --- a/Applications/util/Makefile.6809 +++ b/Applications/util/Makefile.6809 @@ -90,6 +90,7 @@ SRCS = \ ps.c \ remount.c \ sed.c \ + seq.c \ sleep.c \ ssh.c \ socktest.c \ diff --git a/Applications/util/Makefile.z80 b/Applications/util/Makefile.z80 index 2aa264fc..fac4131f 100644 --- a/Applications/util/Makefile.z80 +++ b/Applications/util/Makefile.z80 @@ -86,6 +86,7 @@ SRCS = banner.c \ passwd.c \ ps.c \ remount.c \ + seq.c \ setdate.c \ sleep.c \ ssh.c \ diff --git a/Applications/util/fuzix-util.pkg b/Applications/util/fuzix-util.pkg index a588bb43..5adc6be3 100644 --- a/Applications/util/fuzix-util.pkg +++ b/Applications/util/fuzix-util.pkg @@ -85,6 +85,7 @@ f 0755 /bin/prtroot prtroot f 0755 /bin/reboot reboot l /bin/reboot /bin/halt f 0755 /bin/sed sed +f 0755 /bin/seq seq f 0755 /bin/sleep sleep f 0755 /bin/sort sort f 0755 /bin/sum sum diff --git a/Applications/util/seq.c b/Applications/util/seq.c new file mode 100644 index 00000000..8e286c7b --- /dev/null +++ b/Applications/util/seq.c @@ -0,0 +1,102 @@ +/* + * Integer-only implementation of seq + * + * Copyright 2017 Tormod Volden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#include +#include +#include +#include + +static const char *appname; +static const char *newline = "\n"; + +static void error(void) +{ + fprintf(stderr, "%s: [-s SEP] [start] [step] end\n", appname); + exit(1); +} + +static long chk_strtol(const char *s) +{ + long value; + char *end; + + errno = 0; + value = strtol(s, &end, 0); + if (errno || *end != 0) { + fprintf(stderr, "Error: Bad numeral: %s\n", s); + error(); + } + return value; +} + +int main(int argc, char **argv) +{ + char **argp = argv; + appname = argv[0]; + long start = 1; + long step = 1; + long end; + const char *sep = newline; + bool printed = false; + + while(*++argp && (*argp)[0] == '-' + && ((*argp)[1] < '0' || (*argp)[1] > '9')) { + --argc; + switch((*argp)[1]) { + case 's': + if ((*argp)[2]) { + sep = &(*argp)[2]; + } else { + sep = *++argp; + --argc; + } + break; + default: + error(); + } + } + switch(argc) { + case 2: + end = chk_strtol(argp[0]); + break; + case 3: + start = chk_strtol(argp[0]); + end = chk_strtol(argp[1]); + break; + case 4: + start = chk_strtol(argp[0]); + step = chk_strtol(argp[1]); + end = chk_strtol(argp[2]); + break; + default: + error(); + } + + while ((start <= end && step > 0) || (start >= end && step < 0)) { + if (printed) + printf("%s", sep); + else + printed = true; + printf("%ld", start); + start += step; + } + if (printed) + putchar('\n'); + return 0; +} -- 2.34.1