-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcat.c
93 lines (72 loc) · 1.68 KB
/
testcat.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
// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include<sys/wait.h>
#include <unistd.h>
#include <dirent.h>
// Driver code
int cat(int argc, char *args[])
{
FILE* ptr;
char ch;
if(argc==3){
ptr = fopen(*(args + 1),"r");
char *line = NULL;
size_t len = 0;
if (NULL == ptr) {
printf("file can't be opened \n");
exit(1);
}
while (getline(&line, &len, ptr) != -1) {
printf("%s", line);
}
// do {
// ch = fgetc(ptr);
// printf("%c", ch);
// // Checking if character is not EOF.
// // If it is EOF stop eading.
// } while (ch != EOF);
fclose(ptr);
printf("\n");
}
else if(argc==4){
// printf("arg1: %s\n",*(args +1) );
// printf("arg2: %s\n",*(args +2) );
if(strcmp(*(args + 1),"-n")==0){
ptr = fopen(*(args + 2),"r");
char *line = NULL;
size_t len = 0;
if (NULL == ptr) {
printf("file can't be opened \n");
exit(1);
}
// reads text until newline is encountered
int i = 1;
while (getline(&line, &len, ptr) != -1) {
printf("%d %s", i, line);
i++;
}
fclose(ptr);
printf("\n");
}
else if(strcmp(*(args + 1),"-e")==0){
ptr = fopen(*(args + 2),"r");
char *line = NULL;
size_t len = 0;
if (NULL == ptr) {
printf("file can't be opened \n");
exit(1);
}
// reads text until newline is encountered
while (getline(&line, &len, ptr) != -1) {
printf("%s$", line);
}
fclose(ptr);
printf("\n");
}
}
return 0;
}