2013年5月11日土曜日

開発環境

プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第5章(ポインタと配列)、5.10(コマンド行の引数)、演習5-13を解いてみる。

その他参考書籍

演習 5-13.

コード

sample.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLINES 5000
#define MAXLEN 1000
#define MAXSTOR 5000
#define ALLOCSIZE 10000

void tail(int argc, char *argv[]);
int readlines(char *lineptr[], int nlines);
int my_getline(char *, int);
char *alloc(int);
char allocbuf[ALLOCSIZE];
char *allocp = allocbuf;

int main(int argc, char *argv[])
{
    tail(argc, argv);
    return 0;
}

void tail(int argc, char *argv[])
{
    int n, nlines, i;
    char *lineptr[MAXLINES];
    if (argc == 1 || (*++argv)[0] != '-') {
        n = 10;
    } else
        n = atoi(&(*argv)[1]);
    if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
        if (n > nlines)
            n = nlines;
        for (i = nlines - n; i < nlines; i++)
            printf("%2d行目、%2d行: %s\n", (n + 1) - (nlines - i), i + 1, lineptr[i]);
    } else
        printf("error: input too big\n");
}

int readlines(char *lineptr[], int maxlines)
{
    int len, nlines;
    char *p, line[MAXLEN];
    nlines = 0;
    while ((len = my_getline(line, MAXLEN)) > 0) {
        if (nlines >= maxlines || (p = alloc(len)) == NULL)
            return -1;
        else {
            line[len - 1] = '\0';
            strcpy(p, line);
            lineptr[nlines++] = p;
        }
    }
    return nlines;
}

int my_getline(char *s, int lim)
{
    int c;
    char *t = s;
    
    while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
        *s++ = c;
    if (c == '\n')
        *s++ = c;
    *s = '\0';
    return s - t;
}

char *alloc(int n)
{
    if (allocbuf + ALLOCSIZE - allocp >= n) {
        allocp += n;
        return allocp - n;
    } else 
        return 0;
}

入出力結果(Terminal)

$ cat sample.c | ./a.out
 1行目、68行: }
 2行目、69行: 
 3行目、70行: char *alloc(int n)
 4行目、71行: {
 5行目、72行:     if (allocbuf + ALLOCSIZE - allocp >= n) {
 6行目、73行:         allocp += n;
 7行目、74行:         return allocp - n;
 8行目、75行:     } else 
 9行目、76行:         return 0;
10行目、77行: }
$ cat sample.c | ./a.out -5
 1行目、73行:         allocp += n;
 2行目、74行:         return allocp - n;
 3行目、75行:     } else 
 4行目、76行:         return 0;
 5行目、77行: }
$ cat sample.c | ./a.out -15
 1行目、63行:         *s++ = c;
 2行目、64行:     if (c == '\n')
 3行目、65行:         *s++ = c;
 4行目、66行:     *s = '\0';
 5行目、67行:     return s - t;
 6行目、68行: }
 7行目、69行: 
 8行目、70行: char *alloc(int n)
 9行目、71行: {
10行目、72行:     if (allocbuf + ALLOCSIZE - allocp >= n) {
11行目、73行:         allocp += n;
12行目、74行:         return allocp - n;
13行目、75行:     } else 
14行目、76行:         return 0;
15行目、77行: }
$ cat sample.c | ./a.out -100
 1行目、 1行: #include <stdio.h>
 2行目、 2行: #include <stdlib.h>
 3行目、 3行: #include <string.h>
 4行目、 4行: 
 5行目、 5行: #define MAXLINES 5000
 6行目、 6行: #define MAXLEN 1000
 7行目、 7行: #define MAXSTOR 5000
 8行目、 8行: #define ALLOCSIZE 10000
 9行目、 9行: 
10行目、10行: void tail(int argc, char *argv[]);
11行目、11行: int readlines(char *lineptr[], int nlines);
12行目、12行: int my_getline(char *, int);
13行目、13行: char *alloc(int);
14行目、14行: char allocbuf[ALLOCSIZE];
15行目、15行: char *allocp = allocbuf;
16行目、16行: 
17行目、17行: int main(int argc, char *argv[])
18行目、18行: {
19行目、19行:     tail(argc, argv);
20行目、20行:     return 0;
21行目、21行: }
22行目、22行: 
23行目、23行: void tail(int argc, char *argv[])
24行目、24行: {
25行目、25行:     int n, nlines, i;
26行目、26行:     char *lineptr[MAXLINES];
27行目、27行:     if (argc == 1 || (*++argv)[0] != '-') {
28行目、28行:         n = 10;
29行目、29行:     } else
30行目、30行:         n = atoi(&(*argv)[1]);
31行目、31行:     if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
32行目、32行:         if (n > nlines)
33行目、33行:             n = nlines;
34行目、34行:         for (i = nlines - n; i < nlines; i++)
35行目、35行:             printf("%2d行目、%2d行: %s\n", (n + 1) - (nlines - i), i + 1, lineptr[i]);
36行目、36行:     } else
37行目、37行:         printf("error: input too big\n");
38行目、38行: }
39行目、39行: 
40行目、40行: int readlines(char *lineptr[], int maxlines)
41行目、41行: {
42行目、42行:     int len, nlines;
43行目、43行:     char *p, line[MAXLEN];
44行目、44行:     nlines = 0;
45行目、45行:     while ((len = my_getline(line, MAXLEN)) > 0) {
46行目、46行:         if (nlines >= maxlines || (p = alloc(len)) == NULL)
47行目、47行:             return -1;
48行目、48行:         else {
49行目、49行:             line[len - 1] = '\0';
50行目、50行:             strcpy(p, line);
51行目、51行:             lineptr[nlines++] = p;
52行目、52行:         }
53行目、53行:     }
54行目、54行:     return nlines;
55行目、55行: }
56行目、56行: 
57行目、57行: int my_getline(char *s, int lim)
58行目、58行: {
59行目、59行:     int c;
60行目、60行:     char *t = s;
61行目、61行:     
62行目、62行:     while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
63行目、63行:         *s++ = c;
64行目、64行:     if (c == '\n')
65行目、65行:         *s++ = c;
66行目、66行:     *s = '\0';
67行目、67行:     return s - t;
68行目、68行: }
69行目、69行: 
70行目、70行: char *alloc(int n)
71行目、71行: {
72行目、72行:     if (allocbuf + ALLOCSIZE - allocp >= n) {
73行目、73行:         allocp += n;
74行目、74行:         return allocp - n;
75行目、75行:     } else 
76行目、76行:         return 0;
77行目、77行: }
$

0 コメント:

コメントを投稿