Fixes for gcc warnings - mostly WRT check of returns values
authorAdam Dickmeiss <adam@indexdata.dk>
Fri, 14 Nov 2008 14:17:22 +0000 (15:17 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Fri, 14 Nov 2008 14:17:22 +0000 (15:17 +0100)
15 files changed:
client/admin.c
client/client.c
src/daemon.c
src/seshigh.c
src/statserv.c
src/unix.c
util/marcdump.c
util/srwtst.c
util/yaz-illclient.c
util/yaz-xmlquery.c
util/yaziconv.c
zoom/zoomsh.c
zoom/zoomtst3.c
zoom/zoomtst5.c
zoom/zoomtst6.c

index 019910a..50ddaf8 100644 (file)
@@ -236,8 +236,14 @@ int cmd_adm_import(const char *arg)
                 
                 oct->len = oct->size = status.st_size;
                 oct->buf = (unsigned char *) odr_malloc (out, oct->size);
-                fread (oct->buf, 1, oct->size, inf);
-                fclose (inf);
+                if (fread(oct->buf, 1, oct->size, inf) != oct->size)
+                {
+                    printf("Incomplete read of file %s\n", fname);
+                }
+                if (fclose(inf))
+                {
+                    printf("Close failed for file %s\n", fname);
+                }
                 
                 segment->segmentRecords[segment->num_segmentRecords++] = rec;
 
index 5cce8cc..38ac3d7 100644 (file)
@@ -177,6 +177,8 @@ int cmd_querycharset(const char *arg);
 
 static void close_session(void);
 
+static void marc_file_write(const char *buf, size_t sz);
+
 ODR getODROutputStream(void)
 {
     return out;
@@ -220,9 +222,14 @@ static void do_hex_dump(const char* buf, int len)
             sprintf(fname, "%s.%03d.raw", dump_file_prefix, no);
             of = fopen(fname, "wb");
 
-            fwrite(buf, 1, len, of);
-
-            fclose(of);
+            if (fwrite(buf, 1, len, of) != len)
+            {
+                printf("write failed for %s", fname);
+            }
+            if (fclose(of))
+            {
+                printf("close failed for %s", fname);
+            }
         }
     }
 }
@@ -882,8 +889,7 @@ static void display_record(Z_External *r)
     {
         print_record((const unsigned char *) r->u.octet_aligned->buf,
                      r->u.octet_aligned->len);
-        if (marc_file)
-            fwrite(r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, marc_file);
+        marc_file_write(r->u.octet_aligned->buf, r->u.octet_aligned->len);
     }
     else if (oid && r->which == Z_External_octet)
     {
@@ -943,7 +949,10 @@ static void display_record(Z_External *r)
                 if (yaz_marc_decode_buf(mt, octet_buf, r->u.octet_aligned->len,
                                         &result, &rlen)> 0)
                 {
-                    fwrite(result, rlen, 1, stdout);
+                    if (fwrite(result, rlen, 1, stdout) != 1)
+                    {
+                        printf("write to stdout failed\n");
+                    }
                 }
                 else
                 {
@@ -961,8 +970,7 @@ static void display_record(Z_External *r)
                              r->u.octet_aligned->len);
             }
         }
-        if (marc_file)
-            fwrite(octet_buf, 1, r->u.octet_aligned->len, marc_file);
+        marc_file_write(octet_buf, r->u.octet_aligned->len);
     }
     else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
     {
@@ -972,8 +980,7 @@ static void display_record(Z_External *r)
             return;
         }
         print_record(r->u.sutrs->buf, r->u.sutrs->len);
