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,
69 if ((fd = creat(filename, 0644)) == -1) {
70 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
75 if (silc_file_write(fd, buffer, len) == -1) {
76 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
86 /* Writes a buffer to the file. If the file is created specific mode is
89 int silc_file_writefile_mode(const char *filename, const char *buffer,
90 SilcUInt32 len, int mode)
94 if ((fd = creat(filename, mode)) == -1) {
95 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
100 if ((silc_file_write(fd, buffer, len)) == -1) {
101 SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
111 /* Reads a file to a buffer. The allocated buffer is returned. Length of
112 the file read is returned to the return_len argument. */
114 char *silc_file_readfile(const char *filename, SilcUInt32 *return_len)
120 fd = silc_file_open(filename, O_RDONLY);
124 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
128 filelen = lseek(fd, (off_t)0L, SEEK_END);
133 if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
139 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
144 buffer = silc_calloc(filelen + 1, sizeof(char));
146 if ((silc_file_read(fd, buffer, filelen)) == -1) {
147 memset(buffer, 0, sizeof(buffer));
149 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
155 buffer[filelen] = EOF;
158 *return_len = filelen;
163 /* Returns the size of `filename'. Returns 0 on error. */
165 SilcUInt64 silc_file_size(const char *filename)
171 ret = lstat(filename, &stats);
173 ret = stat(filename, &stats);
178 return (SilcUInt64)stats.st_size;