Support fopen by flags. Fixes
@@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"reflect"
+ "strings"
)
// Programs generated by c2go will reference noarch.Stdin instead of os.Stdin
@@ -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+":
