Skip to content

Commit

Permalink
beeuniq: added new helper beeuniq
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mariux committed Jul 21, 2011
1 parent 5973a6b commit 11c9050
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/bee-query.sh
/bee-remove.sh
/beecut
/beeuniq
/beefind.pl
/beesep
/beesh.sh
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 $^
Expand Down
119 changes: 119 additions & 0 deletions src/beeuniq/beeuniq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
** beeuniq - filter duplicate command line arguments
** Copyright (C) 2010-2011
** Marius Tolzmann <tolzmann@molgen.mpg.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>

#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 <tolzmann@molgen.mpg.de> 2011\n",
VERSION_MAJOR, VERSION_MINOR, VERSION_PATCHLVL);
}

void print_full_usage(void)
{
printf("usage: beeuniq [options] <string>\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);
}

0 comments on commit 11c9050

Please sign in to comment.