Added silc_fd_stream_file2.
authorPekka Riikonen <priikone@silcnet.org>
Sun, 8 Apr 2007 09:56:52 +0000 (09:56 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 8 Apr 2007 09:56:52 +0000 (09:56 +0000)
lib/silcutil/silcfdstream.c
lib/silcutil/silcfdstream.h

index 5a552847e0702a7c68b77906ffa2a1bdceea05ab..a618c63421c1c5921df3ff17b49f053010e48602 100644 (file)
@@ -80,9 +80,6 @@ SilcStream silc_fd_stream_create2(int read_fd, int write_fd)
 {
   SilcFDStream stream;
 
-  if (read_fd < 1)
-    return NULL;
-
   stream = silc_calloc(1, sizeof(*stream));
   if (!stream)
     return NULL;
@@ -98,31 +95,52 @@ SilcStream silc_fd_stream_create2(int read_fd, int write_fd)
 
 /* Create by opening file */
 
-SilcStream silc_fd_stream_file(const char *filename,
-                              SilcBool reading, SilcBool writing)
+SilcStream silc_fd_stream_file(const char *filename, SilcBool reading,
+                              SilcBool writing)
 {
-  int fd, flags = 0;
-  SilcStream stream;
+  const char *read_file = NULL, *write_file = NULL;
 
   if (!filename)
     return NULL;
 
-  SILC_LOG_DEBUG(("Creating new fd stream for file `%s'", filename));
-
-  if (reading)
-    flags |= O_RDONLY;
   if (writing)
-    flags |= O_CREAT | O_WRONLY;
-  if (reading && writing)
-    flags = O_CREAT | O_RDWR;
+    write_file = filename;
+  if (reading)
+    read_file = filename;
 
-  fd = silc_file_open(filename, flags);
-  if (fd < 0)
-    return NULL;
+  return silc_fd_stream_file2(read_file, write_file);
+}
 
-  stream = silc_fd_stream_create(fd);
-  if (!stream)
-    silc_file_close(fd);
+/* Create by opening two files */
+
+SilcStream silc_fd_stream_file2(const char *read_file, const char *write_file)
+{
+  SilcStream stream;
+  int fd1 = 0, fd2 = 0;
+
+  SILC_LOG_DEBUG(("Creating new fd stream for reading `%s' and writing `%s'",
+                 read_file ? read_file : "(none)",
+                 write_file ? write_file : "(none)"));
+
+  if (write_file) {
+    fd2 = silc_file_open(write_file, O_CREAT | O_WRONLY);
+    if (fd2 < 0) {
+      silc_file_close(fd1);
+      return NULL;
+    }
+  }
+
+  if (read_file) {
+    fd1 = silc_file_open(read_file, O_RDONLY);
+    if (fd1 < 0)
+      return NULL;
+  }
+
+  stream = silc_fd_stream_create2(fd1, fd2);
+  if (!stream) {
+    silc_file_close(fd1);
+    silc_file_close(fd2);
+  }
 
   return stream;
 }
@@ -290,7 +308,7 @@ SilcBool silc_fd_stream_notifier(SilcStream stream,
                                silc_fd_stream_io, stream);
       silc_schedule_set_listen_fd(schedule, fd_stream->fd1,
                                  SILC_TASK_READ, FALSE);
-      silc_file_set_nonblock(fd_stream->fd1);;
+      silc_file_set_nonblock(fd_stream->fd1);
       if (fd_stream->fd2 < 1)
        fd_stream->fd2 = fd_stream->fd1;
     }
index f5a95e3a6f50b6a7b60b7e2ef2f46c162b51fb76..cc2c587e41d9b7c2fe90f5f502dd3059c29f0040 100644 (file)
@@ -46,8 +46,7 @@
  *    by `fd'.  The stream is closed with the silc_stream_close and destroyed
  *    with the silc_stream_destroy.
  *
- *    The silc_stream_set_notifier must be called in order to be able to read
- *    from and write to this file descriptor stream if the `fd' is in
+ *    If the silc_stream_set_notifier is called the stream is set to
  *    non-blocking mode.
  *
  ***/
@@ -66,8 +65,7 @@ SilcStream silc_fd_stream_create(int fd);
  *    and `write_fd' opened for writing.  The stream is closed with the
  *    silc_stream_close and destroyed with the silc_stream_destroy.
  *
- *    The silc_stream_set_notifier must be called in order to be able to read
- *    from and write to this file descriptor stream if the `fd' is in
+ *    If the silc_stream_set_notifier is called the stream is set to
  *    non-blocking mode.
  *
  ***/
@@ -87,14 +85,31 @@ SilcStream silc_fd_stream_create2(int read_fd, int write_fd);
  *    opened for reading.  If the `writing' is TRUE the file is opened
  *    for writing.
  *
- *    The silc_stream_set_notifier must be called in order to be able to read
- *    from and write to this file descriptor stream if the `fd' is in
+ *    If the silc_stream_set_notifier is called the stream is set to
  *    non-blocking mode.
  *
  ***/
 SilcStream silc_fd_stream_file(const char *filename, SilcBool reading,
                               SilcBool writing);
 
+/****f* silcutil/SilcFDStreamAPI/silc_fd_stream_file
+ *
+ * SYNOPSIS
+ *
+ *    SilcStream silc_fd_stream_file2(const char *read_file,
+ *                                    const char *write_file);
+ *
+ * DESCRIPTION
+ *
+ *    Same as silc_fd_stream_file but creates the stream by opening `read_file'
+ *    for reading and `write_file' for writing.
+ *
+ *    If the silc_stream_set_notifier is called the stream is set to
+ *    non-blocking mode.
+ *
+ ***/
+SilcStream silc_fd_stream_file2(const char *read_file, const char *write_file);
+
 /****f* silcutil/SilcFDStreamAPI/silc_fd_stream_get_info
  *
  * SYNOPSIS