Skip to content

Commit c5f038c

Browse files
committed
fix ip/hosts file line calc
1 parent b889677 commit c5f038c

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ Simple Nginx Web Page.
147147

148148
## ChangeLog
149149

150+
v0.2.3
151+
- Fix the bug of wrong calculation of file line number
152+
150153
v0.2.2
151154
- The -i option supports IP range scanning, such as 1.2.3.4/24
152155
- The -p option supports custom scan ports, such as 80,8000-8009

README_zh.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ server {
151151

152152
## ChangeLog
153153

154+
v0.2.3
155+
- 修复文件行数计算错误的BUG
156+
154157
v0.2.2
155158
- -i选项支持IP段扫描,1.2.3.4/24
156159
- -p选项支持自定义扫描端口,如 80,8000-8009

utils/file.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@ func LineCounter(filepath string) (int, error) {
1313
return 0, err
1414
}
1515
defer f.Close()
16-
buf := make([]byte, 32*1024)
1716
count := 0
1817
lineSep := []byte{'\n'}
18+
var last_buf []byte
1919

2020
for {
21+
buf := make([]byte, 32*1024)
2122
switch c, err := f.Read(buf[:]); true {
2223
case c < 0:
2324
return 0, err
2425
case c == 0: // EOF
26+
last_buf = bytes.Trim(last_buf, "\x00")
27+
if last_buf[len(last_buf)-1] != lineSep[0]{
28+
count += 1
29+
}
2530
return count, nil
2631
case c > 0:
2732
count += bytes.Count(buf[:c], lineSep)
33+
last_buf = buf
2834
}
2935
}
3036

0 commit comments

Comments
 (0)