Repository: Konstantin8105/c4go
Pull request #421 (2019-04-23 12:41:38)

unistd: fstat

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/421?src=pr&el=h1) Report
> Merging [#421](https://codecov.io/gh/Konstantin8105/c4go/pull/421?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/d8d6cc707218a097829eea294057c4da0cc37ca9?src=pr&el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `85.41%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/421/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/421?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #421      +/-   ##
==========================================
+ Coverage   85.02%   85.06%   +0.03%     
==========================================
  Files         191      191              
  Lines       11583    11631      +48     
==========================================
+ Hits         9849     9894      +45     
- Misses       1254     1259       +5     
+ Partials      480      478       -2
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/421?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [noarch/unistd.go](https://codecov.io/gh/Konstantin8105/c4go/pull/421/diff?src=pr&el=tree#diff-bm9hcmNoL3VuaXN0ZC5nbw==) | `0% <0%> (ø)` | :arrow_up: |
| [program/definition\_struct.go](https://codecov.io/gh/Konstantin8105/c4go/pull/421/diff?src=pr&el=tree#diff-cHJvZ3JhbS9kZWZpbml0aW9uX3N0cnVjdC5nbw==) | `100% <100%> (ø)` | :arrow_up: |
| [transpiler/variables.go](https://codecov.io/gh/Konstantin8105/c4go/pull/421/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci92YXJpYWJsZXMuZ28=) | `87.85% <100%> (+0.13%)` | :arrow_up: |
| [transpiler/cast.go](https://codecov.io/gh/Konstantin8105/c4go/pull/421/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9jYXN0Lmdv) | `75.72% <100%> (+0.28%)` | :arrow_up: |
| [transpiler/operators.go](https://codecov.io/gh/Konstantin8105/c4go/pull/421/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9vcGVyYXRvcnMuZ28=) | `77.25% <0%> (+0.62%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/421?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/421?src=pr&el=footer). Last update [d8d6cc7...364000f](https://codecov.io/gh/Konstantin8105/c4go/pull/421?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/noarch/unistd.go b/noarch/unistd.go
index 6499779..15bb7bb 100644
--- a/noarch/unistd.go
+++ b/noarch/unistd.go
@@ -80,3 +80,15 @@ func Ftruncate(fd int32, length int64) int32 {
 	}
 	return 0
 }
+
+func Fstat(fd int32, stat []syscall.Stat_t) int32 {
+	if len(stat) == 0 {
+		return -1
+	}
+	// func Fstat(fd int, stat *Stat_t) (err error)
+	err := syscall.Fstat(int(fd), &(stat[0]))
+	if err != nil {
+		return -1
+	}
+	return 0
+}
diff --git a/program/definition_struct.go b/program/definition_struct.go
index 51aeb63..6205742 100644
--- a/program/definition_struct.go
+++ b/program/definition_struct.go
@@ -274,4 +274,44 @@ func (p *Program) initializationStructs() {
 			"l_pid":    "Pid",
 		},
 	}
+
+	// sys/stat.h
+	p.Structs["struct stat"] = &Struct{
+		Name: "struct stat",
+		Type: StructType,
+		Fields: map[string]interface{}{
+			"st_dev":     "unsigned long long",
+			"st_ino":     "unsigned long long",
+			"st_mode":    "unsigned int",
+			"st_nlink":   "unsigned long long",
+			"st_uid":     "unsigned int",
+			"st_gid":     "unsigned int",
+			"st_rdev":    "unsigned long long",
+			"st_size":    "long long int",
+			"st_blksize": "long long int",
+			"st_blocks":  "long long int",
+			"st_atime":   "time_t",
+			"st_mtime":   "time_t",
+			"st_ctime":   "time_t",
+		},
+	}
+	p.Structs["c4go_struct stat"] = &Struct{
+		Name: "syscall.Stat_t",
+		Type: StructType,
+		Fields: map[string]interface{}{
+			"st_dev":     "Dev",
+			"st_ino":     "Ino",
+			"st_mode":    "Mode",
+			"st_nlink":   "Nlink",
+			"st_uid":     "Uid",
+			"st_gid":     "Gid",
+			"st_rdev":    "Rdev",
+			"st_size":    "Size",
+			"st_blksize": "Blksize",
+			"st_blocks":  "Blocks",
+			"st_atime":   "Atim",
+			"st_mtime":   "Mtim",
+			"st_ctime":   "Ctim",
+		},
+	}
 }
diff --git a/scripts/ed.sh b/scripts/ed.sh
index 7158054..d8c74e9 100755
--- a/scripts/ed.sh
+++ b/scripts/ed.sh
@@ -19,6 +19,8 @@ mkdir -p ./testdata/
 		wget --no-check-certificate http://mirror.tochlab.net/pub/gnu/ed/$VERSION.tar.lz -P $TEMP_FOLDER
 		echo "Please don't forget install: sudo apt-get install lzip"
 		tar --lzip  -C $TEMP_FOLDER/$VERSION -xvf $TEMP_FOLDER/$VERSION.tar.lz
+		sed -i.bak -e '92d'                                        $TEMP_FOLDER/$VERSION/$VERSION/signal.c
+		sed -i.bak -e '92i   if( ioctl(0, TIOCGWINSZ, &ws) >= 0 )' $TEMP_FOLDER/$VERSION/$VERSION/signal.c
 	fi
 
 # remove go files from last transpilation
diff --git a/transpiler/cast.go b/transpiler/cast.go
index 961fdb1..ff4fe71 100644
--- a/transpiler/cast.go
+++ b/transpiler/cast.go
@@ -148,6 +148,9 @@ func transpileImplicitCastExpr(n *ast.ImplicitCastExpr, p *program.Program, expr
 	if len(n.Type) != 0 && len(n.Type2) != 0 && n.Type != n.Type2 && cast {
 		var tt string
 		tt, err = types.ResolveType(p, n.Type)
+		if err != nil && n.Type2 != "" {
+			tt, err = types.ResolveType(p, n.Type2)
+		}
 		expr = util.NewCallExpr(tt, expr)
 		exprType = n.Type
 		return
diff --git a/transpiler/variables.go b/transpiler/variables.go
index 387136f..fc6d3ac 100644
--- a/transpiler/variables.go
+++ b/transpiler/variables.go
@@ -490,6 +490,13 @@ func transpileMemberExpr(n *ast.MemberExpr, p *program.Program) (
 	}
 	if str := p.GetStruct("c4go_" + lhsType); str != nil {
 		if alias, ok := str.Fields[rhs]; ok {
+			// change type
+			if str, ok := p.Structs[lhsType]; ok {
+				if name, ok := str.Fields[rhs].(string); ok {
+					n.Type = name
+				}
+			}
+			// change field
 			rhs = alias.(string)
 			goto Selector
 		}


Test patch:
diff --git a/tests/unistd.c b/tests/unistd.c
index 4a545bd..fd5d23c 100644
--- a/tests/unistd.c
+++ b/tests/unistd.c
@@ -102,6 +102,41 @@ typedef struct {
 // printf("left: %d right: %d\n",p.left, p.right);
 // }
 // }
+//
+
+void test_fstat()
+{
+	char * filename = "./tests/stdio.txt";
+
+    int file=0;
+        if((file=open(filename,O_RDONLY)) < -1)
+            return;
+ 
+    struct stat fileStat;
+    if(fstat(file,&fileStat) < 0)    
+        return;
+ 
+    printf("Information for %s\n",filename);
+    printf("---------------------------\n");
+    printf("File Size: \t\t%d bytes\n",fileStat.st_size);
+    printf("Number of Links: \t%d\n",fileStat.st_nlink);
+    printf("File inode: \t\t%d\n",fileStat.st_ino);
+ 
+    printf("File Permissions: \t");
+    printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
+    printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
+    printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
+    printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
+    printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
+    printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
+    printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
+    printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
+    printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
+    printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
+    printf("\n\n");
+ 
+    printf("The file %s a symbolic link\n\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
+}
 
 int main()
 {
@@ -111,6 +146,7 @@ int main()
     START_TEST(read);
     START_TEST(read_file);
     // START_TEST(struct);
+	START_TEST(fstat);
 
     done_testing();
 }