C言語-コマンドの作成
前へ 目次へ 次へ 

2-7 fwc

【名前】 fwc - 複数ファイルの行数・単語数・文字数を表示する。

【書式】 fwc [-lwc] filename1 [filename2 ...]

【解説】 複数のファイル名を指定するとファイルごとの行数・単語数・文字数と全体の行数・単語数・文字数を表示する。オプションで表示する項目を指定できる。

【参照】 mtype, getopt.c

【フローチャート・ソースリスト】

fwc


  1:
  2:
  3:
  4:
  5:
  6:
  7:
  8:
  9:
 10:
 11:
 12:
 13:
 14:
 15:
 16:
 17:
 18:
 19:
 20:
 21:
 22:
 23:
 24:
 25:
 26:
 27:
 28:
 29:
 30:
 31:
 32:
 33:
 34:
 35:
 36:
 37:
 38:
 39:
 40:
 41:
 42:
 43:
 44:
 45:
 46:
 47:
 48:
 49:
 50:
 51:
 52:
 53:
 54:
 55:
 56:
 57:
 58:
 59:
 60:
 61:
 62:
 63:
 64:
 65:
 66:
 67:
 68:
 69:
 70:
 71:
 72:
 73:
 74:
 75:
 76:
 77:
 78:
 79:
 80:
 81:
 82:
 83:
 84:
 85:
 86:
 87:
 88:
 89:
 90:
 91:
 92:
 93:
 94:
 95:
 96:
 97:
 98:
 99:
100:
101:
102:
103:
104:
105:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define STRING_MAX 256      /* 最大文字長の定義 */
#define OPT_l 0x01          /* オプション判定用 */
#define OPT_w 0x02
#define OPT_c 0x04

long t_line = 0, t_word = 0, t_moji = 0;

int main(int argc, char **argv)
{
    int     opt = 0;
    void    wc(char *, int);
    extern int  getopt(int, char **, char *);
    extern int  opterr, optind, optopt;

    if(argc < 2){            /* パラメータ数のチェック */
        printf("Usage : FWC [-lwc] filename1 [filename2 ...]\n");
        exit(1);
    }
    opterr = 0;              /* getopt エラー非表示 */
    while(getopt(argc, argv, "lwc") != EOF){    /* オプション判定 */
        switch(optopt){
            case 'l':
                opt = opt | OPT_l;
                break;
            case 'w':
                opt = opt | OPT_w;
                break;
            case 'c':
                opt = opt | OPT_c;
                break;
            default:
                printf("unknown option -%c\n", optopt);
                exit(3);
        }
    }
    if(opt == 0)             /* すべてのオプションなし */
        opt = 0x07;           /* すべてのオプション有効 */
    argc = argc - optind;
    argv = argv + optind;
    if(argc == 1)
        wc(*argv, opt);
    else{
        while(argc){          /* ファイル数分繰り返す */
            wc(*argv, opt);
            --argc;
            ++argv;
        }
        printf("\t%ld\t%ld\t%ld\tTotal\n",t_line, t_word, t_moji);
/* optによって出力する種類を変える。*/
        printf("\t");
        if(opt & OPT_l)
            printf("%ld\t", t_line);
        if(opt & OPT_w)
            printf("%ld\t", t_word);
        if(opt & OPT_c)
            printf("%ld\t", t_moji);
        printf("Total\n");
/* ---------- */
    }
    return(0);
}
void wc(char *file, int opt)
{
    FILE    *fp;
    int     ch, word_f = 0;
    long    line = 0, word = 0, moji = 0;

    if((fp = fopen(file, "rb")) == NULL){      /* オープンチェック */
        printf("can not open file. %s\n", file);
        exit(2);
    }
    while((ch = fgetc(fp)) != EOF){    /* 1文字ずつ読み込む */
        ++moji;                        /* 文字カウント */
        if(word_f == 0){               /* 単語判定 */
            if(!isspace(ch)){
                word_f = 1;
                ++word;                 /* 単語カウント */
            }
        }else{
            if(isspace(ch))
                word_f = 0;
        }
        if(ch == '\n')
            ++line;                     /* 行数カウント */
    }
    printf("\t%ld\t%ld\t%ld\t%s\n",line, word, moji, file);
/* optによって出力する種類を変える。*/
    printf("\t");
    if(opt & OPT_l)
        printf("%ld\t", line);
    if(opt & OPT_w)
        printf("%ld\t", word);
    if(opt & OPT_c)
        printf("%ld\t", moji);
    printf("%s\n", file);
/* ---------- */
    t_line = t_line + line;
    t_word = t_word + word;
    t_moji = t_moji + moji;
    fclose(fp);                        /* クローズ */
}


前へ 目次へ 次へ 
Copyright © 2001-2003 Hiroshi Masuda 

 

 

inserted by FC2 system