-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmasatora-cat.c
More file actions
178 lines (154 loc) · 4.4 KB
/
Copy pathmasatora-cat.c
File metadata and controls
178 lines (154 loc) · 4.4 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <stdarg.h>
#define E_NOT_FOUND 0
#define E_DIRECTORY 1
#define E_PERMISSION 2
#define E_INVALID_ARG 3
typedef struct options {
int number;
} Options;
void print(int file, Options options);
void itoa(int n, char s[]);
void reverse(char s[]);
void error(int e_type, char *name);
int main(int argc, char *argv[])
{
int file, c;
int number = 0;
Options options = { 0 };
struct stat stduf;
// 引数がない場合は標準入力から読み込んで出力
if (argc > 1) {
while (--argc > 0 && (*++argv)[0] == '-') {
//引数がハイフンだけだった場合は標準入力から読み込んで出力
if (*++argv[0] == NULL) {
print(0, options);
return 0;
}
while ((c = *argv[0]++)) {
switch (c) {
case 'n':
options.number = 1;
break;
default:
// cat -[invalid arguments]とした場合、エラーメッセージには一番最初の不正なオプションのみを表示する
// (例) cat -aho の場合、cat: illegal option -- a
argv[0]--;
(*argv)[1] = '\0';
error(E_INVALID_ARG, *argv);
return 1;
}
}
}
// オプションだけ指定した場合は標準入力から読み込んで出力
if (argc > 0) {
while (*argv) {
stat(*argv, &stduf);
if ((stduf.st_mode & S_IFMT) == S_IFDIR) {
error(E_DIRECTORY, *argv);
break;
}
if (access(*argv, F_OK) != 0) {
error(E_NOT_FOUND, *argv);
break;
} else {
if (access(*argv, R_OK) != 0) {
error(E_PERMISSION, *argv);
break;
}
}
file = open(*argv, O_RDONLY, 0);
print(file, options);
close(file);
argv++;
}
} else
print(0, options);
} else
print(0, options);
return 0;
}
#define BUFSIZE 1000
#define MAXSIZE 1000
void print(int file, Options options)
{
// -nオプション
if (options.number) {
char c;
int n, i = 0, lc = 1;
char line[MAXSIZE], lc_char[10];
while ((n = read(file, &c, 1)) == 1) {
line[i] = c;
i++;
if (c == '\n') {
itoa(lc++, lc_char);
line[i] = '\0';
write(1, " ", strlen(" "));
write(1, lc_char, strlen(lc_char));
write(1, " ", strlen(" "));
write(1, line, strlen(line));
i = 0;
}
}
} else {
char buf[BUFSIZE];
int n;
while ((n = read(file, buf, BUFSIZE)) > 0)
write(1, buf, n);
}
}
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0)
n = -n;
i = 0;
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
#define ERROR_MSG_SIZE 256
void error(int e_type, char *name)
{
char error_msg[ERROR_MSG_SIZE];
strcpy(error_msg, "cat: ");
switch (e_type) {
case E_NOT_FOUND:
strcat(error_msg, name);
strcat(error_msg, ": No such file or directory");
break;
case E_DIRECTORY:
strcat(error_msg, name);
strcat(error_msg, ": Is a directory");
break;
case E_PERMISSION:
strcat(error_msg, name);
strcat(error_msg, ": Permission denied");
break;
case E_INVALID_ARG:
strcat(error_msg, "illegal option -- ");
strcat(error_msg, name);
strcat(error_msg, "\nusage: cat [-n] [file ...]");
break;
default:
break;
}
strcat(error_msg, "\n");
write(2, error_msg, strlen(error_msg));
}