-        if (marc_file)
-            fwrite(r->u.sutrs->buf, 1, r->u.sutrs->len, marc_file);
+        marc_file_write(r->u.sutrs->buf, r->u.sutrs->len);
     }
     else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
     {
@@ -3524,35 +3531,41 @@ int cmd_source(const char* arg, int echo )
     FILE* includeFile;
     char line[102400], *cp;
 
-    if(strlen(arg)<1) {
-        fprintf(stderr,"Error in source command use a filename\n");
+    if (strlen(arg) < 1)
+    {
+        fprintf(stderr, "Error in source command use a filename\n");
         return -1;
     }
 
     includeFile = fopen(arg, "r");
 
-    if(!includeFile) {
-        fprintf(stderr,"Unable to open file %s for reading\n",arg);
+    if (!includeFile)
+    {
+        fprintf(stderr, "Unable to open file %s for reading\n",arg);
         return -1;
     }
 
-    while(!feof(includeFile)) {
-        memset(line,0,sizeof(line));
-        fgets(line,sizeof(line),includeFile);
+    while (!feof(includeFile)) {
+        memset(line, 0, sizeof(line));
+        if (!fgets(line, sizeof(line), includeFile))
+        {
+            perror("fgets");
+            break;
+        }
 
-        if(strlen(line) < 2) continue;
-        if(line[0] == '#') continue;
+        if (strlen(line) < 2) continue;
+        if (line[0] == '#') continue;
 
         if ((cp = strrchr(line, '\n')))
             *cp = '\0';
 
-        if( echo ) {
-            printf( "processing line: %s\n",line );
-        };
+        if (echo)
+            printf("processing line: %s\n", line);
         process_cmd_line(line);
     }
 
-    if(fclose(includeFile)<0) {
+    if (fclose(includeFile))
+    {
         perror("unable to close include file");
         exit(1);
     }
@@ -3574,12 +3587,12 @@ int cmd_source_noecho(const char* arg)
 
 int cmd_subshell(const char* args)
 {
-    if(strlen(args))
-        system(args);
-    else
-        system(getenv("SHELL"));
-
+    int ret = system(strlen(args) ? args : getenv("SHELL"));
     printf("\n");
+    if (ret)
+    {
+        printf("Exit %d\n", ret);
+    }
     return 1;
 }
 
@@ -3710,6 +3723,16 @@ int cmd_set_marcdump(const char* arg)
     return 1;
 }
 
+static void marc_file_write(const char *buf, size_t sz)
+{
+    if (marc_file)
+    {
+        if (fwrite(buf, 1, sz, marc_file) != sz)
+        {
+            perror("marcfile write");
+        }
+    }
+}
 /*
    this command takes 3 arge {name class oid}
 */
@@ -3890,9 +3913,13 @@ static void handle_srw_record(Z_SRW_record *rec)
     printf("\n");
     if (rec->recordData_buf && rec->recordData_len)
     {
-        fwrite(rec->recordData_buf, 1, rec->recordData_len, stdout);
-        if (marc_file)
-            fwrite(rec->recordData_buf, 1, rec->recordData_len, marc_file);
+        if (fwrite(rec->recordData_buf, 1, rec->recordData_len, stdout) !=
+            rec->recordData_len)
+        {
+            printf("write to stdout failed\n");
+        }
+        printf("%.*s", rec->recordData_len, rec->recordData_buf);
+        marc_file_write(rec->recordData_buf, rec->recordData_len);
     }
     else
         printf("No data!");
index 82fe6cf..1824ef0 100644 (file)
@@ -246,7 +246,10 @@ int yaz_daemon(const char *progname,
         close(1);
         close(2);
         open("/dev/null", O_RDWR);
-        dup(0); dup(0);
+        if (dup(0) == -1)
+            return 1;
+        if (dup(0) == -1)
+            return 1;
         close(hand[1]);
     }
 
index 2a3ff0e..9c791d1 100644 (file)
@@ -1122,9 +1122,7 @@ static void srw_bend_search(association *assoc, request *req,
             querystr = srw_req->query.pqf;
             break;
         }
-        wrbuf_printf(wr, "SRWSearch ");
-        wrbuf_printf(wr, srw_req->database);
-        wrbuf_printf(wr, " ");
+        wrbuf_printf(wr, "SRWSearch %s ", srw_req->database);
         if (srw_res->num_diagnostics)
             wrbuf_printf(wr, "ERROR %s", srw_res->diagnostics[0].uri);
         else if (*http_code != 200)
@@ -1394,9 +1392,7 @@ static void srw_bend_scan(association *assoc, request *req,
             querystr = "";
         }
 
-        wrbuf_printf(wr, "SRWScan ");
-        wrbuf_printf(wr, srw_req->database);
-        wrbuf_printf(wr, " ");
+        wrbuf_printf(wr, "SRWScan %s ", srw_req->database);
 
         if (srw_res->num_diagnostics)
             wrbuf_printf(wr, "ERROR %s - ", srw_res->diagnostics[0].uri);
@@ -1672,7 +1668,8 @@ static char *read_file(const char *fname, ODR o, int *sz)
     *sz = ftell(inf);
     rewind(inf);
     buf = (char *) odr_malloc(o, *sz);
-    fread(buf, 1, *sz, inf);
+    if (fread(buf, 1, *sz, inf) != *sz)
+        yaz_log(YLOG_WARN|YLOG_ERRNO, "short read %s", fname);
     fclose(inf);
     return buf;     
 }
@@ -2837,7 +2834,7 @@ static Z_APDU *response_searchRequest(association *assoc, request *reqb,
         for (i = 0 ; i < req->num_databaseNames; i++){
             if (i)
                 wrbuf_printf(wr, "+");
-            wrbuf_printf(wr, req->databaseNames[i]);
+            wrbuf_puts(wr, req->databaseNames[i]);
         }
         wrbuf_printf(wr, " ");
         
@@ -3136,7 +3133,7 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
         {
             if (i)
                 wrbuf_printf(wr, "+");
-            wrbuf_printf(wr, req->databaseNames[i]);
+            wrbuf_puts(wr, req->databaseNames[i]);
         }
 
         wrbuf_printf(wr, " ");
@@ -3233,7 +3230,7 @@ static Z_APDU *process_sortRequest(association *assoc, request *reqb,
         {
             if (i)
                 wrbuf_printf(wr, "+");
-            wrbuf_printf(wr, req->inputResultSetNames[i]);
+            wrbuf_puts(wr, req->inputResultSetNames[i]);
         }
         wrbuf_printf(wr, ")->%s ",req->sortedResultSetName);
 
index 00ba31c..59509ce 100644 (file)
@@ -1184,7 +1184,11 @@ statserv_options_block *statserv_getcontrol(void)
 
 void statserv_setcontrol(statserv_options_block *block)
 {
-    chdir(gfs_root_dir);
+    if (gfs_root_dir[0])
+    {
+        if (chdir(gfs_root_dir))
+            yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s", gfs_root_dir);
+    }
 #ifdef WIN32
     if (init_control_tls)
         TlsSetValue(current_control_tls, block);
@@ -1282,7 +1286,10 @@ static int statserv_sc_main(yaz_sc_t s, int argc, char **argv)
             close(1);
             close(2);
             open("/dev/null", O_RDWR);
-            dup(0); dup(0);
+            if (dup(0) == -1)
+                return 1;
+            if (dup(0) == -1)
+                return 1;
         }
         xml_config_add_listeners();
 
index 5d6cad1..268deba 100644 (file)
@@ -425,8 +425,16 @@ static int unix_bind(COMSTACK h, void *address, int mode)
         h->cerrno = CSYSERR;
         return -1;
     }
-    chown(path, sp->uid, sp->gid);
-    chmod(path, sp->umask != -1 ? sp->umask : 0666);
+    if (chown(path, sp->uid, sp->gid))
+    {
+        h->cerrno = CSYSERR;
+        return -1;
+    }
+    if (chmod(path, sp->umask != -1 ? sp->umask : 0666))
+    {
+        h->cerrno = CSYSERR;
+        return -1;
+    }
     if (mode == CS_SERVER && listen(h->iofile, 100) < 0)
     {
         h->cerrno = CSYSERR;
index ed2096a..662445e 100644 (file)
@@ -332,7 +332,11 @@ static void dump(const char *fname, const char *from, const char *to,
             r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result);
             if (r > 0 && result)
             {
-                fwrite (result, len_result, 1, stdout);
+                if (fwrite(result, len_result, 1, stdout) != 1)
+                {
+                    fprintf(stderr, "Write to stdout failed\n");
+                    break;
+                }
             }
             if (r > 0 && cfile)
             {
index 7de077c..9208cd6 100644 (file)
@@ -74,8 +74,11 @@ int main(int argc, char **argv)
                     {
                         fprintf (stderr, "%d\n", i);
                         if (res->records[i].recordData_buf)
-                            fwrite(res->records[i].recordData_buf, 1,
-                                   res->records[i].recordData_len, stderr);
+                        {
+                            fprintf(stderr, "%.*s",
+                                    res->records[i].recordData_len,
+                                    res->records[i].recordData_buf);
+                        }
                     }
                 }
             }
@@ -85,7 +88,9 @@ int main(int argc, char **argv)
     ret = z_soap_codec(encode, &soap_package,
                        &content_buf, &content_len, h);
     if (content_buf && content_len)
-        fwrite (content_buf, content_len, 1, stdout);
+    {
+        printf("%.*s", content_len, content_buf);
+    }
     else
     {
         fprintf(stderr, "No output!\n");
index 389f11d..b00a84a 100644 (file)
@@ -503,8 +503,17 @@ void sendrequest(ILL_APDU *apdu, ODR odr, COMSTACK stack ) {
     }
     if (1) {
         FILE *F = fopen("req.apdu","w");
-        fwrite ( buf_out, 1, len_out, F);
-        fclose(F);
+        if (!F)
+        {
+            yaz_log(YLOG_FATAL|YLOG_ERRNO, "open req.apdu failed");
+        }
+        else
+        {
+            if (fwrite ( buf_out, 1, len_out, F) != len_out)
+                yaz_log(YLOG_FATAL|YLOG_ERRNO, "write req.apdu failed");
+            if (fclose(F))
+                yaz_log(YLOG_FATAL|YLOG_ERRNO, "write req.apdu failed");
+        }
     }
     
 } /* sendrequest */
index b40ddaf..31579e3 100644 (file)
@@ -72,7 +72,13 @@ void pqftoxmlquery(const char *pqf)
                exit(4);
            }
            else
-               fwrite(buf_out, len_out, 1, stdout);
+           {
+               if (fwrite(buf_out, len_out, 1, stdout) != 1)
+               {
+                   fprintf(stderr, "%s: write failed\n", prog);
+                   exit(5);
+               }
+           }
             xmlFreeDoc(doc);
        }
     }    
@@ -143,8 +149,16 @@ void xmlfiletopqf(const char *xmlfile)
     rewind(f);
     xmlstr = (char *) xmalloc(sz+1);
     xmlstr[sz] = '\0';
-    fread(xmlstr, sz, 1, f);
-    fclose(f);
+    if (fread(xmlstr, sz, 1, f) != 1)
+    {
+       fprintf(stderr, "%s: read failed for file %s\n", prog, xmlfile);
+       exit(1);
+    }
+    if (fclose(f))
+    {
+       fprintf(stderr, "%s: close failed for file %s\n", prog, xmlfile);
+       exit(1);
+    }
     
     xmlquerytopqf(xmlstr);
     xfree(xmlstr);
index 9e966e9..e0bd124 100644 (file)
 #define CHUNK_IN 64
 #define CHUNK_OUT 64
 
+void write_out(const char *b0, const char *b1)
+{
+    size_t sz = b1 - b0;
+    if (sz)
+    {
+        if (fwrite(b0, 1, sz, stdout) != sz)
+        {
+            fprintf(stderr, "yaz-iconv: write failed\n");
+            exit(8);
+        }
+    }
+}
+
 void convert(FILE *inf, yaz_iconv_t cd, int verbose)
 {
     char inbuf0[CHUNK_IN], *inbuf = inbuf0;
@@ -35,18 +48,16 @@ void convert(FILE *inf, yaz_iconv_t cd, int verbose)
             {
                 if (ferror(inf))
                 {
-                    fprintf(stderr, "yaziconv: error reading file\n");
+                    fprintf(stderr, "yaz-iconv: error reading file\n");
                     exit(6);
                 }
                 if (r == 0)
                 {
-                    if (outbuf != outbuf0)
-                        fwrite(outbuf0, 1, outbuf - outbuf0, stdout);
+                    write_out(outbuf0, outbuf);
                     outbuf = outbuf0;
                     outbytesleft = CHUNK_OUT;
                     r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
-                    if (outbuf != outbuf0)
-                        fwrite(outbuf0, 1, outbuf - outbuf0, stdout);
+                    write_out(outbuf0, outbuf);
                     break;
                 }
                 inbytesleft = r;
@@ -78,7 +89,7 @@ void convert(FILE *inf, yaz_iconv_t cd, int verbose)
                 {
                     if (ferror(inf))
                     {
-                        fprintf(stderr, "yaziconv: error reading file\n");
+                        fprintf(stderr, "yaz-iconv: error reading file\n");
                         exit(6);
                     }
                 }
@@ -93,14 +104,14 @@ void convert(FILE *inf, yaz_iconv_t cd, int verbose)
             }
             else if (e == YAZ_ICONV_E2BIG) /* no more output space */
             {
-                fwrite(outbuf0, 1, outbuf - outbuf0, stdout);
+                write_out(outbuf0, outbuf);
                 outbuf = outbuf0;
                 outbytesleft = CHUNK_OUT;
                 mustread = 0;
             }
             else
             {
-                fprintf(stderr, "yaziconv: unknown error\n");
+                fprintf(stderr, "yaz-iconv: unknown error\n");
                 exit(7);
             }
         }
@@ -109,7 +120,7 @@ void convert(FILE *inf, yaz_iconv_t cd, int verbose)
             inbuf = inbuf0;
             inbytesleft = CHUNK_IN;
 
-            fwrite(outbuf0, 1, outbuf - outbuf0, stdout);
+            write_out(outbuf0, outbuf);
             outbuf = outbuf0;
             outbytesleft = CHUNK_OUT;
 
@@ -136,7 +147,7 @@ int main(int argc, char **argv)
             inf = fopen(arg, "rb");
             if (!inf)
             {
-                fprintf(stderr, "yaziconv: cannot open %s", arg);
+                fprintf(stderr, "yaz-iconv: cannot open %s", arg);
                 exit(2);
             }
             break;
@@ -150,32 +161,32 @@ int main(int argc, char **argv)
             verbose++;
             break;
         default:
-            fprintf(stderr, "yaziconv: Usage\n"
+            fprintf(stderr, "yaz-iconv: Usage\n"
                     "yaziconv -f encoding -t encoding [-v] [file]\n");
             exit(1);
         }
     }
     if (!to)
     {
-        fprintf(stderr, "yaziconv: -t encoding missing\n");
+        fprintf(stderr, "yaz-iconv: -t encoding missing\n");
         exit(3);
     }
     if (!from)
     {
-        fprintf(stderr, "yaziconv: -f encoding missing\n");
+        fprintf(stderr, "yaz-iconv: -f encoding missing\n");
         exit(4);
     }
     cd = yaz_iconv_open(to, from);
     if (!cd)
     {
-        fprintf(stderr, "yaziconv: unsupported encoding\n");
+        fprintf(stderr, "yaz-iconv: unsupported encoding\n");
         exit(5);
     }
     else
     {
         if (verbose)
         {
-            fprintf(stderr, "yaziconv: using %s\n",
+            fprintf(stderr, "yaz-iconv: using %s\n",
                     yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv");
         }
     }
index f575294..6a376c7 100644 (file)
@@ -206,10 +206,18 @@ static void display_records(ZOOM_connection c,
                        pos, (db ? db : "unknown"), syntax,
                        schema ? schema : "unknown");
                 if (render)
-                    fwrite(render, 1, len, stdout);
+                {
+                    if (write(render, 1, len, stdout) != len)
+                    {
+                        printf("write to stdout failed\n");
+                    }
+                }
                 printf("\n");
                 if (opac_render)
-                    fwrite(opac_render, 1, opac_len, stdout);
+                {
+                    if (fwrite(opac_render, 1, opac_len, stdout) != opac_len)
+                        printf("write to stdout failed\n");
+                }
             }
         }
     }
