Added SILC Server library.
[silc.git] / lib / silcutil / silcfileutil.c
index 4cb3e2f755a2753b006a24d71e029a93780fa1db..30b2a5e5ce2001df916e0a0ebb7b8f38a309af49 100644 (file)
@@ -1,10 +1,10 @@
 /*
 
-  silcfileutil.c 
+  silcfileutil.c
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2002 Pekka Riikonen
+  Copyright (C) 1997 - 2005 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
 */
 /* $Id$ */
 
-#include "silcincludes.h"
+#include "silc.h"
 
 /* Opens a file indicated by the filename `filename' with flags indicated
    by the `flags'. */
 
 int silc_file_open(const char *filename, int flags)
 {
-  int fd = open(filename, flags, 0600);
-  return fd;
+  return silc_file_open_mode(filename, flags, 0600);
 }
 
 /* Opens a file indicated by the filename `filename' with flags indicated
@@ -40,14 +39,14 @@ int silc_file_open_mode(const char *filename, int flags, int mode)
 
 /* Reads data from file descriptor `fd' to `buf'. */
 
-int silc_file_read(int fd, unsigned char *buf, uint32 buf_len)
+int silc_file_read(int fd, unsigned char *buf, SilcUInt32 buf_len)
 {
   return read(fd, (void *)buf, buf_len);
 }
 
-/* Writes `buffer' of length of `len' to file descriptor `fd. */
+/* Writes `buffer' of length of `len' to file descriptor `fd'. */
 
-int silc_file_write(int fd, const char *buffer, uint32 len)
+int silc_file_write(int fd, const char *buffer, SilcUInt32 len)
 {
   return write(fd, (const void *)buffer, len);
 }
@@ -61,41 +60,52 @@ int silc_file_close(int fd)
 
 /* Writes a buffer to the file. */
 
-int silc_file_writefile(const char *filename, const char *buffer, uint32 len)
+int silc_file_writefile(const char *filename, const char *buffer,
+                       SilcUInt32 len)
 {
   int fd;
-        
-  if ((fd = creat(filename, 0644)) == -1) {
+  int flags = O_CREAT | O_WRONLY | O_TRUNC;
+
+#if defined(O_BINARY)
+  flags |= O_BINARY;
+#endif /* O_BINARY */
+
+  if ((fd = open(filename, flags, 0644)) == -1) {
     SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
                    strerror(errno)));
     return -1;
   }
-  
-  if ((silc_file_write(fd, buffer, len)) == -1) {
+
+  if (silc_file_write(fd, buffer, len) == -1) {
     SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
     silc_file_close(fd);
     return -1;
   }
 
   silc_file_close(fd);
-  
+
   return 0;
 }
 
 /* Writes a buffer to the file.  If the file is created specific mode is
    set to the file. */
 
-int silc_file_writefile_mode(const char *filename, const char *buffer, 
-                            uint32 len, int mode)
+int silc_file_writefile_mode(const char *filename, const char *buffer,
+                            SilcUInt32 len, int mode)
 {
   int fd;
-        
-  if ((fd = creat(filename, mode)) == -1) {
+  int flags = O_CREAT | O_WRONLY | O_TRUNC;
+
+#if defined(O_BINARY)
+  flags |= O_BINARY;
+#endif /* O_BINARY */
+
+  if ((fd = open(filename, flags, mode)) == -1) {
     SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
                    strerror(errno)));
     return -1;
   }
-  
+
   if ((silc_file_write(fd, buffer, len)) == -1) {
     SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
     silc_file_close(fd);
@@ -103,14 +113,14 @@ int silc_file_writefile_mode(const char *filename, const char *buffer,
   }
 
   silc_file_close(fd);
-  
+
   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)
+char *silc_file_readfile(const char *filename, SilcUInt32 *return_len)
 {
   int fd;
   char *buffer;
@@ -139,9 +149,9 @@ char *silc_file_readfile(const char *filename, uint32 *return_len)
     silc_file_close(fd);
     return NULL;
   }
-  
+
   buffer = silc_calloc(filelen + 1, sizeof(char));
-  
+
   if ((silc_file_read(fd, buffer, filelen)) == -1) {
     memset(buffer, 0, sizeof(buffer));
     silc_file_close(fd);
@@ -159,9 +169,9 @@ char *silc_file_readfile(const char *filename, uint32 *return_len)
   return buffer;
 }
 
-/* Returns files size. Returns 0 on error. */
+/* Returns the size of `filename'. Returns 0 on error. */
 
-uint64 silc_file_size(const char *filename)
+SilcUInt64 silc_file_size(const char *filename)
 {
   int ret;
   struct stat stats;
@@ -174,5 +184,5 @@ uint64 silc_file_size(const char *filename)
   if (ret < 0)
     return 0;
 
-  return (uint64)stats.st_size;
+  return (SilcUInt64)stats.st_size;
 }