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))
76 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
81 if (silc_file_write(fd, buffer, len) == -1) {
82 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
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, 0644))
107 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
112 if ((silc_file_write(fd, buffer, len)) == -1) {
113 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
123 /* Reads a file to a buffer. The allocated buffer is returned. Length of
124 the file read is returned to the return_len argument. */
126 char *silc_file_readfile(const char *filename, SilcUInt32 *return_len)
132 fd = silc_file_open(filename, O_RDONLY);
136 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
140 filelen = lseek(fd, (off_t)0L, SEEK_END);
145 if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
151 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
156 buffer = silc_calloc(filelen + 1, sizeof(char));
158 if ((silc_file_read(fd, buffer, filelen)) == -1) {
159 memset(buffer, 0, sizeof(buffer));
161 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
167 buffer[filelen] = EOF;
170 *return_len = filelen;
175 /* Returns the size of `filename'. Returns 0 on error. */
177 SilcUInt64 silc_file_size(const char *filename)
183 ret = lstat(filename, &stats);
185 ret = stat(filename, &stats);
190 return (SilcUInt64)stats.st_size;