-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.c
More file actions
44 lines (42 loc) · 702 Bytes
/
cat.c
File metadata and controls
44 lines (42 loc) · 702 Bytes
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *ptr = fopen(argv[1], "r");
char c;
int e = 0;
int n = 0;
if (argc == 3 && !strcmp(argv[2], "-E")) {
e = 1;
}
if (argc == 3 && !strcmp(argv[2], "-n")) {
n = 1;
}
if (ptr == NULL) {
printf("cat: %s: No such file or directory, or invalid arguments\n", argv[1]);
return -1;
}
int i = 2;
if (n) {
printf(" 1 ");
}
while (!feof(ptr)) {
c = fgetc(ptr);
if (e && c =='\n') {
printf("$");
}
if (!feof(ptr)) {
printf("%c", c);
}
if (n && c == '\n') {
printf(" %d ", i);
i++;
}
}
if (n) {
printf("\n");
}
fclose(ptr);
return 0;
}