Skip to content

Commit

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

Closes #457
  • Loading branch information
0x501D committed Dec 12, 2023
1 parent 2348fd0 commit 5983e13
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 5983e13

Please sign in to comment.