All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 2/2] cld: read the cld.port file using g_file_get_contents
@ 2009-11-28  2:23 Colin McCabe
  2009-11-28 10:37 ` Jeff Garzik
  2009-11-28 17:58 ` Pete Zaitcev
  0 siblings, 2 replies; 6+ messages in thread
From: Colin McCabe @ 2009-11-28  2:23 UTC (permalink / raw)
  To: Project Hail List; +Cc: Pete Zaitcev, Jeff Garzik, Colin McCabe

Signed-off-by: Colin McCabe <cmccabe@alumni.cmu.edu>

---
 lib/common.c |   29 +++++++++++++++--------------
 1 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/lib/common.c b/lib/common.c
index 68f60f8..db20e2a 100644
--- a/lib/common.c
+++ b/lib/common.c
@@ -1,4 +1,5 @@
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -61,26 +62,26 @@ const char *cld_errstr(enum cle_err_codes ecode)
  */
 int cld_readport(const char *fname)
 {
-	enum { LEN = 11 };
-	char buf[LEN+1];
 	long port;
-	int fd;
-	int rc;
+	gchar *buf;
+	GError *err = NULL;
+	gsize len;
 
-	if ((fd = open(fname, O_RDONLY)) == -1)
-		return -errno;
-	rc = read(fd, buf, LEN);
-	close(fd);
-	if (rc < 0)
-		return -errno;
-	if (rc == 0)
-		return -EPIPE;
-	buf[rc] = 0;
+	if (!g_file_get_contents(fname, &buf, &len, &err)) {
+		fprintf(stderr, "Unable to read port file: %s\n",
+			 err->message);
+		g_error_free(err);
+		return -EIO;
+	}
 
+	if (len == 0) {
+		g_free(buf);
+		return -EPIPE;
+	}
 	port = strtol(buf, NULL, 10);
+	g_free(buf);
 	if (port <= 0 || port >= 65636)
 		return -EDOM;
 
 	return (int)port;
 }
-
-- 
1.6.2.5

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-11-28 23:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-28  2:23 [PATCH v2 2/2] cld: read the cld.port file using g_file_get_contents Colin McCabe
2009-11-28 10:37 ` Jeff Garzik
2009-11-28 10:49   ` Jeff Garzik
2009-11-28 17:58 ` Pete Zaitcev
2009-11-28 21:23   ` Jeff Garzik
2009-11-28 23:02     ` Pete Zaitcev

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.