지그프리드 2008. 8. 10. 23:48
UNIX의 wc(1)의 기능을 구현한 것.

#include "ourhdr.h"  //System Programming Default Header

enum bool {false, true}; //C dosen't support the "boolean" type. So, I defined as enumeration type.


/////////////////////////////////////////
//main function  ////////////
////////////////////////////////////////

int main (int agrc, char *agrv[])
{
    int c = 0;       //get character input
    int lines = 0, words = 0 , bytes = 0; //count lines, words, bytes
    enum bool real_word = false;   //check a byte whether a part of the first word or not

    while ( (c = getchar() )> 0)    //count loop - until EOF (This retuns -1)
    {
        if (c > 33)     //check first character is input or not
            real_word = true;  //character inputed

        if (c=='\n')     //check new-line character
        {
            lines++;     //line count
            words++;     //word count
        }
        else if (c==' ' || c=='\t' || c=='\r' || c=='\v' || c=='\f') //check word seperation conditions
        {
            if (real_word)      //first character inputed
                words++;        //word count
        }

        bytes++;      //byte count
    }

    if (ferror(stdin))     //check stdin error
        err_sys("input error");  //system error massage

    printf ("      %d       %d      %d\n", lines, words, bytes); //print out to stdout

    return 0;
}