about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2009-03-28T19·29+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2009-03-28T19·29+0000
commit3a2bbe7f8ad7ec8b2896ff5e666b8f5525691c6f (patch)
tree6c0b4a12f499f2cb6d1c0cc0f199cad0239b1566 /src/libutil
parent7fb548aa2621375559f980b4627955dbc6fe9914 (diff)
* Simplify communication with the hook a bit (don't use file
  descriptors 3/4, just use stdin/stderr).

Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc27
-rw-r--r--src/libutil/util.hh6
2 files changed, 33 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 02dd53a17991..1cb94215ee1b 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -229,6 +229,33 @@ void writeFile(const Path & path, const string & s)
 }
 
 
+string readLine(int fd)
+{
+    string s;
+    while (1) {
+        checkInterrupt();
+        char ch;
+        ssize_t rd = read(fd, &ch, 1);
+        if (rd == -1) {
+            if (errno != EINTR)
+                throw SysError("reading a line");
+        } else if (rd == 0)
+            throw Error("unexpected EOF reading a line");
+        else {
+            if (ch == '\n') return s;
+            s += ch;
+        }
+    }
+}
+
+
+void writeLine(int fd, string s)
+{
+    s += '\n';
+    writeFull(fd, (const unsigned char *) s.c_str(), s.size());
+}
+
+
 static void _computePathSize(const Path & path,
     unsigned long long & bytes, unsigned long long & blocks)
 {
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 33b06ca955df..5744e5692280 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -60,6 +60,12 @@ string readFile(const Path & path);
 /* Write a string to a file. */
 void writeFile(const Path & path, const string & s);
 
+/* Read a line from a file descriptor. */
+string readLine(int fd);
+
+/* Write a line to a file descriptor. */
+void writeLine(int fd, string s);
+
 /* Compute the sum of the sizes of all files in `path'. */
 void computePathSize(const Path & path,
     unsigned long long & bytes, unsigned long long & blocks);