From 11c9050d8d124e4165cd4e604c8d3eb9b1e9560d Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Thu, 21 Jul 2011 10:49:49 +0200 Subject: [PATCH 1/3] beeuniq: added new helper beeuniq beeuniq removes duplicate arguments and prints them ./beeuniq ggg aaa bbb ccc aaa ddd would remove the second 'aaa' and just print: ggg aaa bbb ccc ddd --- .gitignore | 1 + Makefile | 6 ++- src/beeuniq/beeuniq.c | 119 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/beeuniq/beeuniq.c diff --git a/.gitignore b/.gitignore index d8c2c5a..e3b3f2c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ /bee-query.sh /bee-remove.sh /beecut +/beeuniq /beefind.pl /beesep /beesh.sh diff --git a/Makefile b/Makefile index 31641a9..bb8d2d9 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ DEFCONFDIR=${SYSCONFDIR}/default DESTDIR= -PROGRAMS_C=beeversion beesep beecut +PROGRAMS_C=beeversion beesep beecut beeuniq PROGRAMS_SHELL=bee beesh PROGRAMS_PERL=beefind.pl @@ -71,6 +71,10 @@ beecut: src/beecut/beecut.c @echo "linking $@ .." @gcc -Wall -o $@ $^ +beeuniq: src/beeuniq/beeuniq.c + @echo "linking $@ .." + @gcc -Wall -o $@ $^ + %.o: %.c @echo "compiling $@ .." @gcc -Wall -o $@ -c $^ diff --git a/src/beeuniq/beeuniq.c b/src/beeuniq/beeuniq.c new file mode 100644 index 0000000..213a31b --- /dev/null +++ b/src/beeuniq/beeuniq.c @@ -0,0 +1,119 @@ +/* +** beeuniq - filter duplicate command line arguments +** Copyright (C) 2010-2011 +** Marius Tolzmann +** +** This file is part of Bee. +** +** Bee 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 + +#define VERSION_MAJOR 0 +#define VERSION_MINOR 1 +#define VERSION_PATCHLVL 0 + +#define OPT_VERSION 'v' +#define OPT_HELP 'h' + +void print_version(void) +{ + printf("beeuniq v%d.%d.%d - " + "by Marius Tolzmann 2011\n", + VERSION_MAJOR, VERSION_MINOR, VERSION_PATCHLVL); +} + +void print_full_usage(void) +{ + printf("usage: beeuniq [options] \n"); +} + +char in(char *search, char *strings[], int max) +{ + int i; + + for(i=0; i < max ; i++) + if(!strcmp(search, strings[i])) + return 1; + + return 0; +} + +int bee_uniq(int count, char *strings[]) +{ + int i = 0; + int ret; + + for(i=1, ret=count; i < ret ; i++) { + if(!in(strings[i], strings, i)) + continue; + + ret--; + memmove(&strings[i], &strings[i+1], sizeof(*strings)*(ret-i)); + i--; + } + + return ret; +} + +int main(int argc, char *argv[]) +{ + int option_index = 0; + int c = 0; + int max, i; + + struct option long_options[] = { + {"version", no_argument, 0, OPT_VERSION}, + {"help", no_argument, 0, OPT_HELP}, + + {0, 0, 0, 0} + }; + + while ((c = getopt_long_only(argc, argv, "hv", long_options, &option_index)) != -1) { + + switch (c) { + case OPT_HELP: + print_version(); + printf("\n"); + print_full_usage(); + printf("\n"); + exit(EXIT_SUCCESS); + + case OPT_VERSION: + print_version(); + exit(EXIT_SUCCESS); + } + } /* end while getopt_long_only */ + + if(argc-optind < 1) { + print_full_usage(); + exit(EXIT_FAILURE); + } + + max = bee_uniq(argc-optind, &argv[optind]); + + for(i=optind; i <= max; i++) { + fputs(argv[i], stdout); + if(max-i) + putchar(' '); + } + + putchar('\n'); + + return(EXIT_SUCCESS); +} From de469190e83d2d66de34edbf95cc1c9dca3dc4bf Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Thu, 21 Jul 2011 11:36:28 +0200 Subject: [PATCH 2/3] beeuniq: added -d option to specify output delimiter --- src/beeuniq/beeuniq.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/beeuniq/beeuniq.c b/src/beeuniq/beeuniq.c index 213a31b..159872d 100644 --- a/src/beeuniq/beeuniq.c +++ b/src/beeuniq/beeuniq.c @@ -28,6 +28,7 @@ #define VERSION_MINOR 1 #define VERSION_PATCHLVL 0 +#define OPT_DELIMITER 'd' #define OPT_VERSION 'v' #define OPT_HELP 'h' @@ -40,7 +41,9 @@ void print_version(void) void print_full_usage(void) { - printf("usage: beeuniq [options] \n"); + printf("usage: beeuniq [options] \n\n"); + printf("options:\n\n"); + printf(" -d | --delimiter specify the outputdelimiter character\n\n"); } char in(char *search, char *strings[], int max) @@ -77,7 +80,11 @@ int main(int argc, char *argv[]) int c = 0; int max, i; + char delimiter = ' '; + struct option long_options[] = { + {"delimiter", required_argument, 0, OPT_DELIMITER}, + {"version", no_argument, 0, OPT_VERSION}, {"help", no_argument, 0, OPT_HELP}, @@ -87,6 +94,14 @@ int main(int argc, char *argv[]) while ((c = getopt_long_only(argc, argv, "hv", long_options, &option_index)) != -1) { switch (c) { + case OPT_DELIMITER: + if (!optarg[0] || optarg[1]) { + fprintf(stderr, "invalid delimiter '%s'\n", optarg); + exit(EXIT_FAILURE); + } + delimiter = optarg[0]; + break; + case OPT_HELP: print_version(); printf("\n"); @@ -105,12 +120,13 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - max = bee_uniq(argc-optind, &argv[optind]); + max = bee_uniq(argc-optind, &argv[optind]); + max += optind-1; for(i=optind; i <= max; i++) { fputs(argv[i], stdout); if(max-i) - putchar(' '); + putchar(delimiter); } putchar('\n'); From df2a7128ef27d12238a8faca435dee49c83ab171 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Thu, 21 Jul 2011 11:41:07 +0200 Subject: [PATCH 3/3] config-lib: use beeuniq to remove dupes from pathes --- src/beelib.config.sh.in | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/beelib.config.sh.in b/src/beelib.config.sh.in index 7db42c3..bca4fcb 100644 --- a/src/beelib.config.sh.in +++ b/src/beelib.config.sh.in @@ -45,6 +45,10 @@ function config_init_base_config() { # append bee installed defaults to config search path : ${BEE_CONFIG_DIR:=${BEE_DEFCONFDIR}} XDG_CONFIG_DIRS=${XDG_CONFIG_DIRS}:${BEE_CONFIG_DIR} + + # remove dupes from pathes + XDG_CONFIG_DIRS=$(beeuniq -d : ${XDG_CONFIG_DIRS//:/ }) + XDG_DATA_DIRS=$(beeuniq -d : ${XDG_DATA_DIRS//:/ }) } function config_handle_deprecated_config() {