hal/hald util.c,1.10,1.11

David Zeuthen david at freedesktop.org
Thu Mar 10 11:44:14 PST 2005


Update of /cvs/hal/hal/hald
In directory gabe:/tmp/cvs-serv11128/hald

Modified Files:
	util.c 
Log Message:
2005-03-10  David Zeuthen  <davidz at redhat.com>

	Bah, so it turns out that crappy BIOS'es and what-have-you gets really
	upset of hald, every five seconds, accessing the state file three
	times and the info file one time (in /proc/acpi/battery/BAT%d)

	https://www.redhat.com/archives/fedora-devel-list/2005-March/msg00704.html

	So, off to read the battstat applet source - fix this up so we
	only access a single file every 30 seconds; just like battstat applet.

	I really hate hardware.

	* hald/linux2/acpi.c (ACPI_POLL_INTERVAL): Only poll every 30 seconds
	(battery_refresh_poll): New function
	(battery_refresh): Call battery_refresh_poll
	(acpi_poll): Call battery_refresh_poll instead of acpi_rescan_device

	* hald/util.c (hal_util_grep_file): Fix this so the last read buffer
	is cached for reuse



Index: util.c
===================================================================
RCS file: /cvs/hal/hal/hald/util.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- util.c	7 Mar 2005 19:08:41 -0000	1.10
+++ util.c	10 Mar 2005 19:44:11 -0000	1.11
@@ -673,11 +673,13 @@
 gchar *
 hal_util_grep_file (const gchar *directory, const gchar *file, const gchar *linestart, gboolean reuse)
 {
-	FILE *f;
-	static gchar buf[512];
+	static gchar buf[2048];
+	static unsigned int bufsize;
 	static gchar filename[HAL_PATH_MAX];
+	static gchar oldfilename[HAL_PATH_MAX];
 	gchar *result;
 	gsize linestart_len;
+	gchar *p;
 
 	result = NULL;
 
@@ -689,36 +691,54 @@
 		snprintf (filename, sizeof (filename), "%s/%s", directory, file);
 	else
 		strncpy (filename, directory, sizeof (filename));
-	f = fopen (filename, "r");
-	if (f == NULL)
-		goto out;
+
+	if (_grep_can_reuse && reuse && strcmp (oldfilename, filename) == 0) {
+		/* just reuse old file; e.g. bufsize, buf */
+		HAL_INFO (("hal_util_grep_file: reusing buf for %s", filename));
+	} else {
+		FILE *f;
+
+		f = fopen (filename, "r");
+		if (f == NULL)
+			goto out;
+		bufsize = fread (buf, sizeof (char), sizeof (buf) - 1, f);
+		buf[bufsize] = '\0';
+		fclose (f);
+
+		HAL_INFO (("hal_util_grep_file: read %s of %d bytes", filename, bufsize));
+	}
+
+	/* book keeping */
+	_grep_can_reuse = TRUE;
+	strncpy (oldfilename, filename, sizeof(oldfilename));
 
 	linestart_len = strlen (linestart);
 
+	/* analyze buf */
+	p = buf;
 	do {
-		if (fgets (buf, sizeof (buf), f) == NULL)
-			goto out;
+		unsigned int linelen;
+		static char line[256];
 
-		if (strncmp (buf, linestart, linestart_len) == 0) {
-			guint i;
-			gsize len;
+		for (linelen = 0; p[linelen] != '\n' && p[linelen] != '\0'; linelen++)
+			;
 
-			len = strlen (buf);
-			for (i = len - 1; i > 0; --i) {
-				if (buf[i] == '\n' || buf[i] == '\r')
-					buf[i] = '\0';
-				else
-					break;
+		if (linelen < sizeof (line)) {
+
+			strncpy (line, p, linelen);
+			line[linelen] = '\0';
+
+			if (strncmp (line, linestart, linestart_len) == 0) {
+				result = line + linestart_len;
+				goto out;
 			}
-			break;
 		}
-	} while (TRUE);
 
-	result = buf + linestart_len;
+		p += linelen + 1;
+
+	} while (p < buf + bufsize);
 
 out:
-	if (f != NULL)
-		fclose (f);
 	return result;
 }
 




More information about the hal-commit mailing list