C言語-コマンドの作成 |
2-7 fwc
【名前】 fwc - 複数ファイルの行数・単語数・文字数を表示する。
【書式】 fwc [-lwc] filename1 [filename2 ...]
【解説】 複数のファイル名を指定するとファイルごとの行数・単語数・文字数と全体の行数・単語数・文字数を表示する。オプションで表示する項目を指定できる。
【参照】 mtype, getopt.c
【フローチャート・ソースリスト】
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; } |
Copyright © 2001-2003 Hiroshi Masuda |