5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * These are general utility functions that doesn't belong to any specific
27 * Revision 1.2 2000/07/05 06:06:12 priikone
28 * Added file saving with specific mode.
30 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
31 * Imported from internal CVS/Added Log headers.
36 #include "silcincludes.h"
38 /* Reads a file to a buffer. The allocated buffer is returned. Length of
39 the file read is returned to the return_len argument. */
41 char *silc_file_read(const char *filename, int *return_len)
47 fd = open(filename, O_RDONLY);
49 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
53 filelen = lseek(fd, (off_t)0L, SEEK_END);
56 if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
60 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
64 buffer = silc_calloc(filelen + 1, sizeof(char));
66 if ((read(fd, buffer, filelen)) == -1) {
67 memset(buffer, 0, sizeof(buffer));
69 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
75 buffer[filelen] = EOF;
77 *return_len = filelen;
81 /* Writes a buffer to the file. */
83 int silc_file_write(const char *filename, const char *buffer, int len)
87 if ((fd = creat(filename, 0644)) == -1) {
88 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno)));
92 if ((write(fd, buffer, len)) == -1) {
93 SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno)));
102 /* Writes a buffer to the file. If the file is created specific mode is
105 int silc_file_write_mode(const char *filename, const char *buffer,
110 if ((fd = creat(filename, mode)) == -1) {
111 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno)));
115 if ((write(fd, buffer, len)) == -1) {
116 SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno)));
125 /* Gets line from a buffer. Stops reading when a newline or EOF occurs.
126 This doesn't remove the newline sign from the destination buffer. The
127 argument begin is returned and should be passed again for the function. */
129 int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin)
131 static int start = 0;
134 memset(dest, 0, destlen);
140 for ( ; start <= srclen; i++, start++) {
144 dest[i] = src[start];
157 /* Checks line for illegal characters. Return -1 when illegal character
158 were found. This is used to check for bad lines when reading data from
159 for example a configuration file. */
161 int silc_check_line(char *buf)
163 /* Illegal characters in line */
164 if (strchr(buf, '#')) return -1;
165 if (strchr(buf, '\'')) return -1;
166 if (strchr(buf, '\\')) return -1;
167 if (strchr(buf, '\r')) return -1;
168 if (strchr(buf, '\a')) return -1;
169 if (strchr(buf, '\b')) return -1;
170 if (strchr(buf, '\f')) return -1;
179 /* Returns current time as string. */
181 char *silc_get_time()
186 curtime = time(NULL);
187 return_time = ctime(&curtime);
188 return_time[strlen(return_time) - 1] = '\0';
193 /* Converts string to capital characters */
195 char *silc_to_upper(char *string)
198 char *ret = silc_calloc(strlen(string) + 1, sizeof(char));
200 for (i = 0; i < strlen(string); i++)
201 ret[i] = toupper(string[i]);
206 /* Compares two strings. Strings may include wildcards * and ?.
207 Returns TRUE if strings match. */
209 int silc_string_compare(char *string1, char *string2)
212 int slen1 = strlen(string1);
213 int slen2 = strlen(string2);
214 char *tmpstr1, *tmpstr2;
216 if (!string1 || !string2)
219 /* See if they are same already */
220 if (!strncmp(string1, string2, strlen(string2)))
224 if (!strchr(string1, '*'))
227 /* Take copies of the original strings as we will change them */
228 tmpstr1 = silc_calloc(slen1 + 1, sizeof(char));
229 memcpy(tmpstr1, string1, slen1);
230 tmpstr2 = silc_calloc(slen2 + 1, sizeof(char));
231 memcpy(tmpstr2, string2, slen2);
233 for (i = 0; i < slen2; i++) {
235 /* * wildcard. Only one * wildcard is possible. */
236 if (tmpstr1[i] == '*')
237 if (!strncmp(tmpstr1, tmpstr2, i)) {
238 memset(tmpstr2, 0, slen2);
239 strncpy(tmpstr2, tmpstr1, i);
244 if (tmpstr1[i] == '?') {
245 if (!strncmp(tmpstr1, tmpstr2, i)) {
246 if (!(slen1 < i + 1))
247 if (tmpstr1[i + 1] != '?' &&
248 tmpstr1[i + 1] != tmpstr2[i + 1])
251 if (!(slen1 < slen2))
256 if (strncmp(tmpstr1, tmpstr2, i))
257 strncpy(tmpstr2, string2, slen2);
262 /* if using *, remove it */
263 if (strchr(tmpstr1, '*'))
264 *strchr(tmpstr1, '*') = 0;
266 if (!strcmp(tmpstr1, tmpstr2)) {
267 memset(tmpstr1, 0, slen1);
268 memset(tmpstr2, 0, slen2);
274 memset(tmpstr1, 0, slen1);
275 memset(tmpstr2, 0, slen2);