2013年5月30日木曜日

開発環境

プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第7章(入出力)、7.7(行の入出力)、演習7-7を解いてみる。

その他参考書籍

演習 7-7.

コード

sample.c

#include <stdio.h>
#include <string.h>
#define MAXLINE 1000

int my_getline(char *, int);

int main(int argc, char *argv[])
{
    char line[MAXLINE];
    long lineno = 0;
    int c, except = 0, number = 0, found = 0;
    FILE *fp;
    char pattern[MAXLINE];
    
    while (--argc > 0 && (*++argv)[0] == '-')
        while (c = *++argv[0])
            switch (c) {
                case 'x':
                    except = 1;
                    break;
                case 'n':
                    number = 1;
                    break;
                default:
                    printf("find: illegal option %c\n", c);
                    argc = 0;
                    found = -1;
                    break;
            }
    if (argc == 2) {
        strcpy(pattern, *argv);
        if((fp = fopen(*++argv, "r")) == NULL) {
            fprintf(stderr, "%s: can't open %s\n", argv[0], *argv);
            exit(1);
        } else {
            while (fgets(line, MAXLINE, fp) != NULL) {
                lineno++;
                if ((strstr(line, pattern) != NULL) != except) {
                    if (number)
                        printf("%ld:", lineno);
                    printf("%s", line);
                    found++;
                }
            }
            fclose(fp);
        }
    } else if (argc == 1)
        while (my_getline(line, MAXLINE) > 0) {
            lineno++;
            if ((strstr(line, *argv) != NULL) != except) {
                if (number)
                    printf("%ld:", lineno);
                printf("%s", line);
                found++;
            }
        }
    else
        fprintf(stderr, "Usage: find -x -n pattern [filename]\n");
    return found;
}

int my_getline(char s[], int lim)
{
    int c, i;
    
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

入出力結果(Terminal)

$ ./a.out MAXLINE < sample.c
#define MAXLINE 1000
    char line[MAXLINE];
    char pattern[MAXLINE];
            while (fgets(line, MAXLINE, fp) != NULL) {
        while (my_getline(line, MAXLINE) > 0) {
$ ./a.out MAXLINE sample.c
#define MAXLINE 1000
    char line[MAXLINE];
    char pattern[MAXLINE];
            while (fgets(line, MAXLINE, fp) != NULL) {
        while (my_getline(line, MAXLINE) > 0) {
$ ./a.out -n MAXLINE < sample.c
3:#define MAXLINE 1000
9:    char line[MAXLINE];
13:    char pattern[MAXLINE];
36:            while (fgets(line, MAXLINE, fp) != NULL) {
48:        while (my_getline(line, MAXLINE) > 0) {
$ ./a.out -n MAXLINE sample.c
3:#define MAXLINE 1000
9:    char line[MAXLINE];
13:    char pattern[MAXLINE];
36:            while (fgets(line, MAXLINE, fp) != NULL) {
48:        while (my_getline(line, MAXLINE) > 0) {
$ ./a.out -nx MAXLINE sample.c
1:#include <stdio.h>
2:#include <string.h>
4:
5:int my_getline(char *, int);
6:
7:int main(int argc, char *argv[])
8:{
10:    long lineno = 0;
11:    int c, except = 0, number = 0, found = 0;
12:    FILE *fp;
14:    
15:    while (--argc > 0 && (*++argv)[0] == '-')
16:        while (c = *++argv[0])
17:            switch (c) {
18:                case 'x':
19:                    except = 1;
20:                    break;
21:                case 'n':
22:                    number = 1;
23:                    break;
24:                default:
25:                    printf("find: illegal option %c\n", c);
26:                    argc = 0;
27:                    found = -1;
28:                    break;
29:            }
30:    if (argc == 2) {
31:        strcpy(pattern, *argv);
32:        if((fp = fopen(*++argv, "r")) == NULL) {
33:            fprintf(stderr, "%s: can't open %s\n", argv[0], *argv);
34:            exit(1);
35:        } else {
37:                lineno++;
38:                if ((strstr(line, pattern) != NULL) != except) {
39:                    if (number)
40:                        printf("%ld:", lineno);
41:                    printf("%s", line);
42:                    found++;
43:                }
44:            }
45:            fclose(fp);
46:        }
47:    } else if (argc == 1)
49:            lineno++;
50:            if ((strstr(line, *argv) != NULL) != except) {
51:                if (number)
52:                    printf("%ld:", lineno);
53:                printf("%s", line);
54:                found++;
55:            }
56:        }
57:    else
58:        fprintf(stderr, "Usage: find -x -n pattern [filename]\n");
59:    return found;
60:}
61:
62:int my_getline(char s[], int lim)
63:{
64:    int c, i;
65:    
66:    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
67:        s[i] = c;
68:    if (c == '\n') {
69:        s[i] = c;
70:        ++i;
71:    }
72:    s[i] = '\0';
73:    return i;
74:}
$ ./a.out
Usage: find -x -n pattern [filename]
$ ./a.out -a
find: illegal option a
Usage: find -x -n pattern [filename]
$

0 コメント:

コメントを投稿