changeset 52:2e1ea58a4692

Fixes for Unix/gcc and general safety of INI file loading.
author Brian Smith <brian@dbsoft.org>
date Mon, 09 Apr 2012 05:08:03 -0500
parents 6afd9bcf7ef9
children e2eeb0af551c
files Makefile.in cc.c statsunx.c
diffstat 3 files changed, 65 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.in	Mon Apr 09 03:46:48 2012 -0500
+++ b/Makefile.in	Mon Apr 09 05:08:03 2012 -0500
@@ -15,7 +15,7 @@
 include Version
 VER_REV=@MERCURIAL@
 
-CFLAGS = @CFLAGS@ @ARCH@ -DVER_MAJ=$(VER_MAJ) -DVER_MIN=$(VER_MIN) -DVER_REV=$(VER_REV) -D__UNIX__ -DDW_RESOURCES
+CFLAGS = @CFLAGS@ @ARCH@ -D__TARGET__=\"$(TARGET)\" -DVER_MAJ=$(VER_MAJ) -DVER_MIN=$(VER_MIN) -DVER_REV=$(VER_REV) -D__UNIX__ -DDW_RESOURCES
 LDFLAGS = @LDFLAGS@ -ldwindows -ldwcompat
 
 TARGET=cc
--- a/cc.c	Mon Apr 09 03:46:48 2012 -0500
+++ b/cc.c	Mon Apr 09 05:08:03 2012 -0500
@@ -85,7 +85,7 @@
 	{ "COLOR_GRID",			TYPE_ULONG,	&(current_colors[COLOR_GRID]) },
 	{ "COLOR_RECV",			TYPE_ULONG,	&(current_colors[COLOR_RECV]) },
 	{ "COLOR_SENT",			TYPE_ULONG,	&(current_colors[COLOR_SENT]) },
-	{ 0, 0, 0}
+	{ "", 0, 0}
 };
 
 unsigned long Sent = 0, Recv = 0, TotalSent = 0, TotalRecv = 0, MaxSent = 0, MaxRecv = 0;
@@ -226,26 +226,32 @@
 	int z;
 
 	memset(in, 0, INI_BUFFER);
-	fgets(in, INI_BUFFER - 1, f);
 
-	if(in[strlen(in)-1] == '\n')
-		in[strlen(in)-1] = 0;
-
-	if(in[0] != '#')
+	if(fgets(in, INI_BUFFER - 1, f))
 	{
-		for(z=0;z<strlen(in);z++)
+		int len = strlen(in);
+	   
+		if(len > 0 && in[len-1] == '\n')
+			in[len-1] = 0;
+
+		if(in[0] != '#')
 		{
-			if(in[z] == '=')
+			len = strlen(in);
+	      
+			for(z=0;z<len;z++)
 			{
-				in[z] = 0;
-				strcpy(entry, in);
-				strcpy(entrydata, &in[z+1]);
-				return;
+				if(in[z] == '=')
+				{
+					in[z] = 0;
+					strcpy(entry, in);
+					strcpy(entrydata, &in[z+1]);
+					return;
+				}
 			}
 		}
 	}
-	strcpy(entry, "");
-	strcpy(entrydata, "");
+	entry[0] = 0;
+	entrydata[0] = 0;
 }
 
 /* Load the cc.ini file from disk setting all the necessary flags */
--- a/statsunx.c	Mon Apr 09 03:46:48 2012 -0500
+++ b/statsunx.c	Mon Apr 09 05:08:03 2012 -0500
@@ -77,21 +77,21 @@
 		char buf[1024];
 		int user, nice, sys, idle, used, total;
 
-		fgets(buf, 1024, fp);
+		if(fgets(buf, 1024, fp))
+		{
+		   sscanf(buf, "cpu %d %d %d %d", &user, &nice, &sys, &idle);
 
-		sscanf(buf, "cpu %d %d %d %d", &user, &nice, &sys, &idle);
-
-		used = (user+nice+sys);
-		total = used + idle;
+		   used = (user+nice+sys);
+		   total = used + idle;
 
-		if(lasttotal && (total - lasttotal))
-			lastload = *Load = (double)(used-lastused)/(total-lasttotal);
-		else
-			*Load = lastload;
+		   if(lasttotal && (total - lasttotal))
+			   lastload = *Load = (double)(used-lastused)/(total-lasttotal);
+		   else
+			   *Load = lastload;
 
-		lastused = used;
-		lasttotal = total;
-
+		   lastused = used;
+		   lasttotal = total;
+      }
 		fclose(fp);
 	}
 #elif defined(__FreeBSD__)
@@ -203,41 +203,42 @@
 			char *ifacename;
 			int packets, bytes, errs, drop, fifo, frame, compressed, multicast, sbytes, serrs;
 
-			fgets(buf, 1024, fp);
-
-			tmp = buf;
-
-			/* Find the first interface entry */
-			while(*tmp == ' ')
-				tmp++;
-			/* Save the start of the name */
-			ifacename = tmp;
-			/* Find the end of the interface name */
-			while(*tmp && *tmp != ':')
-				tmp++;
-
-			/* Looks like we found something! */
-			if(*tmp == ':' && if_unit < 16)
+			if(fgets(buf, 1024, fp))
 			{
-				/* Terminate the interface name */
-				*tmp = 0;
+			   tmp = buf;
 
-				tmp++;
+			   /* Find the first interface entry */
+			   while(*tmp == ' ')
+				   tmp++;
+			   /* Save the start of the name */
+			   ifacename = tmp;
+			   /* Find the end of the interface name */
+			   while(*tmp && *tmp != ':')
+				   tmp++;
 
-				sscanf(tmp, "%d %d %d %d %d %d %d %d %d %d", &bytes, &packets, &errs, &drop, &fifo, &frame,
-					   &compressed, &multicast, &sbytes, &serrs);
+			   /* Looks like we found something! */
+			   if(*tmp == ':' && if_unit < 16)
+			   {
+				   /* Terminate the interface name */
+				   *tmp = 0;
 
-				if(!firsttime)
-				{
-					*Recv += bytes - ulTotalIn[if_unit];
-					*Sent += sbytes - ulTotalOut[if_unit];
-				}
-				ulTotalOut[if_unit] = sbytes;
-				*TotalSent += sbytes;
-				ulTotalIn[if_unit] = bytes;
-				*TotalRecv += bytes;
-				if_unit++;
-			}
+				   tmp++;
+
+				   sscanf(tmp, "%d %d %d %d %d %d %d %d %d %d", &bytes, &packets, &errs, &drop, &fifo, &frame,
+					      &compressed, &multicast, &sbytes, &serrs);
+
+				   if(!firsttime)
+				   {
+					   *Recv += bytes - ulTotalIn[if_unit];
+					   *Sent += sbytes - ulTotalOut[if_unit];
+				   }
+				   ulTotalOut[if_unit] = sbytes;
+				   *TotalSent += sbytes;
+				   ulTotalIn[if_unit] = bytes;
+				   *TotalRecv += bytes;
+				   if_unit++;
+			   }
+		   }
 		}
 		firsttime = 0;
 		fclose(fp);