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, SilcUInt32 len)
68 if ((fd = creat(filename, 0644)) == -1) {
69 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
74 if ((silc_file_write(fd, buffer, len)) == -1) {
75 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
85 /* Writes a buffer to the file. If the file is created specific mode is
88 int silc_file_writefile_mode(const char *filename, const char *buffer,
89 SilcUInt32 len, int mode)
93 if ((fd = creat(filename, mode)) == -1) {
94 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
99 if ((silc_file_write(fd, buffer, len)) == -1) {
100 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
110 /* Reads a file to a buffer. The allocated buffer is returned. Length of
111 the file read is returned to the return_len argument. */
113 char *silc_file_readfile(const char *filename, SilcUInt32 *return_len)
119 fd = silc_file_open(filename, O_RDONLY);
123 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
127 filelen = lseek(fd, (off_t)0L, SEEK_END);
132 if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
138 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
143 buffer = silc_calloc(filelen + 1, sizeof(char));
145 if ((silc_file_read(fd, buffer, filelen)) == -1) {
146 memset(buffer, 0, sizeof(buffer));
148 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
154 buffer[filelen] = EOF;
157 *return_len = filelen;
162 /* Returns files size. Returns 0 on error. */
164 SilcUInt64 silc_file_size(const char *filename)
170 ret = lstat(filename, &stats);
172 ret = stat(filename, &stats);
177 return (SilcUInt64)stats.st_size;