updates.
[silc.git] / lib / silcutil / silcutil.c
index 6bc8261b1d0a8c90a252ccfd32cc5bf90bd5250f..9d1ab991b5311cdb6157913aafa8da8b4a57b660 100644 (file)
 
 #include "silcincludes.h"
 
-/* Reads a file to a buffer. The allocated buffer is returned. Length of
-   the file read is returned to the return_len argument. */
+/* Opens a file indicated by the filename `filename' with flags indicated
+   by the `flags'. */
 
-char *silc_file_read(const char *filename, uint32 *return_len)
+int silc_file_open(const char *filename, int flags)
 {
   int fd;
-  char *buffer;
-  int filelen;
 
-  fd = open(filename, O_RDONLY);
-  if (fd < 0) {
-    if (errno == ENOENT)
-      return NULL;
-    SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
-    return NULL;
-  }
+  fd = open(filename, flags);
 
-  filelen = lseek(fd, (off_t)0L, SEEK_END);
-  if (filelen < 0) {
-    close(fd);
-    return NULL;
-  }
-  if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
-    close(fd);
-    return NULL;
-  }
+  return fd;
+}
 
-  if (filelen < 0) {
-    SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
-    close(fd);
-    return NULL;
-  }
-  
-  buffer = silc_calloc(filelen + 1, sizeof(char));
-  
-  if ((read(fd, buffer, filelen)) == -1) {
-    memset(buffer, 0, sizeof(buffer));
-    close(fd);
-    SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
-                    strerror(errno)));
-    return NULL;
-  }
+/* Reads data from file descriptor `fd' to `buf'. */
 
-  close(fd);
-  buffer[filelen] = EOF;
+int silc_file_read(int fd, unsigned char *buf, uint32 buf_len)
+{
+  return read(fd, (void *)buf, buf_len);
+}
 
-  if (return_len)
-    *return_len = filelen;
+/* Writes `buffer' of length of `len' to file descriptor `fd. */
 
-  return buffer;
+int silc_file_write(int fd, const char *buffer, uint32 len)
+{
+  return write(fd, (const void *)buffer, len);
+}
+
+/* Closes file descriptor */
+
+int silc_file_close(int fd)
+{
+  return close(fd);
 }
 
 /* Writes a buffer to the file. */
 
-int silc_file_write(const char *filename, const char *buffer, uint32 len)
+int silc_file_writefile(const char *filename, const char *buffer, uint32 len)
 {
   int fd;
         
@@ -103,8 +84,8 @@ int silc_file_write(const char *filename, const char *buffer, uint32 len)
 /* Writes a buffer to the file.  If the file is created specific mode is
    set to the file. */
 
-int silc_file_write_mode(const char *filename, const char *buffer, 
-                        uint32 len, int mode)
+int silc_file_writefile_mode(const char *filename, const char *buffer, 
+                            uint32 len, int mode)
 {
   int fd;
         
@@ -125,6 +106,58 @@ int silc_file_write_mode(const char *filename, const char *buffer,
   return 0;
 }
 
+/* Reads a file to a buffer. The allocated buffer is returned. Length of
+   the file read is returned to the return_len argument. */
+
+char *silc_file_readfile(const char *filename, uint32 *return_len)
+{
+  int fd;
+  char *buffer;
+  int filelen;
+
+  fd = silc_file_open(filename, O_RDONLY);
+  if (fd < 0) {
+    if (errno == ENOENT)
+      return NULL;
+    SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
+    return NULL;
+  }
+
+  filelen = lseek(fd, (off_t)0L, SEEK_END);
+  if (filelen < 0) {
+    close(fd);
+    return NULL;
+  }
+  if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
+    close(fd);
+    return NULL;
+  }
+
+  if (filelen < 0) {
+    SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
+    close(fd);
+    return NULL;
+  }
+  
+  buffer = silc_calloc(filelen + 1, sizeof(char));
+  
+  if ((read(fd, buffer, filelen)) == -1) {
+    memset(buffer, 0, sizeof(buffer));
+    close(fd);
+    SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
+                    strerror(errno)));
+    return NULL;
+  }
+
+  close(fd);
+  buffer[filelen] = EOF;
+
+  if (return_len)
+    *return_len = filelen;
+
+  return buffer;
+}
+
 /* Gets line from a buffer. Stops reading when a newline or EOF occurs.
    This doesn't remove the newline sign from the destination buffer. The
    argument begin is returned and should be passed again for the function. */