[v3,02/14] sim/erc32: Removed type mismatch compiler warnings

Message ID 1425244244-27709-3-git-send-email-jiri@gaisler.se
State Superseded
Headers

Commit Message

Jiri Gaisler March 1, 2015, 9:10 p.m. UTC
  * func.c (exec_cmd) : silence compiler warnings when calling system().
	(batch) : replace fgets() with getline().
---
 sim/erc32/func.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
  

Comments

Mike Frysinger March 2, 2015, 1:04 a.m. UTC | #1
On 01 Mar 2015 22:10, Jiri Gaisler wrote:
>      while (!feof(fp)) {
> -	lbuf[0] = 0;
> -	fgets(lbuf, 1023, fp);
> -	if ((strlen(lbuf) > 0) && (lbuf[strlen(lbuf) - 1] == '\n'))
> +	if (getline(&lbuf, &len, fp) == -1)
> +	    break;

do you still need the feof check ?  getline will do that for you:
	while (getline(&lbuf, &len, fp) != -1)

> +	if ((strlen(lbuf) > 0) && (lbuf[strlen(lbuf) - 1] == '\n')) {
>  	    lbuf[strlen(lbuf) - 1] = 0;

is gcc smart enough to cache that strlen call ?  might be better to do it 
yourself:
	size_t slen = strlen(lbuf);
	if (slen && lbuf[slen - 1] == '\n')
	  lbuf[slen - 1] = 0;

> @@ -383,7 +387,7 @@ exec_cmd(sregs, cmd)
>  {
>      char           *cmd1, *cmd2;
>      int32           stat;
> -    uint32          len, i, clen, j;
> +    uint32          len, i, clen, j, tmp;
>      static uint32   daddr = 0;
>      char           *cmdsave;
>  

do you still need |tmp| now that you changed how the system() warning is
silenced ?
-mike
  

Patch

diff --git a/sim/erc32/func.c b/sim/erc32/func.c
index e324b57..e0037e7 100644
--- a/sim/erc32/func.c
+++ b/sim/erc32/func.c
@@ -81,20 +81,24 @@  batch(sregs, fname)
     char           *fname;
 {
     FILE           *fp;
-    char            lbuf[1024];
+    char           *lbuf = NULL;
+    size_t         len = 0;
+    ssize_t        tmp;
 
     if ((fp = fopen(fname, "r")) == NULL) {
 	fprintf(stderr, "couldn't open batch file %s\n", fname);
 	return (0);
     }
     while (!feof(fp)) {
-	lbuf[0] = 0;
-	fgets(lbuf, 1023, fp);
-	if ((strlen(lbuf) > 0) && (lbuf[strlen(lbuf) - 1] == '\n'))
+	if (getline(&lbuf, &len, fp) == -1)
+	    break;
+	if ((strlen(lbuf) > 0) && (lbuf[strlen(lbuf) - 1] == '\n')) {
 	    lbuf[strlen(lbuf) - 1] = 0;
-	printf("sis> %s\n", lbuf);
-	exec_cmd(sregs, lbuf);
+	    printf("sis> %s\n", lbuf);
+	    exec_cmd(sregs, lbuf);
+	}
     }
+    free(lbuf);
     fclose(fp);
     return (1);
 }
@@ -383,7 +387,7 @@  exec_cmd(sregs, cmd)
 {
     char           *cmd1, *cmd2;
     int32           stat;
-    uint32          len, i, clen, j;
+    uint32          len, i, clen, j, tmp;
     static uint32   daddr = 0;
     char           *cmdsave;
 
@@ -555,7 +559,9 @@  exec_cmd(sregs, cmd)
 	    sim_halt();
 	} else if (strncmp(cmd1, "shell", clen) == 0) {
 	    if ((cmd1 = strtok(NULL, " \t\n\r")) != NULL) {
-		system(&cmdsave[clen]);
+		if (system(&cmdsave[clen])) {
+		    /* Silence unused return value warning.  */
+		}
 	    }
 	} else if (strncmp(cmd1, "step", clen) == 0) {
 	    stat = run_sim(sregs, 1, 1);