Integer type name change.
[silc.git] / lib / silcutil / silcfileutil.c
1 /*
2
3   silcfileutil.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2002 Pekka Riikonen
8
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.
12
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.
17
18 */
19 /* $Id$ */
20
21 #include "silcincludes.h"
22
23 /* Opens a file indicated by the filename `filename' with flags indicated
24    by the `flags'. */
25
26 int silc_file_open(const char *filename, int flags)
27 {
28   int fd = open(filename, flags, 0600);
29   return fd;
30 }
31
32 /* Opens a file indicated by the filename `filename' with flags indicated
33    by the `flags', and with the specified `mode'. */
34
35 int silc_file_open_mode(const char *filename, int flags, int mode)
36 {
37   int fd = open(filename, flags, mode);
38   return fd;
39 }
40
41 /* Reads data from file descriptor `fd' to `buf'. */
42
43 int silc_file_read(int fd, unsigned char *buf, SilcUInt32 buf_len)
44 {
45   return read(fd, (void *)buf, buf_len);
46 }
47
48 /* Writes `buffer' of length of `len' to file descriptor `fd. */
49
50 int silc_file_write(int fd, const char *buffer, SilcUInt32 len)
51 {
52   return write(fd, (const void *)buffer, len);
53 }
54
55 /* Closes file descriptor */
56
57 int silc_file_close(int fd)
58 {
59   return close(fd);
60 }
61
62 /* Writes a buffer to the file. */
63
64 int silc_file_writefile(const char *filename, const char *buffer, SilcUInt32 len)
65 {
66   int fd;
67         
68   if ((fd = creat(filename, 0644)) == -1) {
69     SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
70                     strerror(errno)));
71     return -1;
72   }
73   
74   if ((silc_file_write(fd, buffer, len)) == -1) {
75     SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
76     silc_file_close(fd);
77     return -1;
78   }
79
80   silc_file_close(fd);
81   
82   return 0;
83 }
84
85 /* Writes a buffer to the file.  If the file is created specific mode is
86    set to the file. */
87
88 int silc_file_writefile_mode(const char *filename, const char *buffer, 
89                              SilcUInt32 len, int mode)
90 {
91   int fd;
92         
93   if ((fd = creat(filename, mode)) == -1) {
94     SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
95                     strerror(errno)));
96     return -1;
97   }
98   
99   if ((silc_file_write(fd, buffer, len)) == -1) {
100     SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
101     silc_file_close(fd);
102     return -1;
103   }
104
105   silc_file_close(fd);
106   
107   return 0;
108 }
109
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. */
112
113 char *silc_file_readfile(const char *filename, SilcUInt32 *return_len)
114 {
115   int fd;
116   char *buffer;
117   int filelen;
118
119   fd = silc_file_open(filename, O_RDONLY);
120   if (fd < 0) {
121     if (errno == ENOENT)
122       return NULL;
123     SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
124     return NULL;
125   }
126
127   filelen = lseek(fd, (off_t)0L, SEEK_END);
128   if (filelen < 0) {
129     silc_file_close(fd);
130     return NULL;
131   }
132   if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
133     silc_file_close(fd);
134     return NULL;
135   }
136
137   if (filelen < 0) {
138     SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
139     silc_file_close(fd);
140     return NULL;
141   }
142   
143   buffer = silc_calloc(filelen + 1, sizeof(char));
144   
145   if ((silc_file_read(fd, buffer, filelen)) == -1) {
146     memset(buffer, 0, sizeof(buffer));
147     silc_file_close(fd);
148     SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
149                     strerror(errno)));
150     return NULL;
151   }
152
153   silc_file_close(fd);
154   buffer[filelen] = EOF;
155
156   if (return_len)
157     *return_len = filelen;
158
159   return buffer;
160 }
161
162 /* Returns files size. Returns 0 on error. */
163
164 SilcUInt64 silc_file_size(const char *filename)
165 {
166   int ret;
167   struct stat stats;
168
169 #ifndef SILC_WIN32
170   ret = lstat(filename, &stats);
171 #else
172   ret = stat(filename, &stats);
173 #endif
174   if (ret < 0)
175     return 0;
176
177   return (SilcUInt64)stats.st_size;
178 }