5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 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 return silc_file_close(fd);
90 /* Writes a buffer to the file. If the file is created specific mode is
93 int silc_file_writefile_mode(const char *filename, const char *buffer,
94 SilcUInt32 len, int mode)
97 int flags = O_CREAT | O_WRONLY | O_TRUNC;
101 #endif /* O_BINARY */
103 if ((fd = open(filename, flags, mode)) == -1) {
104 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
109 if ((silc_file_write(fd, buffer, len)) == -1) {
110 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
117 return silc_file_close(fd);
120 /* Reads a file to a buffer. The allocated buffer is returned. Length of
121 the file read is returned to the return_len argument. */
123 char *silc_file_readfile(const char *filename, SilcUInt32 *return_len)
129 fd = silc_file_open(filename, O_RDONLY);
133 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
137 filelen = lseek(fd, (off_t)0L, SEEK_END);
142 if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
148 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
153 buffer = silc_calloc(filelen + 1, sizeof(char));
155 if ((silc_file_read(fd, buffer, filelen)) == -1) {
156 memset(buffer, 0, sizeof(buffer));
158 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
164 buffer[filelen] = EOF;
167 *return_len = filelen;
172 /* Returns the size of `filename'. Returns 0 on error. */
174 SilcUInt64 silc_file_size(const char *filename)
180 ret = lstat(filename, &stats);
182 ret = stat(filename, &stats);
187 return (SilcUInt64)stats.st_size;