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