Added SILC Server library.
[silc.git] / lib / silcutil / unix / silcunixutil.c
1 /*
2
3   silcunixutil.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2005 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 /*
20  * These are general utility functions that doesn't belong to any specific
21  * group of routines.
22  */
23 /* $Id$ */
24
25 #include "silc.h"
26
27 /* XXX lib/contrib/regex.c might cmopile on WIN32 as well */
28
29 /* Inspects the `string' for wildcards and returns regex string that can
30    be used by the GNU regex library. A comma (`,') in the `string' means
31    that the string is list. */
32
33 char *silc_string_regexify(const char *string)
34 {
35   int i, len, count;
36   char *regex;
37
38   len = strlen(string);
39   count = 4;
40   for (i = 0; i < len; i++) {
41     if (string[i] == '*' || string[i] == '?')
42       count++;                  /* Will add '.' */
43     if (string[i] == ',')
44       count += 2;               /* Will add '|' and '^' */
45   }
46
47   regex = silc_calloc(len + count + 1, sizeof(*regex));
48
49   count = 0;
50   regex[count++] = '(';
51   regex[count++] = '^';
52
53   for (i = 0; i < len; i++) {
54     if (string[i] == '*' || string[i] == '?') {
55       regex[count] = '.';
56       count++;
57     } else if (string[i] == ',') {
58       if (i + 2 == len)
59         continue;
60       regex[count++] = '|';
61       regex[count++] = '^';
62       continue;
63     }
64
65     regex[count++] = string[i];
66   }
67
68   regex[count++] = ')';
69   regex[count] = '$';
70
71   return regex;
72 }
73
74 /* Combines two regex strings into one regex string so that they can be
75    used as one by the GNU regex library. The `string2' is combine into
76    the `string1'. */
77
78 char *silc_string_regex_combine(const char *string1, const char *string2)
79 {
80   char *tmp;
81   int len1, len2;
82
83   len1 = strlen(string1);
84   len2 = strlen(string2);
85
86   tmp = silc_calloc(2 + len1 + len2, sizeof(*tmp));
87   strncat(tmp, string1, len1 - 2);
88   strncat(tmp, "|", 1);
89   strncat(tmp, string2 + 1, len2 - 1);
90
91   return tmp;
92 }
93
94 /* Matches the two strings and returns TRUE if the strings match. */
95
96 int silc_string_regex_match(const char *regex, const char *string)
97 {
98   regex_t preg;
99   int ret = FALSE;
100   
101   if (regcomp(&preg, regex, REG_NOSUB | REG_EXTENDED) != 0)
102     return FALSE;
103
104   if (regexec(&preg, string, 0, NULL, 0) == 0)
105     ret = TRUE;
106
107   regfree(&preg);
108
109   return ret;
110 }
111
112 /* Do regex match to the two strings `string1' and `string2'. If the
113    `string2' matches the `string1' this returns TRUE. */
114
115 int silc_string_match(const char *string1, const char *string2)
116 {
117   char *s1;
118   int ret = FALSE;
119
120   if (!string1 || !string2)
121     return ret;
122
123   s1 = silc_string_regexify(string1);
124   ret = silc_string_regex_match(s1, string2);
125   silc_free(s1);
126
127   return ret;
128 }
129
130 /* Returns the username of the user. If the global variable LOGNAME
131    does not exists we will get the name from the password file. */
132
133 char *silc_get_username()
134 {
135   char *logname = NULL;
136   
137   logname = getenv("LOGNAME");
138   if (!logname) {
139     logname = getlogin();
140     if (!logname) {
141       struct passwd *pw;
142
143       pw = getpwuid(getuid());
144       if (!pw)
145         return strdup("foo");
146
147       logname = pw->pw_name;
148     }
149   }
150   
151   return strdup(logname);
152 }
153
154 /* Returns the real name of ther user. */
155
156 char *silc_get_real_name()
157 {
158   char *realname = NULL;
159   struct passwd *pw;
160     
161   pw = getpwuid(getuid());
162   if (!pw)
163      return strdup("Foo T. Bar");
164
165   if (strchr(pw->pw_gecos, ','))
166     *strchr(pw->pw_gecos, ',') = 0;
167
168   realname = strdup(pw->pw_gecos);
169
170   return realname;
171 }
172
173 /* Return current time to struct timeval. */
174
175 int silc_gettimeofday(struct timeval *p)
176 {
177   return gettimeofday(p, NULL);
178 }
179
180 int silc_file_set_nonblock(int fd)
181 {
182   return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
183 }
184