oga's tools
Révision | 57c866cfbce601fce70bb2107dbee656795a699c (tree) |
---|---|
l'heure | 2024-10-18 12:50:07 |
Auteur | hyperoga <hyperoga@gmai...> |
Commiter | hyperoga |
add head.c
@@ -0,0 +1,122 @@ | ||
1 | +/* | |
2 | + * head : ファイルの先頭を指定した行表示する | |
3 | + * | |
4 | + * 2024/10/18 V0.10 by oga. | |
5 | + */ | |
6 | +#include <stdio.h> | |
7 | +#include <string.h> | |
8 | +#include <stdlib.h> | |
9 | +#include <sys/types.h> | |
10 | +#include <sys/stat.h> | |
11 | +#ifndef _WIN32 | |
12 | +#include <unistd.h> | |
13 | +#endif | |
14 | + | |
15 | +#define BUF_SIZE (4096*10) | |
16 | + | |
17 | +int vf = 0; | |
18 | + | |
19 | +/* | |
20 | + * HeadTail : 指定ファイルの先頭1行と最後1行を読み込む | |
21 | + * | |
22 | + * IN : fname : ファイル名 | |
23 | + * OUT: headbuf : はじめの1行(サイズBUF_SIZE以上) | |
24 | + */ | |
25 | +int Head(char *fname, int line) | |
26 | +{ | |
27 | + FILE *fp; | |
28 | + char buf[BUF_SIZE]; | |
29 | + struct stat stbuf; | |
30 | + int filef = 1; /* 1:file 0:stdin */ | |
31 | + int cnt = 0; | |
32 | + int line2 = 0; /* abs(line) */ | |
33 | + | |
34 | + if (!strcmp(fname, "-")) { | |
35 | + filef = 0; | |
36 | + } else if (stat(fname, &stbuf) < 0) { | |
37 | + perror(fname); | |
38 | + return -1; | |
39 | + } | |
40 | + | |
41 | + if (filef) { | |
42 | + if ((fp = fopen(fname, "r")) == NULL) { | |
43 | + perror("fopen"); | |
44 | + return -2; | |
45 | + } | |
46 | + } else { | |
47 | + fp = stdin; | |
48 | + } | |
49 | + | |
50 | + if (line < 0) { | |
51 | + line2 = -line; | |
52 | + } else { | |
53 | + line2 = line; | |
54 | + } | |
55 | + | |
56 | + while (fgets(buf, BUF_SIZE, fp)) { | |
57 | + if (line < 0) { | |
58 | + /* 先頭n行 */ | |
59 | + if (cnt < line2) { | |
60 | + printf("%s", buf); | |
61 | + } else { | |
62 | + break; | |
63 | + } | |
64 | + } else { | |
65 | + /* 指定行以降を出力 */ | |
66 | + if (cnt >= line2) { | |
67 | + printf("%s", buf); | |
68 | + } | |
69 | + } | |
70 | + ++cnt; | |
71 | + } | |
72 | + | |
73 | + fclose(fp); | |
74 | + | |
75 | + return 0; | |
76 | +} | |
77 | + | |
78 | +void usage() | |
79 | +{ | |
80 | + printf("usage: head [{-<n> | +<n>}] [<filename>]\n"); | |
81 | +} | |
82 | + | |
83 | +int main(int a, char *b[]) | |
84 | +{ | |
85 | + char headbuf[BUF_SIZE]; | |
86 | + char tailbuf[BUF_SIZE]; | |
87 | + char *fname = NULL; | |
88 | + int i; | |
89 | + int line = -10; /* default 先頭10行 */ | |
90 | + | |
91 | + /* arg check */ | |
92 | + for (i = 1; i<a; i++) { | |
93 | + if (!strncmp(b[i],"-v",2)) { | |
94 | + vf = 1; | |
95 | + continue; | |
96 | + } | |
97 | + if (b[i][0] == '-' || b[i][0] == '+') { | |
98 | + line = atoi(b[i]); | |
99 | + continue; | |
100 | + } | |
101 | + if (!strncmp(b[i],"-h",2)) { | |
102 | + usage(); | |
103 | + exit(1); | |
104 | + } | |
105 | + fname = b[i]; | |
106 | + } | |
107 | + | |
108 | + if (line == 0) { | |
109 | + usage(); | |
110 | + exit(1); | |
111 | + } | |
112 | + | |
113 | + if (fname == NULL) { | |
114 | + /* usage(); */ | |
115 | + /* exit(1); */ | |
116 | + fname = "-"; | |
117 | + } | |
118 | + | |
119 | + Head(fname, line); | |
120 | + | |
121 | + return 0; | |
122 | +} |