본문 바로가기

Studies/Advanced Programming in the UNIX Environment

WC : Word Count

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;
}

'Studies > Advanced Programming in the UNIX Environment' 카테고리의 다른 글

myShell  (0) 2008.08.12
myCAT : cat(1)  (0) 2008.08.12
실행파일 만들기 : Make a .exe file for APUE  (0) 2008.08.10
error.c  (0) 2008.08.10
ourhdr.h  (0) 2008.08.10