Mega Code Archive

 
Categories / C / Small Application
 

Translate all DOTS -056, except for the last one

#include <stdio.h> #include <getopt.h> #include <string.h> #define PACKAGE "dedot" #define VERSION "1.0.0" #define MAXLINE 1024 void print_help(int exval); int main(int argc, char *argv[]) { char line[MAXLINE]; char *ptr = NULL; /* points to the last dot */ char sub_char[1]; /* substidute char */ int sub_set = 0; /* substitude set ? */ int all_flag = 0; /* print all lines, even the ones not subject to change */ /* 1=on, 0=off */ int i = 0; int opt; while((opt = getopt(argc, argv, "hac:")) != -1) { switch(opt) { case 'h': print_help(0); break; case 'a': all_flag = 1; break; case 'c': sub_set = 1; strncpy(sub_char, optarg, 1); break; case ':': fprintf(stderr, "%s: Option `%c' needs an argument `-c CHAR' ?\n\n", PACKAGE, optopt); print_help(1); break; case '?': fprintf(stderr, "%s: No such option `%c'\n\n", PACKAGE, optopt); print_help(1); break; } /* switch */ } /* while */ while((fgets(line, 1024, stdin)) != NULL) { if((ptr = strrchr(line, '.')) == NULL) { if(all_flag == 1) printf("%s", line); continue; } for(i = 0; i < strlen(line); i++) if(sub_set == 1 && line[i] == '.' && &line[i] != ptr) printf("%c", sub_char[0]); else if(line[i] != '.' || &line[i] == ptr) printf("%c", line[i]); } return 0; } void print_help(int exval) { printf("%s,%s remove every dot `.' exept for the last one\n", PACKAGE, VERSION); printf("%s [-h] [-c CHAR]\n\n", PACKAGE); printf(" -h print this help and exit\n"); printf(" -c CHAR substitude all dots EXCEPT the last one with character `CHAR'\n"); printf(" -a print all lines, even the ones which are not subject of change\n\n"); exit(exval); }