From 8d3a16c68c7698605ca1453b348e7282290d5f17 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Thu, 7 Jul 2011 00:15:54 +0200 Subject: [PATCH] beecut: added --newline to print each element on a seperate line --- src/beecut/beecut.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/beecut/beecut.c b/src/beecut/beecut.c index a297da8..795aed2 100644 --- a/src/beecut/beecut.c +++ b/src/beecut/beecut.c @@ -31,6 +31,7 @@ #define OPT_SHORT 's' #define OPT_PREPEND 'p' #define OPT_APPEND 'a' +#define OPT_NEWLINE 'n' #define OPT_VERSION 128 #define OPT_HELP 129 @@ -49,7 +50,8 @@ void print_full_usage(void) printf(" -d | --delimeter specify the delimeter character\n\n"); - printf(" -s | --short output short elements\n\n"); + printf(" -s | --short output short elements\n"); + printf(" -n | --newline output each element on a seperate line\n\n"); printf(" -p | --prepend prepend to each output element\n"); printf(" -a | --append append to each output element\n\n"); @@ -61,9 +63,12 @@ void print_full_usage(void) printf(" beecut -s -d '-' a-b-c will print 'a-b-c a b c'\n\n"); } -void cut_and_print(char *string, char delimeter, char opt_short, char *prefix, char *suffix) +void cut_and_print(char *string, char delimeter, char opt_short, char opt_newline, char *prefix, char *suffix) { char *p, *s; + char nl; + + nl = opt_newline ? '\n' : ' '; p = s = string; @@ -71,7 +76,7 @@ void cut_and_print(char *string, char delimeter, char opt_short, char *prefix, c while((p=strchr(p, delimeter))) { if(s < p) /* only print space if we have something to print */ - printf("%s %s", suffix, prefix); + printf("%s%c%s", suffix, nl, prefix); while(s < p) putchar(*(s++)); @@ -81,7 +86,7 @@ void cut_and_print(char *string, char delimeter, char opt_short, char *prefix, c s = (opt_short) ? p : string; } - printf("%s %s%s%s\n", suffix, prefix, s, suffix); + printf("%s%c%s%s%s\n", suffix, nl, prefix, s, suffix); } int main(int argc, char *argv[]) @@ -91,7 +96,8 @@ int main(int argc, char *argv[]) char delimeter = '.'; - char opt_short = 0; + char opt_short = 0; + char opt_newline = 0; char *opt_prepend = ""; char *opt_append = opt_prepend; @@ -103,6 +109,7 @@ int main(int argc, char *argv[]) {"append", required_argument, 0, OPT_APPEND}, {"short", no_argument, 0, OPT_SHORT}, + {"newline", no_argument, 0, OPT_NEWLINE}, {"version", no_argument, 0, OPT_VERSION}, {"help", no_argument, 0, OPT_HELP}, @@ -110,7 +117,7 @@ int main(int argc, char *argv[]) {0, 0, 0, 0} }; - while ((c = getopt_long_only(argc, argv, "p:a:d:s", long_options, &option_index)) != -1) { + while ((c = getopt_long_only(argc, argv, "p:a:d:sn", long_options, &option_index)) != -1) { switch (c) { case OPT_DELIMETER: @@ -133,6 +140,10 @@ int main(int argc, char *argv[]) opt_short = 1; break; + case OPT_NEWLINE: + opt_newline = 1; + break; + case OPT_HELP: printf("\n"); print_version(); @@ -152,8 +163,8 @@ int main(int argc, char *argv[]) } while(optind < argc) - cut_and_print(argv[optind++], delimeter, opt_short, opt_prepend, opt_append); - + cut_and_print(argv[optind++], delimeter, opt_short, + opt_newline, opt_prepend, opt_append); return(0); }