@@ -429,8 +437,8 @@ static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
                 int len = 0;
                 const char *term = ZOOM_scanset_display_term(s[i], p,
                                                              &occ, &len);
-                fwrite(term, 1, len, stdout);
-                printf(" %d\n", occ);
+                
+                printf("%.*s %d\n", len, term, occ);
             }            
             ZOOM_scanset_destroy(s[i]);
         }
index 8873db8..1c8fb9d 100644 (file)
@@ -95,12 +95,7 @@ int main(int argc, char **argv)
                         ZOOM_resultset_record (r[i], pos), "render", &len);
                 /* if rec is non-null, we got a record for display */
                 if (rec)
-                {
-                    printf ("%d\n", pos+1);
-                    if (rec)
-                        fwrite (rec, 1, len, stdout);
-                    printf ("\n");
-                }
+                    printf ("%d\n%.*s\n", pos+1, len, rec);
             }
         }
     }
index 0203670..14ee88b 100644 (file)
@@ -104,7 +104,10 @@ int main(int argc, char **argv)
                     printf ("%d %s %s\n", pos+1, syntax, 
                             (db ? db : "unknown"));
                     if (rec)
-                        fwrite (str, 1, len, stdout);
+                    {
+                        if (fwrite (str, 1, len, stdout) != len)
+                            printf("write to stdout failed\n");
+                    }
                     printf ("\n");
                 }
             }
index 931e399..cce5ea1 100644 (file)
@@ -29,7 +29,10 @@ static void display_records (const char *tname, ZOOM_resultset r)
             {
                 printf ("%d %s\n", pos+1, (db ? db : "unknown"));
                 if (render)
-                    fwrite (render, 1, len, stdout);
+                {
+                    if (fwrite (render, 1, len, stdout) != len)
+                        printf("write to stdout failed\n");
+                }
                 printf ("\n");
             }
         }