Skip to content

Commit

Permalink
Support fopen by flags. Fixes #89 (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamphaus authored and elliotchance committed May 17, 2018
1 parent 570d3ad commit 3669b9d
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions noarch/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"reflect"
"strings"
)

// Programs generated by c2go will reference noarch.Stdin instead of os.Stdin
Expand Down Expand Up @@ -65,9 +66,26 @@ func Fopen(filePath, mode []byte) *File {

sFilePath := CStringToString(filePath)

// TODO: Only some modes are supported by fopen()
// https://github.com/elliotchance/c2go/issues/89
switch CStringToString(mode) {
m := CStringToString(mode)
// no-overwrite flag
if strings.Contains(m, "x") {
m = strings.Replace(m, "x", "", -1)
if strings.Contains(m, "w") {
// only applies when writing to a file
var info os.FileInfo
info, err = os.Stat(sFilePath)
if info != nil || !os.IsNotExist(err) {
setCurrentErrno(EEXIST)
return nil
}
}
}
// binary flag
if strings.Contains(m, "b") {
m = strings.Replace(m, "b", "", -1)
// no other action needed, we are always using binary mode
}
switch m {
case "r":
file, err = os.OpenFile(sFilePath, os.O_RDONLY, 0655)
case "r+":
Expand Down

0 comments on commit 3669b9d

Please sign in to comment.