5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "silcincludes.h"
23 /* Opens a file indicated by the filename `filename' with flags indicated
26 int silc_file_open(const char *filename, int flags)
28 int fd = open(filename, flags, 0600);
32 /* Opens a file indicated by the filename `filename' with flags indicated
33 by the `flags', and with the specified `mode'. */
35 int silc_file_open_mode(const char *filename, int flags, int mode)
37 int fd = open(filename, flags, mode);
41 /* Reads data from file descriptor `fd' to `buf'. */
43 int silc_file_read(int fd, unsigned char *buf, SilcUInt32 buf_len)
45 return read(fd, (void *)buf, buf_len);
48 /* Writes `buffer' of length of `len' to file descriptor `fd'. */
50 int silc_file_write(int fd, const char *buffer, SilcUInt32 len)
52 return write(fd, (const void *)buffer, len);
55 /* Closes file descriptor */
57 int silc_file_close(int fd)
62 /* Writes a buffer to the file. */
64 int silc_file_writefile(const char *filename, const char *buffer,
68 int flags = O_CREAT | O_WRONLY | O_TRUNC;
74 if ((fd = open(filename, flags, 0644)) == -1) {
75 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
80 if (silc_file_write(fd, buffer, len) == -1) {
81 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
91 /* Writes a buffer to the file. If the file is created specific mode is
94 int silc_file_writefile_mode(const char *filename, const char *buffer,
95 SilcUInt32 len, int mode)
98 int flags = O_CREAT | O_WRONLY | O_TRUNC;
100 #if defined(O_BINARY)
102 #endif /* O_BINARY */
104 if ((fd = open(filename, flags, 0644)) == -1) {
105 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
110 if ((silc_file_write(fd, buffer, len)) == -1) {
111 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
121 /* Reads a file to a buffer. The allocated buffer is returned. Length of
122 the file read is returned to the return_len argument. */
124 char *silc_file_readfile(const char *filename, SilcUInt32 *return_len)
130 fd = silc_file_open(filename, O_RDONLY);
134 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
138 filelen = lseek(fd, (off_t)0L, SEEK_END);
143 if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
149 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
154 buffer = silc_calloc(filelen + 1, sizeof(char));
156 if ((silc_file_read(fd, buffer, filelen)) == -1) {
157 memset(buffer, 0, sizeof(buffer));
159 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
165 buffer[filelen] = EOF;
168 *return_len = filelen;
173 /* Returns the size of `filename'. Returns 0 on error. */
175 SilcUInt64 silc_file_size(const char *filename)
181 ret = lstat(filename, &stats);
183 ret = stat(filename, &stats);
188 return (SilcUInt64)stats.st_size;