5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2008 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.
20 #include "silcruntime.h"
22 /* Opens a file indicated by the filename `filename' with flags indicated
25 int silc_file_open(const char *filename, int flags)
27 return silc_file_open_mode(filename, flags, 0600);
30 /* Opens a file indicated by the filename `filename' with flags indicated
31 by the `flags', and with the specified `mode'. */
33 int silc_file_open_mode(const char *filename, int flags, int mode)
35 int fd = open(filename, flags, mode);
37 silc_set_errno_posix(errno);
41 /* Reads data from file descriptor `fd' to `buf'. */
43 int silc_file_read(int fd, unsigned char *buf, SilcUInt32 buf_len)
45 int ret = read(fd, (void *)buf, buf_len);
47 silc_set_errno_posix(errno);
51 /* Writes `buffer' of length of `len' to file descriptor `fd'. */
53 int silc_file_write(int fd, const char *buffer, SilcUInt32 len)
55 int ret = write(fd, (const void *)buffer, len);
57 silc_set_errno_posix(errno);
61 /* Closes file descriptor */
63 int silc_file_close(int fd)
67 silc_set_errno_posix(errno);
71 /* Writes a buffer to the file. */
73 int silc_file_writefile(const char *filename, const char *buffer,
77 int flags = O_CREAT | O_WRONLY | O_TRUNC;
83 if ((fd = open(filename, flags, 0644)) == -1) {
84 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
85 silc_errno_string(silc_errno)));
89 if (silc_file_write(fd, buffer, len) == -1) {
90 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename,
91 silc_errno_string(silc_errno)));
98 #endif /* SILC_UNIX */
100 return silc_file_close(fd);
103 /* Writes a buffer to the file. If the file is created specific mode is
106 int silc_file_writefile_mode(const char *filename, const char *buffer,
107 SilcUInt32 len, int mode)
110 int flags = O_CREAT | O_WRONLY | O_TRUNC;
112 #if defined(O_BINARY)
114 #endif /* O_BINARY */
116 if ((fd = open(filename, flags, mode)) == -1) {
117 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
118 silc_errno_string(silc_errno)));
122 if ((silc_file_write(fd, buffer, len)) == -1) {
123 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename,
124 silc_errno_string(silc_errno)));
131 #endif /* SILC_UNIX */
133 return silc_file_close(fd);
136 /* Reads a file to a buffer. The allocated buffer is returned. Length of
137 the file read is returned to the return_len argument. */
139 char *silc_file_readfile(const char *filename, SilcUInt32 *return_len,
143 unsigned char *buffer;
146 fd = silc_file_open(filename, O_RDONLY);
148 if (silc_errno == SILC_ERR_NO_SUCH_FILE)
150 SILC_LOG_ERROR(("Cannot open file %s: %s", filename,
151 silc_errno_string(silc_errno)));
155 filelen = lseek(fd, (off_t)0L, SEEK_END);
157 silc_set_errno_posix(errno);
161 if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
162 silc_set_errno_posix(errno);
167 buffer = silc_calloc(filelen + 1, sizeof(*buffer));
169 if ((silc_file_read(fd, buffer, filelen)) == -1) {
170 memset(buffer, 0, sizeof(buffer));
172 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
173 silc_errno_string(silc_errno)));
178 buffer[filelen] = EOF;
181 *return_len = filelen;
183 return (char *)buffer;
186 /* Returns the size of `filename'. Returns 0 on error. */
188 SilcUInt64 silc_file_size(const char *filename)
194 ret = stat(filename, &stats);
195 #endif /* SILC_WIN32 */
197 ret = lstat(filename, &stats);
198 #endif /* SILC_UNIX */
200 ret = stat(filename, &stats);
201 #endif /* SILC_SYMBIAN */
203 silc_set_errno_posix(errno);
207 return (SilcUInt64)stats.st_size;