Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[silc.git] / lib / silcutil / silcfileutil.c
index 7d2d14757281af64b6f1e7ae737fa72ed1cd7fe6..02620b04b877ee1a91829b24d3bd2cdf8aa2cb45 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2002 Pekka Riikonen
+  Copyright (C) 1997 - 2007 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
@@ -65,8 +64,13 @@ int silc_file_writefile(const char *filename, const char *buffer,
                        SilcUInt32 len)
 {
   int fd;
+  int flags = O_CREAT | O_WRONLY | O_TRUNC;
+
+#if defined(O_BINARY)
+  flags |= O_BINARY;
+#endif /* O_BINARY */
 
-  if ((fd = creat(filename, 0644)) == -1) {
+  if ((fd = open(filename, flags, 0644)) == -1) {
     SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
                    strerror(errno)));
     return -1;
@@ -78,9 +82,11 @@ int silc_file_writefile(const char *filename, const char *buffer,
     return -1;
   }
 
-  silc_file_close(fd);
+#ifdef SILC_UNIX
+  fsync(fd);
+#endif /* SILC_UNIX */
 
-  return 0;
+  return silc_file_close(fd);
 }
 
 /* Writes a buffer to the file.  If the file is created specific mode is
@@ -90,8 +96,13 @@ int silc_file_writefile_mode(const char *filename, const char *buffer,
                             SilcUInt32 len, int mode)
 {
   int fd;
+  int flags = O_CREAT | O_WRONLY | O_TRUNC;
 
-  if ((fd = creat(filename, mode)) == -1) {
+#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;
@@ -103,9 +114,11 @@ int silc_file_writefile_mode(const char *filename, const char *buffer,
     return -1;
   }
 
-  silc_file_close(fd);
+#ifdef SILC_UNIX
+  fsync(fd);
+#endif /* SILC_UNIX */
 
-  return 0;
+  return silc_file_close(fd);
 }
 
 /* Reads a file to a buffer. The allocated buffer is returned. Length of
@@ -167,11 +180,15 @@ SilcUInt64 silc_file_size(const char *filename)
   int ret;
   struct stat stats;
 
-#ifndef SILC_WIN32
+#ifdef SILC_WIN32
+  ret = stat(filename, &stats);
+#endif /* SILC_WIN32 */
+#ifdef SILC_UNIX
   ret = lstat(filename, &stats);
-#else
+#endif /* SILC_UNIX */
+#ifdef SILC_SYMBIAN
   ret = stat(filename, &stats);
-#endif
+#endif /* SILC_SYMBIAN */
   if (ret < 0)
     return 0;