### #:/dev/zero v2.0 http://pastebin.skddev.de ---------------------------------------------------------- /* wordcount.c */ #include #include #include #include #define size 512 int main(int argc, char *argv[]) { int cflag = 0; int lflag = 0; /* int index; */ int c; char buffer[size]; int count_lines, count_char, i; count_lines = 0; count_char = 0; opterr = 0; while ((c = getopt (argc, argv, "cl")) != -1) { switch (c) { case 'c': cflag = 1; break; case 'l': lflag = 1; break; case '?': if (isprint (optopt)) { fprintf (stderr, "Unknown option `-%c'.\n", optopt); } else { fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); return 1; } break; default: abort (); } } if(!(cflag || lflag)) { fprintf(stderr, "\tsyntax: wordcount [-l -c] < file\n\tcounts the words (-c) or the number of lines (-l)\n from the text given to me on stdin"); return -1; } else { while(( fgets(buffer, size-1, stdin) != NULL)) { i = 0; while(buffer[i] != 0) { count_char++; i++; } count_lines++; } if(cflag && lflag) { printf("-l: %i\n-c: %i\n",count_lines, count_char); } else { if(cflag) { printf("%i\n", count_char); } if(lflag) { printf("%i\n", count_lines); } } } return 0; }