-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_cat.c
More file actions
132 lines (113 loc) · 3.18 KB
/
Copy pathread_cat.c
File metadata and controls
132 lines (113 loc) · 3.18 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
#ifndef READCAT
#define READCAT
#include "open_close.c"
int read_file(char *fd_str, char *nbytes_str)
{
// ask for fd
int fd = atoi(fd_str);
// ask for nbytes
int nbytes = atoi(nbytes_str);
// verify fd is opened for RD or RW
if (fd < 0 || fd >= NFD)
{
printf("read_file> invalid fd\n");
return 0;
}
OFT *oftp = running->fd[fd];
if (!oftp)
{
printf("read_file> fd not an instance\n");
return 0;
}
if (oftp->mode == 0 || oftp->mode == 2)
{
}
else
{
printf("read_file> fd not correct mode\n");
return 0;
}
// create buf of size nbytes + 1 for storing nullbyte
char buf[BLKSIZE];
// make read call
int result = my_read(fd, buf, nbytes);
buf[nbytes] = 0;
printf("%s\n", buf);
return result;
}
int my_read(int fd, char buf[ ], int nbytes)
{
OFT *oftp = running->fd[fd];
MINODE *mip = oftp->minodePtr;
INODE *ip = &mip->INODE;
int total_read = 0;
int available = ip->i_size - oftp->offset;
char *cq = buf;
while (nbytes && available)
{
int test = 0;
// compute logical block
int lbk = oftp->offset / BLKSIZE;
int startByte = oftp->offset % BLKSIZE;
// get block
int blk = -1;
if (lbk < 12)
{
blk = ip->i_block[lbk];
}
else if (lbk >= 12 && lbk < 256 + 12)
{
// indirect block
int ibuf[256];
get_block(mip->dev, ip->i_block[12], ibuf);
blk = ibuf[lbk - 12];
}
else {
// double indirect block
int ibuf[256];
get_block(mip->dev, ip->i_block[13], ibuf);
// get second indirect block
int second_lbk = (lbk - 256 - 12) / 256;
int iibuf[256];
get_block(mip->dev, ibuf[second_lbk], iibuf);
test = ibuf[0];
blk = iibuf[(lbk - 256 - 12) % 256];
}
// read blk
// printf("test: %d lbk: %d blk: %d offset: %d\n", test, lbk, blk, oftp->offset);
char rbuf[BLKSIZE];
bzero(rbuf, BLKSIZE);
get_block(mip->dev, blk, rbuf);
char *cp = rbuf + startByte;
int remain = BLKSIZE - startByte;
// read the lesser of available and and nbytes
int read_amount = available < nbytes ? available : nbytes;
// then ensure that the amount is less than remain
read_amount = read_amount < remain ? read_amount : remain;
// copy rbuf contents to buf
memcpy(cq, cp, read_amount);
// update buf offset, nbytes, total_read, available, and offset
cq += read_amount;
nbytes -= read_amount;
total_read += read_amount;
available -= read_amount;
oftp->offset += read_amount;
}
// printf("read %d char from file descriptor fd=%d\n", total_read, fd);
return total_read;
}
int my_cat(char *pathname)
{
// intialize
char buf[BLKSIZE], dummy = 0;
int n;
//
int fd = open_file(pathname, "0");
while (n = my_read(fd, buf, BLKSIZE))
{
buf[n] = 0;
printf("%s", buf);
}
close_file(fd);
}
#endif