Skip to content

Commit

Permalink
beeuniq: added -d option to specify output delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed Jul 21, 2011
1 parent 11c9050 commit de46919
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/beeuniq/beeuniq.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define VERSION_MINOR 1
#define VERSION_PATCHLVL 0

#define OPT_DELIMITER 'd'
#define OPT_VERSION 'v'
#define OPT_HELP 'h'

Expand All @@ -40,7 +41,9 @@ void print_version(void)

void print_full_usage(void)
{
printf("usage: beeuniq [options] <string>\n");
printf("usage: beeuniq [options] <string>\n\n");
printf("options:\n\n");
printf(" -d | --delimiter <char> specify the outputdelimiter character\n\n");
}

char in(char *search, char *strings[], int max)
Expand Down Expand Up @@ -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},

Expand All @@ -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");
Expand All @@ -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');
Expand Down

0 comments on commit de46919

Please sign in to comment.