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