Skip to content

Commit 2680c74

Browse files
committed
fopen: expand supported file modes
Support file modes: r+b, w+b, a+b. Closes #457
1 parent 2348fd0 commit 2680c74

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

_glua-tests/issues.lua

+20
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,26 @@ function test()
480480
end
481481
test()
482482

483+
-- issue #457
484+
function test()
485+
local file = os.tmpname()
486+
os.remove(file)
487+
assert(io.open(file) == nil)
488+
489+
local fh = io.open(file, 'r+b')
490+
assert(fh == nil)
491+
local fh = io.open(file, 'w+b')
492+
assert(fh ~= nil)
493+
fh:close()
494+
os.remove(file)
495+
assert(io.open(file) == nil)
496+
local fh = io.open(file, 'a+b')
497+
assert(fh ~= nil)
498+
fh:close()
499+
os.remove(file)
500+
end
501+
test()
502+
483503
-- issue #459
484504
function test()
485505
local a, b = io.popen("ls", nil)

iolib.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ func ioLines(L *LState) int {
613613
return 1
614614
}
615615

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

618619
func ioOpenFile(L *LState) int {
619620
path := L.CheckString(1)
@@ -633,11 +634,11 @@ func ioOpenFile(L *LState) int {
633634
readable = false
634635
case "a", "ab":
635636
mode = os.O_WRONLY | os.O_APPEND | os.O_CREATE
636-
case "r+", "rb+":
637+
case "r+", "rb+", "r+b":
637638
mode = os.O_RDWR
638-
case "w+", "wb+":
639+
case "w+", "wb+", "w+b":
639640
mode = os.O_RDWR | os.O_TRUNC | os.O_CREATE
640-
case "a+", "ab+":
641+
case "a+", "ab+", "a+b":
641642
mode = os.O_APPEND | os.O_RDWR | os.O_CREATE
642643
}
643644
file, err := newFile(L, nil, path, mode, os.FileMode(perm), writable, readable)

0 commit comments

Comments
 (0)