5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 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.
23 /* Opens a file indicated by the filename `filename' with flags indicated
26 int silc_file_open(const char *filename, int flags)
28 return silc_file_open_mode(filename, flags, 0600);
31 /* Opens a file indicated by the filename `filename' with flags indicated
32 by the `flags', and with the specified `mode'. */
34 int silc_file_open_mode(const char *filename, int flags, int mode)
36 int fd = open(filename, flags, mode);
38 silc_set_errno_posix(errno);
42 /* Reads data from file descriptor `fd' to `buf'. */
44 int silc_file_read(int fd, unsigned char *buf, SilcUInt32 buf_len)
46 int ret = read(fd, (void *)buf, buf_len);
48 silc_set_errno_posix(errno);
52 /* Writes `buffer' of length of `len' to file descriptor `fd'. */
54 int silc_file_write(int fd, const char *buffer, SilcUInt32 len)
56 int ret = write(fd, (const void *)buffer, len);
58 silc_set_errno_posix(errno);
62 /* Closes file descriptor */
64 int silc_file_close(int fd)
68 silc_set_errno_posix(errno);
72 /* Writes a buffer to the file. */
74 int silc_file_writefile(const char *filename, const char *buffer,
78 int flags = O_CREAT | O_WRONLY | O_TRUNC;
84 if ((fd = open(filename, flags, 0644)) == -1) {
85 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
86 silc_errno_string(silc_errno)));
90 if (silc_file_write(fd, buffer, len) == -1) {
91 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename,
92 silc_errno_string(silc_errno)));
99 #endif /* SILC_UNIX */
101 return silc_file_close(fd);
104 /* Writes a buffer to the file. If the file is created specific mode is
107 int silc_file_writefile_mode(const char *filename, const char *buffer,
108 SilcUInt32 len, int mode)
111 int flags = O_CREAT | O_WRONLY | O_TRUNC;
113 #if defined(O_BINARY)
115 #endif /* O_BINARY */
117 if ((fd = open(filename, flags, mode)) == -1) {
118 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
119 silc_errno_string(silc_errno)));
123 if ((silc_file_write(fd, buffer, len)) == -1) {
124 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename,
125 silc_errno_string(silc_errno)));
132 #endif /* SILC_UNIX */
134 return silc_file_close(fd);
137 /* Reads a file to a buffer. The allocated buffer is returned. Length of
138 the file read is returned to the return_len argument. */
140 char *silc_file_readfile(const char *filename, SilcUInt32 *return_len,
144 unsigned char *buffer;
147 fd = silc_file_open(filename, O_RDONLY);
149 if (silc_errno == SILC_ERR_NO_SUCH_FILE)
151 SILC_LOG_ERROR(("Cannot open file %s: %s", filename,
152 silc_errno_string(silc_errno)));
156 filelen = lseek(fd, (off_t)0L, SEEK_END);
158 silc_set_errno_posix(errno);
162 if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
163 silc_set_errno_posix(errno);
168 buffer = silc_calloc(filelen + 1, sizeof(*buffer));
170 if ((silc_file_read(fd, buffer, filelen)) == -1) {
171 memset(buffer, 0, sizeof(buffer));
173 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
174 silc_errno_string(silc_errno)));
179 buffer[filelen] = EOF;
182 *return_len = filelen;
184 return (char *)buffer;
187 /* Returns the size of `filename'. Returns 0 on error. */
189 SilcUInt64 silc_file_size(const char *filename)
195 ret = stat(filename, &stats);
196 #endif /* SILC_WIN32 */
198 ret = lstat(filename, &stats);
199 #endif /* SILC_UNIX */
201 ret = stat(filename, &stats);
202 #endif /* SILC_SYMBIAN */
204 silc_set_errno_posix(errno);
208 return (SilcUInt64)stats.st_size;