-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.c
More file actions
61 lines (52 loc) · 1.17 KB
/
Copy pathcat.c
File metadata and controls
61 lines (52 loc) · 1.17 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
#include "ucode.c"
char newline = '\n';
char CR = '\r';
int main(int argc, char *argv[])
{
int fd, i, n;
char *cp;
char buf[1024];
print2f("Tyler's obese cat\n");
fd = 0;
if (argc > 1) //if we have more than one arg
{
fd = open(argv[1], O_RDONLY); //open what youre passing in
if (fd < 0) //if it doesnt work throw error
{
printf("cat %s error\n", argv[1]);
exit(0);
}
}
if (argc < 2) //for cat w no args
{
while (gets(buf)) //get the buf
{
print2f(buf); //print it out
print2f("\n\r");
}
exit(0);
}
while ((n = read(fd, buf, 1024))) //while reading from file
{
buf[n] = 0; //setting null terminator to end of buf
cp = buf; //setting up cp
if (fd) //if fd is open
{
for (int i = 0; i < n; i++) //for each byte youre writing byte by byte to stdout
{
write(1, &buf[i], 1);
if (buf[i] == '\n') //if new line youre writing carriage return
write(2, &CR, 1);
}
}
else
{
cp = buf; //set cp to buf
if (*cp == '\r') //check if it equals carriage return
write(2, &newline, 1); //literally write new line to stdout
write(1, cp, 1); //write new line to stdin
}
}
close(fd);
exit(0);
}