X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcfileutil.c;h=23d2c17c12901a0e1c9b56886bc3bdc172ba88d4;hb=c257b555225193e54d85daf541d29578b3c93882;hp=0bb65962fad2774478c7d710d86b55c01ae31027;hpb=a818c5b5411bbc4436d1c5f011236985c96bb787;p=silc.git diff --git a/lib/silcutil/silcfileutil.c b/lib/silcutil/silcfileutil.c index 0bb65962..23d2c17c 100644 --- a/lib/silcutil/silcfileutil.c +++ b/lib/silcutil/silcfileutil.c @@ -1,10 +1,10 @@ /* - silcfileutil.c + silcfileutil.c Author: Pekka Riikonen - Copyright (C) 1997 - 2002 Pekka Riikonen + Copyright (C) 1997 - 2003 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 @@ -45,7 +45,7 @@ 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, SilcUInt32 len) { @@ -61,41 +61,52 @@ int silc_file_close(int fd) /* Writes a buffer to the file. */ -int silc_file_writefile(const char *filename, const char *buffer, SilcUInt32 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, +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,7 +114,7 @@ int silc_file_writefile_mode(const char *filename, const char *buffer, } silc_file_close(fd); - + return 0; } @@ -139,9 +150,9 @@ char *silc_file_readfile(const char *filename, SilcUInt32 *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,7 +170,7 @@ char *silc_file_readfile(const char *filename, SilcUInt32 *return_len) return buffer; } -/* Returns files size. Returns 0 on error. */ +/* Returns the size of `filename'. Returns 0 on error. */ SilcUInt64 silc_file_size(const char *filename) {