From 9f0ad28628acc008e5a09728f94a0d0bb03ad8be Mon Sep 17 00:00:00 2001 From: Jacek Jendrzej Date: Wed, 29 Jan 2020 23:35:31 +0100 Subject: [PATCH] libnet: fix compil warning Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/0a14475cfc11035b36c923c51c77de6e3a61e615 Author: Jacek Jendrzej Date: 2020-01-29 (Wed, 29 Jan 2020) ------------------ No further description and justification available within origin commit message! ------------------ This commit was generated by Migit --- lib/libnet/libnet.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/libnet/libnet.cpp b/lib/libnet/libnet.cpp index 8c683f76e..d55f3eed8 100644 --- a/lib/libnet/libnet.cpp +++ b/lib/libnet/libnet.cpp @@ -172,8 +172,8 @@ void netGetDefaultRoute( std::string &ip ) fp = fopen("/proc/net/route","r"); if (fp == NULL) return; - fgets(zeile,sizeof(zeile),fp); /* skip header */ - while(fgets(zeile,sizeof(zeile),fp)) + char *fg = fgets(zeile,sizeof(zeile),fp); /* skip header */ + while(fg && (fg = fgets(zeile,sizeof(zeile),fp))) { destination = 1; /* in case sscanf fails */ sscanf(zeile,"%8s %x %x", interface, &destination, &gw); @@ -218,13 +218,18 @@ void netGetHostname( std::string &host ) void netSetHostname( std::string &host ) { - FILE * fp; - - sethostname(host.c_str(), host.length()); - fp = fopen("/etc/hostname", "w"); - if(fp != NULL) { - fprintf(fp, "%s\n", host.c_str()); - fclose(fp); + if (sethostname(host.c_str(), host.length()) < 0) + { + fprintf(stderr, "error set %s\n", host.c_str()); + } + else + { + FILE * fp = fopen("/etc/hostname", "w"); + if(fp != NULL) + { + fprintf(fp, "%s\n", host.c_str()); + fclose(fp); + } } }