Removed status paylaod encoding functions -> not needed anymore.
[silc.git] / lib / silccore / silcutil.c
1 /*
2
3   silcutil.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 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; either version 2 of the License, or
12   (at your option) any later version.
13   
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.
18
19 */
20 /*
21  * These are general utility functions that doesn't belong to any specific
22  * group of routines.
23  */
24 /*
25  * $Id$
26  * $Log$
27  * Revision 1.2  2000/07/05 06:06:12  priikone
28  *      Added file saving with specific mode.
29  *
30  * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
31  *      Imported from internal CVS/Added Log headers.
32  *
33  *
34  */
35
36 #include "silcincludes.h"
37
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. */
40
41 char *silc_file_read(const char *filename, int *return_len)
42 {
43   int fd;
44   char *buffer;
45   int filelen;
46
47   fd = open(filename, O_RDONLY);
48   if (fd < 0) {
49     SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
50     return NULL;
51   }
52
53   filelen = lseek(fd, (off_t)0L, SEEK_END);
54   if (filelen < 0)
55     return NULL;
56   if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
57     return NULL;
58
59   if (filelen < 0) {
60     SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
61     return NULL;
62   }
63   
64   buffer = silc_calloc(filelen + 1, sizeof(char));
65   
66   if ((read(fd, buffer, filelen)) == -1) {
67     memset(buffer, 0, sizeof(buffer));
68     close(fd);
69     SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
70                     strerror(errno)));
71     return NULL;
72   }
73
74   close(fd);
75   buffer[filelen] = EOF;
76   
77   *return_len = filelen;
78   return buffer;
79 }
80
81 /* Writes a buffer to the file. */
82
83 int silc_file_write(const char *filename, const char *buffer, int len)
84 {
85   int fd;
86         
87   if ((fd = creat(filename, 0644)) == -1) {
88     SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno)));
89     return -1;
90   }
91   
92   if ((write(fd, buffer, len)) == -1) {
93     SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno)));
94     return -1;
95   }
96
97   close(fd);
98   
99   return 0;
100 }
101
102 /* Writes a buffer to the file.  If the file is created specific mode is
103    set to the file. */
104
105 int silc_file_write_mode(const char *filename, const char *buffer, 
106                          int len, int mode)
107 {
108   int fd;
109         
110   if ((fd = creat(filename, mode)) == -1) {
111     SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno)));
112     return -1;
113   }
114   
115   if ((write(fd, buffer, len)) == -1) {
116     SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno)));
117     return -1;
118   }
119
120   close(fd);
121   
122   return 0;
123 }
124
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. */
128
129 int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin)
130 {
131   static int start = 0;
132   int i;
133   
134   memset(dest, 0, destlen);
135   
136   if (begin != start)
137     start = 0;
138   
139   i = 0;
140   for ( ; start <= srclen; i++, start++) {
141     if (i > destlen)
142       return -1;
143     
144     dest[i] = src[start];
145     
146     if (dest[i] == EOF) 
147       return EOF;
148     
149     if (dest[i] == '\n') 
150       break;
151   }
152   start++;
153   
154   return start;
155 }
156
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. */
160
161 int silc_check_line(char *buf) 
162 {
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;
171   
172   /* Empty line */
173   if (buf[0] == '\n')
174     return -1;
175   
176   return 0;
177 }
178
179 /* Returns current time as string. */
180
181 char *silc_get_time()
182 {
183   time_t curtime;
184   char *return_time;
185
186   curtime = time(NULL);
187   return_time = ctime(&curtime);
188   return_time[strlen(return_time) - 1] = '\0';
189
190   return return_time;
191 }
192
193 /* Converts string to capital characters */
194
195 char *silc_to_upper(char *string)
196 {
197   int i;
198   char *ret = silc_calloc(strlen(string) + 1, sizeof(char));
199
200   for (i = 0; i < strlen(string); i++)
201     ret[i] = toupper(string[i]);
202
203   return ret;
204 }
205
206 /* Compares two strings. Strings may include wildcards * and ?.
207    Returns TRUE if strings match. */
208
209 int silc_string_compare(char *string1, char *string2)
210 {
211   int i;
212   int slen1 = strlen(string1);
213   int slen2 = strlen(string2);
214   char *tmpstr1, *tmpstr2;
215
216   if (!string1 || !string2)
217     return FALSE;
218
219   /* See if they are same already */
220   if (!strncmp(string1, string2, strlen(string2)))
221     return TRUE;
222
223   if (slen2 < slen1)
224     if (!strchr(string1, '*'))
225       return FALSE;
226   
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);
232   
233   for (i = 0; i < slen2; i++) {
234     
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);
240         break;
241       }
242     
243     /* ? wildcard */
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])
249             continue;
250         
251         if (!(slen1 < slen2))
252           tmpstr2[i] = '?';
253       }
254 #if 0
255     } else {
256       if (strncmp(tmpstr1, tmpstr2, i))
257         strncpy(tmpstr2, string2, slen2);
258 #endif
259     }
260   }
261   
262   /* if using *, remove it */
263   if (strchr(tmpstr1, '*'))
264     *strchr(tmpstr1, '*') = 0;
265   
266   if (!strcmp(tmpstr1, tmpstr2)) {
267     memset(tmpstr1, 0, slen1);
268     memset(tmpstr2, 0, slen2);
269     silc_free(tmpstr1);
270     silc_free(tmpstr2);
271     return TRUE;
272   }
273   
274   memset(tmpstr1, 0, slen1);
275   memset(tmpstr2, 0, slen2);
276   silc_free(tmpstr1);
277   silc_free(tmpstr2);
278   return FALSE;
279 }