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