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);
40 /* Reads data from file descriptor `fd' to `buf'. */
42 int silc_file_read(int fd, unsigned char *buf, SilcUInt32 buf_len)
44 return read(fd, (void *)buf, buf_len);
47 /* Writes `buffer' of length of `len' to file descriptor `fd'. */
49 int silc_file_write(int fd, const char *buffer, SilcUInt32 len)
51 return write(fd, (const void *)buffer, len);
54 /* Closes file descriptor */
56 int silc_file_close(int fd)
61 /* Writes a buffer to the file. */
63 int silc_file_writefile(const char *filename, const char *buffer,
67 int flags = O_CREAT | O_WRONLY | O_TRUNC;
73 if ((fd = open(filename, flags, 0644)) == -1) {
74 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
79 if (silc_file_write(fd, buffer, len) == -1) {
80 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
87 #endif /* SILC_UNIX */
89 return silc_file_close(fd);
92 /* Writes a buffer to the file. If the file is created specific mode is
95 int silc_file_writefile_mode(const char *filename, const char *buffer,
96 SilcUInt32 len, int mode)
99 int flags = O_CREAT | O_WRONLY | O_TRUNC;
101 #if defined(O_BINARY)
103 #endif /* O_BINARY */
105 if ((fd = open(filename, flags, mode)) == -1) {
106 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
111 if ((silc_file_write(fd, buffer, len)) == -1) {
112 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
119 #endif /* SILC_UNIX */
121 return silc_file_close(fd);
124 /* Reads a file to a buffer. The allocated buffer is returned. Length of
125 the file read is returned to the return_len argument. */
127 char *silc_file_readfile(const char *filename, SilcUInt32 *return_len,
131 unsigned char *buffer;
134 fd = silc_file_open(filename, O_RDONLY);
138 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
142 filelen = lseek(fd, (off_t)0L, SEEK_END);
147 if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
153 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
158 buffer = silc_calloc(filelen + 1, sizeof(*buffer));
160 if ((silc_file_read(fd, buffer, filelen)) == -1) {
161 memset(buffer, 0, sizeof(buffer));
163 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
169 buffer[filelen] = EOF;
172 *return_len = filelen;
174 return (char *)buffer;
177 /* Returns the size of `filename'. Returns 0 on error. */
179 SilcUInt64 silc_file_size(const char *filename)
185 ret = stat(filename, &stats);
186 #endif /* SILC_WIN32 */
188 ret = lstat(filename, &stats);
189 #endif /* SILC_UNIX */
191 ret = stat(filename, &stats);
192 #endif /* SILC_SYMBIAN */
196 return (SilcUInt64)stats.st_size;