Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xv6-public
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
OS Lab Group
奖励实验
xv6-public
提交
1a89baa7
提交
1a89baa7
8月 28, 2007
创建
作者:
rsc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add grep; add lost echo
上级
51c0c1a8
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
108 行增加
和
0 行删除
+108
-0
Makefile
Makefile
+2
-0
grep.c
grep.c
+106
-0
没有找到文件。
Makefile
浏览文件 @
1a89baa7
...
...
@@ -93,7 +93,9 @@ mkfs: mkfs.c fs.h
UPROGS
=
\
_cat
\
_echo
\
_forktest
\
_grep
\
_init
\
_kill
\
_ln
\
...
...
grep.c
0 → 100644
浏览文件 @
1a89baa7
// Simple grep. Only supports ^ . * $ operators.
#include "types.h"
#include "stat.h"
#include "user.h"
char
buf
[
1024
];
int
match
(
char
*
,
char
*
);
void
grep
(
char
*
pattern
,
int
fd
)
{
int
n
,
m
;
char
*
p
,
*
q
;
m
=
0
;
while
((
n
=
read
(
fd
,
buf
+
m
,
sizeof
(
buf
)
-
m
))
>
0
){
m
+=
n
;
p
=
buf
;
while
((
q
=
strchr
(
p
,
'\n'
))
!=
0
){
*
q
=
0
;
if
(
match
(
pattern
,
p
)){
*
q
=
'\n'
;
write
(
1
,
p
,
q
+
1
-
p
);
}
p
=
q
+
1
;
}
if
(
p
==
buf
)
m
=
0
;
if
(
m
>
0
){
m
-=
p
-
buf
;
memmove
(
buf
,
p
,
m
);
}
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
fd
,
i
;
char
*
pattern
;
if
(
argc
<=
1
){
printf
(
2
,
"usage: grep pattern [file ...]
\n
"
);
exit
();
}
pattern
=
argv
[
1
];
if
(
argc
<=
2
){
grep
(
pattern
,
0
);
exit
();
}
for
(
i
=
2
;
i
<
argc
;
i
++
){
if
((
fd
=
open
(
argv
[
i
],
0
))
<
0
){
printf
(
1
,
"grep: cannot open %s
\n
"
,
argv
[
i
]);
exit
();
}
grep
(
pattern
,
fd
);
close
(
fd
);
}
exit
();
}
// Regexp matcher from Kernighan & Pike,
// The Practice of Programming, Chapter 9.
int
matchhere
(
char
*
,
char
*
);
int
matchstar
(
int
,
char
*
,
char
*
);
int
match
(
char
*
re
,
char
*
text
)
{
if
(
re
[
0
]
==
'^'
)
return
matchhere
(
re
+
1
,
text
);
do
{
// must look at empty string
if
(
matchhere
(
re
,
text
))
return
1
;
}
while
(
*
text
++
!=
'\0'
);
return
0
;
}
// matchhere: search for re at beginning of text
int
matchhere
(
char
*
re
,
char
*
text
)
{
if
(
re
[
0
]
==
'\0'
)
return
1
;
if
(
re
[
1
]
==
'*'
)
return
matchstar
(
re
[
0
],
re
+
2
,
text
);
if
(
re
[
0
]
==
'$'
&&
re
[
1
]
==
'\0'
)
return
*
text
==
'\0'
;
if
(
*
text
!=
'\0'
&&
(
re
[
0
]
==
'.'
||
re
[
0
]
==*
text
))
return
matchhere
(
re
+
1
,
text
+
1
);
return
0
;
}
// matchstar: search for c*re at beginning of text
int
matchstar
(
int
c
,
char
*
re
,
char
*
text
)
{
do
{
// a * matches zero or more instances
if
(
matchhere
(
re
,
text
))
return
1
;
}
while
(
*
text
!=
'\0'
&&
(
*
text
++==
c
||
c
==
'.'
));
return
0
;
}
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论