Skip to content

Commit

Permalink
fopen: expand supported file modes (#9)
Browse files Browse the repository at this point in the history
Support file modes: r+b, w+b, a+b.

Closes yuin#457

Co-authored-by: Pavel Balaev <[email protected]>
  • Loading branch information
tsukinoko-kun and 0x501D authored Apr 2, 2024
1 parent 0ea1e3a commit 2c37dac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
20 changes: 20 additions & 0 deletions _glua-tests/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,26 @@ function test()
end
test()

-- issue #457
function test()
local file = os.tmpname()
os.remove(file)
assert(io.open(file) == nil)

local fh = io.open(file, 'r+b')
assert(fh == nil)
local fh = io.open(file, 'w+b')
assert(fh ~= nil)
fh:close()
os.remove(file)
assert(io.open(file) == nil)
local fh = io.open(file, 'a+b')
assert(fh ~= nil)
fh:close()
os.remove(file)
end
test()

-- issue #459
function test()
local a, b = io.popen("ls", nil)
Expand Down
9 changes: 5 additions & 4 deletions iolib.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@ func ioLines(L *LState) int {
return 1
}

var ioOpenOpions = []string{"r", "rb", "w", "wb", "a", "ab", "r+", "rb+", "w+", "wb+", "a+", "ab+"}
var ioOpenOpions = []string{"r", "rb", "w", "wb", "a", "ab", "r+", "rb+", "w+", "wb+", "a+", "ab+",
"r+b", "w+b", "a+b"}

func ioOpenFile(L *LState) int {
path := L.CheckString(1)
Expand All @@ -633,11 +634,11 @@ func ioOpenFile(L *LState) int {
readable = false
case "a", "ab":
mode = os.O_WRONLY | os.O_APPEND | os.O_CREATE
case "r+", "rb+":
case "r+", "rb+", "r+b":
mode = os.O_RDWR
case "w+", "wb+":
case "w+", "wb+", "w+b":
mode = os.O_RDWR | os.O_TRUNC | os.O_CREATE
case "a+", "ab+":
case "a+", "ab+", "a+b":
mode = os.O_APPEND | os.O_RDWR | os.O_CREATE
}
file, err := newFile(L, nil, path, mode, os.FileMode(perm), writable, readable)
Expand Down

0 comments on commit 2c37dac

Please sign in to comment.