forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 12
/
more.c
215 lines (195 loc) · 3.45 KB
/
more.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
*文件名称:more.c
*创建者:程嘉梁
*创建日期:2018/05/07
*文件描述:实现按页阅读文件的功能(more命令)
*/
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
#define LINE_LEN 50//每行最长长度
#define PAGE_LINES 5//每页行数
#define PAGE_MAX 300//最大页数
void more(int fp)
{
int i,j,length;
const int bufSize = 1024;
char buf[bufSize];
int page_capacity[PAGE_MAX];
char c[6];
int current_page,current_line,line_num,nextpos;
//初始化
current_page=current_line=line_num=nextpos=0;
j=0;
char *show=(char*)malloc(bufSize*sizeof(char));
for(i=0;i<PAGE_MAX;i++)
{
page_capacity[i]=0;
}
length=read(fp,buf,sizeof(buf));
for(i=0;i<length&&line_num<PAGE_LINES;i++)
{
if(buf[i]=='\n'&¤t_line<LINE_LEN)
{
show[j]='\n';
current_line=0;
page_capacity[current_page]++;
line_num++;
j++;
}
else if(current_line>=LINE_LEN)
{
current_line=0;
show[j]='\n';
j++;
line_num++;
if(buf[i]=='\n')
{
page_capacity[current_page]++;
}
else
{
i--;
}
}
else
{
show[j]=buf[i];
j++;
current_line++;
page_capacity[current_page]++;
}
}
show[j]='\0';
j=0;
printf(1,"------------------------------\n");
printf(1,"%s",show);
printf(1,"------------------------------\n");
while(1)
{
gets(c,5);
switch(c[0])
{
case 'd':
current_page++;
nextpos=0;
for(i=0;i<current_page;i++)
{
nextpos+=page_capacity[i];
}
lseek(fp,nextpos,0);
if((length=read(fp,buf,sizeof(buf)))<=0)//读到最后一页
{
current_page--;
nextpos=0;
for(i=0;i<current_page;i++)
{
nextpos+=page_capacity[i];
}
}
break;
case 'u':
current_page--;
nextpos=0;
if(current_page<0)//读到第一页
{
current_page=0;
}
for(i=0;i<current_page;i++)
{
nextpos+=page_capacity[i];
}
break;
case 'z':
nextpos=-1;
break;
default:
printf(1,"input 'd': Read next page.\n");
printf(1,"input 'u': Read previous page.\n");
printf(1,"input 'z': Exit.\n");
nextpos=-2;
break;
}
if(nextpos==-1)
{
free(show);
return;
}
if(nextpos==-2)
{
continue;
}
lseek(fp,nextpos,0);
length=read(fp,buf,sizeof(buf));
current_line=line_num=0;
j=0;
page_capacity[current_page]=0;
for(i=0;i<length&&line_num<PAGE_LINES;i++)
{
if(buf[i]=='\n'&¤t_line<LINE_LEN)
{
show[j]='\n';
current_line=0;
page_capacity[current_page]++;
line_num++;
j++;
}
else if(current_line>=LINE_LEN)
{
current_line=0;
show[j]='\n';
j++;
line_num++;
if(buf[i]=='\n')
{
page_capacity[current_page]++;
}
else
{
i--;
}
}
else
{
show[j]=buf[i];
j++;
current_line++;
page_capacity[current_page]++;
}
}
show[j]='\0';
j=0;
printf(1,"------------------------------\n");
printf(1,"%s",show);
printf(1,"------------------------------\n");
}
free(show);
}
int main(int argc,char* argv[])
{
//判断参数
if(argc!=2)
{
printf(1, "please input the command as [more dest_file]\n");
exit();
}
//打开文件
int fp;
if((fp=open(argv[1],O_RDONLY))<0)
{
printf(1,"more : cannot open %s\n",argv[1]);
exit();
}
//判断文件状态是否为文件夹
struct stat st;
fstat(fp, &st);
if (st.type == T_DIR)
{
printf(1, "the file is a directory!\n");
exit();
}
more(fp);
close(fp);
exit();
}