5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
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.
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.
21 * These are general utility functions that doesn't belong to any specific
26 #include "silcincludes.h"
28 /* XXX lib/contrib/regex.c might cmopile on WIN32 as well */
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. */
34 char *silc_string_regexify(const char *string)
41 for (i = 0; i < len; i++)
42 if (string[i] == '*' || string[i] == '?')
45 regex = silc_calloc(len + count, sizeof(*regex));
51 for (i = 0; i < len; i++) {
52 if (string[i] == '*' || string[i] == '?') {
55 } else if (string[i] == ',') {
63 regex[count] = string[i];
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
77 char *silc_string_regex_combine(const char *string1, const char *string2)
82 len1 = strlen(string1);
83 len2 = strlen(string2);
85 tmp = silc_calloc(2 + len1 + len2, sizeof(*tmp));
86 strncat(tmp, string1, len1 - 2);
88 strncat(tmp, string2 + 1, len2 - 1);
93 /* Matches the two strings and returns TRUE if the strings match. */
95 int silc_string_regex_match(const char *regex, const char *string)
100 if (regcomp(&preg, regex, REG_NOSUB | REG_EXTENDED) < 0)
103 if (regexec(&preg, string, 0, NULL, 0) == 0)
111 /* Do regex match to the two strings `string1' and `string2'. If the
112 `string2' matches the `string1' this returns TRUE. */
114 int silc_string_match(const char *string1, const char *string2)
119 if (!string1 || !string2)
122 s1 = silc_string_regexify(string1);
123 ret = silc_string_regex_match(s1, string2);
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. */
132 char *silc_get_username()
134 char *logname = NULL;
136 logname = getenv("LOGNAME");
138 logname = getlogin();
142 pw = getpwuid(getuid());
144 return strdup("foo");
146 logname = pw->pw_name;
150 return strdup(logname);
153 /* Returns the real name of ther user. */
155 char *silc_get_real_name()
157 char *realname = NULL;
160 pw = getpwuid(getuid());
162 return strdup("Foo T. Bar");
164 if (strchr(pw->pw_gecos, ','))
165 *strchr(pw->pw_gecos, ',') = 0;
167 realname = strdup(pw->pw_gecos);
172 /* Return current time to struct timeval. */
174 int silc_gettimeofday(struct timeval *p)
176 return gettimeofday(p, NULL);