Skip to content

Commit

Permalink
fix ip/hosts file line calc
Browse files Browse the repository at this point in the history
  • Loading branch information
cckuailong committed Oct 7, 2023
1 parent b889677 commit c5f038c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ Simple Nginx Web Page.

## ChangeLog

v0.2.3
- Fix the bug of wrong calculation of file line number

v0.2.2
- The -i option supports IP range scanning, such as 1.2.3.4/24
- The -p option supports custom scan ports, such as 80,8000-8009
Expand Down
3 changes: 3 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ server {

## ChangeLog

v0.2.3
- 修复文件行数计算错误的BUG

v0.2.2
- -i选项支持IP段扫描,1.2.3.4/24
- -p选项支持自定义扫描端口,如 80,8000-8009
Expand Down
8 changes: 7 additions & 1 deletion utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ func LineCounter(filepath string) (int, error) {
return 0, err
}
defer f.Close()
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
var last_buf []byte

for {
buf := make([]byte, 32*1024)
switch c, err := f.Read(buf[:]); true {
case c < 0:
return 0, err
case c == 0: // EOF
last_buf = bytes.Trim(last_buf, "\x00")
if last_buf[len(last_buf)-1] != lineSep[0]{
count += 1
}
return count, nil
case c > 0:
count += bytes.Count(buf[:c], lineSep)
last_buf = buf
}
}

Expand Down

0 comments on commit c5f038c

Please sign in to comment.