From 4e9746c31473163b1abe868ec0e9bbef24d40cb1 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 30 Nov 2014 16:49:39 +0100 Subject: [PATCH] Add yes(1). --- doc/user-guide | 1 + utils/.gitignore | 1 + utils/Makefile | 1 + utils/yes.cpp | 108 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 utils/yes.cpp diff --git a/doc/user-guide b/doc/user-guide index 68092fd3..f9247f7b 100644 --- a/doc/user-guide +++ b/doc/user-guide @@ -195,6 +195,7 @@ Sortix comes with a number of home-made programs. Here is an overview: * `uptime` - time since initialization * `wc` - count words and lines * `which` - find path to command +* `yes` - repeatedly output a line Third Party Software -------------------- diff --git a/utils/.gitignore b/utils/.gitignore index 42ece0d8..314a0aa9 100644 --- a/utils/.gitignore +++ b/utils/.gitignore @@ -47,3 +47,4 @@ uptime wc which xinstall +yes diff --git a/utils/Makefile b/utils/Makefile index e8f9809d..4deec737 100644 --- a/utils/Makefile +++ b/utils/Makefile @@ -58,6 +58,7 @@ uniq \ uptime \ wc \ which \ +yes \ BINARIES=\ $(BINARIES_EXCEPT_INSTALL) \ diff --git a/utils/yes.cpp b/utils/yes.cpp new file mode 100644 index 00000000..4c554165 --- /dev/null +++ b/utils/yes.cpp @@ -0,0 +1,108 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2014. + + 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 . + + yes.cpp + Repeatedly output a line. + +*******************************************************************************/ + +#include +#include +#include + +static void compact_arguments(int* argc, char*** argv) +{ + for ( int i = 0; i < *argc; i++ ) + { + while ( i < *argc && !(*argv)[i] ) + { + for ( int n = i; n < *argc; n++ ) + (*argv)[n] = (*argv)[n+1]; + (*argc)--; + } + } +} + +static void help(FILE* fp, const char* argv0) +{ + fprintf(fp, "Usage: %s [STRING]...\n", argv0); + fprintf(fp, " or: %s OPTION\n", argv0); + fprintf(fp, "Repeatedly output a line with all specified STRING(s), or 'y'.\n"); + fprintf(fp, "\n"); + fprintf(fp, " --help display this help and exit\n"); + fprintf(fp, " --version output version information and exit\n"); +} + +static void version(FILE* fp, const char* argv0) +{ + fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR); + fprintf(fp, "License GPLv3+: GNU GPL version 3 or later .\n"); + fprintf(fp, "This is free software: you are free to change and redistribute it.\n"); + fprintf(fp, "There is NO WARRANTY, to the extent permitted by law.\n"); +} + +int main(int argc, char* argv[]) +{ + const char* argv0 = argv[0]; + for ( int i = 1; i < argc; i++ ) + { + const char* arg = argv[i]; + if ( arg[0] != '-' || !arg[1] ) + continue; + argv[i] = NULL; + if ( !strcmp(arg, "--") ) + break; + if ( arg[1] != '-' ) + { + while ( char c = *++arg ) switch ( c ) + { + default: + fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); + help(stderr, argv0); + exit(1); + } + } + else if ( !strcmp(arg, "--help") ) + help(stdout, argv0), exit(0); + else if ( !strcmp(arg, "--version") ) + version(stdout, argv0), exit(0); + else + { + fprintf(stderr, "%s: unknown option: %s\n", argv0, arg); + help(stderr, argv0); + exit(1); + } + } + + compact_arguments(&argc, &argv); + + while ( true ) + { + if ( argc == 1 ) + { + printf("y"); + } + else + { + for ( int i = 1; i < argc; i++ ) + printf("%s%s", 1 < i ? " " : "", argv[i]); + } + printf("\n"); + } + + return 0; +}