Browse Source

Moving grab_file

dinesh 17 years ago
parent
commit
909bf610f3
2 changed files with 31 additions and 4 deletions
  1. 27 0
      ccan/string/string.c
  2. 4 4
      tools/grab_file.c

+ 27 - 0
ccan/string/string.c

@@ -47,6 +47,15 @@ char *strjoin(const void *ctx, char *strings[], const char *delim)
 	return ret;
 }
 
+static int close_no_errno(int fd)
+{
+	int ret = 0, serrno = errno;
+	if (close(fd) < 0)
+		ret = errno;
+	errno = serrno;
+	return ret;
+}
+
 void *grab_fd(const void *ctx, int fd)
 {
 	int ret;
@@ -67,3 +76,21 @@ void *grab_fd(const void *ctx, int fd)
 
 	return buffer;
 }
+
+void *grab_file(const void *ctx, const char *filename)
+{
+	int fd;
+	char *buffer;
+
+	if (streq(filename, "-"))
+		fd = dup(STDIN_FILENO);
+	else
+		fd = open(filename, O_RDONLY, 0);
+
+	if (fd < 0)
+		return NULL;
+
+	buffer = grab_fd(ctx, fd);
+	close_no_errno(fd);
+	return buffer;
+}

+ 4 - 4
tools/grab_file.c

@@ -7,14 +7,14 @@
 #include <unistd.h>
 #include <errno.h>
 
-static int close_no_errno(int fd)
+/*static int close_no_errno(int fd)
 {
 	int ret = 0, serrno = errno;
 	if (close(fd) < 0)
 		ret = errno;
 	errno = serrno;
 	return ret;
-}
+}*/
 
 /*void *grab_fd(const void *ctx, int fd)
 {
@@ -38,7 +38,7 @@ static int close_no_errno(int fd)
 }*/
 
 /* This version adds one byte (for nul term) */
-void *grab_file(const void *ctx, const char *filename)
+/*void *grab_file(const void *ctx, const char *filename)
 {
 	int fd;
 	char *buffer;
@@ -54,5 +54,5 @@ void *grab_file(const void *ctx, const char *filename)
 	buffer = grab_fd(ctx, fd);
 	close_no_errno(fd);
 	return buffer;
-}
+}*/