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.1 2000/06/27 11:36:55 priikone
33 #include "silcincludes.h"
35 /* Reads a file to a buffer. The allocated buffer is returned. Length of
36 the file read is returned to the return_len argument. */
38 char *silc_file_read(const char *filename, int *return_len)
44 fd = open(filename, O_RDONLY);
46 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
50 filelen = lseek(fd, (off_t)0L, SEEK_END);
51 lseek(fd, (off_t)0L, SEEK_SET);
54 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
58 buffer = silc_calloc(filelen + 1, sizeof(char));
60 if ((read(fd, buffer, filelen)) == -1) {
61 memset(buffer, 0, sizeof(buffer));
63 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
69 buffer[filelen] = EOF;
71 *return_len = filelen;
75 /* Writes a buffer to the file. */
77 int silc_file_write(const char *filename, const char *buffer, int len)
81 if ((fd = creat(filename, 0644)) == -1) {
82 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno)));
86 if ((write(fd, buffer, len)) == -1) {
87 SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno)));
96 /* Gets line from a buffer. Stops reading when a newline or EOF occurs.
97 This doesn't remove the newline sign from the destination buffer. The
98 argument begin is returned and should be passed again for the function. */
100 int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin)
102 static int start = 0;
105 memset(dest, 0, destlen);
111 for ( ; start <= srclen; i++, start++) {
115 dest[i] = src[start];
128 /* Checks line for illegal characters. Return -1 when illegal character
129 were found. This is used to check for bad lines when reading data from
130 for example a configuration file. */
132 int silc_check_line(char *buf)
134 /* Illegal characters in line */
135 if (strchr(buf, '#')) return -1;
136 if (strchr(buf, '\'')) return -1;
137 if (strchr(buf, '\\')) return -1;
138 if (strchr(buf, '\r')) return -1;
139 if (strchr(buf, '\a')) return -1;
140 if (strchr(buf, '\b')) return -1;
141 if (strchr(buf, '\f')) return -1;
150 /* Returns current time as string. */
152 char *silc_get_time()
157 curtime = time(NULL);
158 return_time = ctime(&curtime);
159 return_time[strlen(return_time) - 1] = '\0';
164 /* Converts string to capital characters */
166 char *silc_to_upper(char *string)
169 char *ret = silc_calloc(strlen(string) + 1, sizeof(char));
171 for (i = 0; i < strlen(string); i++)
172 ret[i] = toupper(string[i]);
177 /* Compares two strings. Strings may include wildcards * and ?.
178 Returns TRUE if strings match. */
180 int silc_string_compare(char *string1, char *string2)
183 int slen1 = strlen(string1);
184 int slen2 = strlen(string2);
185 char *tmpstr1, *tmpstr2;
187 if (!string1 || !string2)
190 /* See if they are same already */
191 if (!strncmp(string1, string2, strlen(string2)))
195 if (!strchr(string1, '*'))
198 /* Take copies of the original strings as we will change them */
199 tmpstr1 = silc_calloc(slen1 + 1, sizeof(char));
200 memcpy(tmpstr1, string1, slen1);
201 tmpstr2 = silc_calloc(slen2 + 1, sizeof(char));
202 memcpy(tmpstr2, string2, slen2);
204 for (i = 0; i < slen2; i++) {
206 /* * wildcard. Only one * wildcard is possible. */
207 if (tmpstr1[i] == '*')
208 if (!strncmp(tmpstr1, tmpstr2, i)) {
209 memset(tmpstr2, 0, slen2);
210 strncpy(tmpstr2, tmpstr1, i);
215 if (tmpstr1[i] == '?') {
216 if (!strncmp(tmpstr1, tmpstr2, i)) {
217 if (!(slen1 < i + 1))
218 if (tmpstr1[i + 1] != '?' &&
219 tmpstr1[i + 1] != tmpstr2[i + 1])
222 if (!(slen1 < slen2))
227 if (strncmp(tmpstr1, tmpstr2, i))
228 strncpy(tmpstr2, string2, slen2);
233 /* if using *, remove it */
234 if (strchr(tmpstr1, '*'))
235 *strchr(tmpstr1, '*') = 0;
237 if (!strcmp(tmpstr1, tmpstr2)) {
238 memset(tmpstr1, 0, slen1);
239 memset(tmpstr2, 0, slen2);
245 memset(tmpstr1, 0, slen1);
246 memset(tmpstr2, 0, slen2);