-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.c
93 lines (76 loc) · 1.88 KB
/
cat.c
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void cat(char *arr[])
{
if (arr[2] != NULL)
{
arr[2][strcspn(arr[2], "\n")] = 0;
FILE *f;
f = fopen(arr[2], "r");
if (f != NULL)
{
if (!strcmp(arr[1], "-n"))
{
int i = 1;
char arrr[1000];
while (fgets(arrr, sizeof(arrr), f))
{
printf(" %d ", i);
printf("%s", arrr);
i++;
}
}
else if (!strcmp(arr[1], "-E"))
{
char arrr[1000];
while (fgets(arrr, sizeof(arrr), f))
{
arrr[strcspn(arrr, "\n")] = 0;
printf("%s", arrr);
printf("$\n");
}
}
else
{
printf("cat: invalid option %s\n", arr[1]);
}
fclose(f);
}
else if (!strcmp("SHELL=/bin/bash", arr[2]) || !strcmp("LESSOPEN=| /usr/bin/lesspipe %s", arr[2]))
{
printf("cat: missing operand\n");
}
else
{
printf("cat: %s: No such file or directory\n", arr[2]);
}
}
else if (arr[1] != NULL)
{
arr[1][strcspn(arr[1], "\n")] = 0;
FILE *f;
f = fopen(arr[1], "r");
if (f != NULL)
{
char content = fgetc(f);
while (content != EOF)
{
printf("%c", content);
content = fgetc(f);
}
fclose(f);
}
else
{
printf("cat: %s: No such file or directory\n", arr[1]);
}
}
}
int main(int argc, char *argv[])
{
char *arr[] = {argv[1], argv[2], argv[3]};
cat(arr);
return 0;
}