forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 12
/
head.c
executable file
·65 lines (62 loc) · 1.11 KB
/
head.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
/*
*文件名称:head.c
*创建者:程嘉梁
*创建日期:2018/04/23
*文件描述:列出文件前十行(head命令)
*历史记录:整合自三字班方案二
*/
#include "types.h"
#include "stat.h"
#include "user.h"
void head(int fp,int num){
int i,length,pos=0;
const int bufSize = 128;
char buf[bufSize];
char tmp[bufSize];
int counter = 0;
while((length = read(fp,buf,bufSize)) > 0){
if(length > bufSize)
return;
for(i = 0;i < length;++i){
if(buf[i] == '\n'){
tmp[pos] = 0;
counter++;
printf(1,"%d : %s\n",counter,tmp);
pos = 0;
if(counter == num){
exit();
}
}
else{
tmp[pos++] = buf[i];
if(pos==(bufSize-1))
{
buf[bufSize-1]=0;
counter++;
printf(1,"%d : %s\n",counter,tmp);
pos = 0;
if(counter == num){
exit();
}
}
}
}
}
}
int main(int argc,char* argv[]){
int i;
int fp;
if(argc < 2){
printf(1, "Usage: head files...\n");
exit();
}
for(i = 1;i < argc;i++){
if((fp = open(argv[i],0)) < 0){
printf(1,"head : cannot open %s\n",argv[i]);
exit();
}
head(fp,10);
close(fp);
}
exit();
}