| 1 |
adx |
30 |
/* |
| 2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
adx |
30 |
* |
| 4 |
michael |
5347 |
* Copyright (c) 1997-2015 ircd-hybrid development team |
| 5 |
adx |
30 |
* |
| 6 |
|
|
* This program is free software; you can redistribute it and/or modify |
| 7 |
|
|
* it under the terms of the GNU General Public License as published by |
| 8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
|
|
* (at your option) any later version. |
| 10 |
|
|
* |
| 11 |
|
|
* This program is distributed in the hope that it will be useful, |
| 12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
|
|
* GNU General Public License for more details. |
| 15 |
|
|
* |
| 16 |
|
|
* You should have received a copy of the GNU General Public License |
| 17 |
|
|
* along with this program; if not, write to the Free Software |
| 18 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
adx |
30 |
* USA |
| 20 |
|
|
*/ |
| 21 |
|
|
|
| 22 |
michael |
2916 |
/*! \file s_misc.c |
| 23 |
|
|
* \brief Yet another miscellaneous functions file. |
| 24 |
|
|
* \version $Id$ |
| 25 |
|
|
*/ |
| 26 |
|
|
|
| 27 |
adx |
30 |
#include "stdinc.h" |
| 28 |
michael |
3347 |
#include "misc.h" |
| 29 |
adx |
30 |
#include "irc_string.h" |
| 30 |
|
|
#include "ircd.h" |
| 31 |
|
|
|
| 32 |
|
|
|
| 33 |
michael |
2301 |
static const char *const months[] = |
| 34 |
adx |
30 |
{ |
| 35 |
|
|
"January", "February", "March", "April", |
| 36 |
|
|
"May", "June", "July", "August", |
| 37 |
|
|
"September", "October", "November","December" |
| 38 |
|
|
}; |
| 39 |
|
|
|
| 40 |
michael |
2301 |
static const char *const weekdays[] = |
| 41 |
adx |
30 |
{ |
| 42 |
|
|
"Sunday", "Monday", "Tuesday", "Wednesday", |
| 43 |
|
|
"Thursday", "Friday", "Saturday" |
| 44 |
|
|
}; |
| 45 |
|
|
|
| 46 |
michael |
1847 |
const char * |
| 47 |
michael |
2916 |
date(time_t lclock) |
| 48 |
adx |
30 |
{ |
| 49 |
|
|
static char buf[80], plus; |
| 50 |
|
|
struct tm *lt, *gm; |
| 51 |
|
|
struct tm gmbuf; |
| 52 |
|
|
int minswest; |
| 53 |
michael |
6413 |
static time_t lclock_last; |
| 54 |
adx |
30 |
|
| 55 |
michael |
2916 |
if (!lclock) |
| 56 |
adx |
30 |
lclock = CurrentTime; |
| 57 |
michael |
3244 |
|
| 58 |
michael |
6413 |
if (lclock_last == lclock) |
| 59 |
|
|
return buf; |
| 60 |
|
|
|
| 61 |
|
|
lclock_last = lclock; |
| 62 |
adx |
30 |
gm = gmtime(&lclock); |
| 63 |
|
|
memcpy(&gmbuf, gm, sizeof(gmbuf)); |
| 64 |
|
|
gm = &gmbuf; |
| 65 |
|
|
lt = localtime(&lclock); |
| 66 |
|
|
|
| 67 |
|
|
/* |
| 68 |
|
|
* There is unfortunately no clean portable way to extract time zone |
| 69 |
|
|
* offset information, so do ugly things. |
| 70 |
|
|
*/ |
| 71 |
|
|
minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min); |
| 72 |
|
|
|
| 73 |
|
|
if (lt->tm_yday != gm->tm_yday) |
| 74 |
|
|
{ |
| 75 |
|
|
if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) || |
| 76 |
|
|
(lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year)) |
| 77 |
|
|
minswest -= 24 * 60; |
| 78 |
|
|
else |
| 79 |
|
|
minswest += 24 * 60; |
| 80 |
|
|
} |
| 81 |
|
|
|
| 82 |
|
|
plus = (minswest > 0) ? '-' : '+'; |
| 83 |
|
|
if (minswest < 0) |
| 84 |
|
|
minswest = -minswest; |
| 85 |
|
|
|
| 86 |
michael |
1233 |
snprintf(buf, sizeof(buf), "%s %s %d %d -- %02u:%02u:%02u %c%02u:%02u", |
| 87 |
|
|
weekdays[lt->tm_wday], months[lt->tm_mon],lt->tm_mday, |
| 88 |
|
|
lt->tm_year + 1900, lt->tm_hour, lt->tm_min, lt->tm_sec, |
| 89 |
|
|
plus, minswest/60, minswest%60); |
| 90 |
adx |
30 |
return buf; |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
const char * |
| 94 |
michael |
6415 |
date_iso8601(time_t lclock) |
| 95 |
|
|
{ |
| 96 |
|
|
static char buf[MAX_DATE_STRING]; |
| 97 |
|
|
static time_t lclock_last; |
| 98 |
|
|
|
| 99 |
|
|
if (!lclock) |
| 100 |
|
|
lclock = CurrentTime; |
| 101 |
|
|
|
| 102 |
|
|
if (lclock_last != lclock) |
| 103 |
|
|
{ |
| 104 |
|
|
lclock_last = lclock; |
| 105 |
|
|
strftime(buf, sizeof(buf), "%FT%H:%M:%S%z", localtime(&lclock)); |
| 106 |
|
|
} |
| 107 |
|
|
|
| 108 |
|
|
return buf; |
| 109 |
|
|
} |
| 110 |
|
|
|
| 111 |
|
|
const char * |
| 112 |
adx |
30 |
smalldate(time_t lclock) |
| 113 |
|
|
{ |
| 114 |
|
|
static char buf[MAX_DATE_STRING]; |
| 115 |
|
|
struct tm *lt, *gm; |
| 116 |
|
|
struct tm gmbuf; |
| 117 |
michael |
6413 |
static time_t lclock_last; |
| 118 |
adx |
30 |
|
| 119 |
|
|
if (!lclock) |
| 120 |
|
|
lclock = CurrentTime; |
| 121 |
|
|
|
| 122 |
michael |
6413 |
if (lclock_last == lclock) |
| 123 |
|
|
return buf; |
| 124 |
|
|
|
| 125 |
|
|
lclock_last = lclock; |
| 126 |
adx |
30 |
gm = gmtime(&lclock); |
| 127 |
|
|
memcpy(&gmbuf, gm, sizeof(gmbuf)); |
| 128 |
michael |
2916 |
gm = &gmbuf; |
| 129 |
adx |
30 |
lt = localtime(&lclock); |
| 130 |
michael |
2916 |
|
| 131 |
michael |
1233 |
snprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d", |
| 132 |
|
|
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, |
| 133 |
|
|
lt->tm_hour, lt->tm_min); |
| 134 |
adx |
30 |
|
| 135 |
|
|
return buf; |
| 136 |
|
|
} |
| 137 |
|
|
|
| 138 |
michael |
2970 |
/* |
| 139 |
|
|
* myctime - This is like standard ctime()-function, but it zaps away |
| 140 |
|
|
* the newline from the end of that string. Also, it takes |
| 141 |
|
|
* the time value as parameter, instead of pointer to it. |
| 142 |
|
|
* Note that it is necessary to copy the string to alternate |
| 143 |
|
|
* buffer (who knows how ctime() implements it, maybe it statically |
| 144 |
|
|
* has newline there and never 'refreshes' it -- zapping that |
| 145 |
|
|
* might break things in other places...) |
| 146 |
|
|
* |
| 147 |
|
|
* |
| 148 |
|
|
* Thu Nov 24 18:22:48 1986 |
| 149 |
|
|
*/ |
| 150 |
|
|
const char * |
| 151 |
michael |
6413 |
myctime(time_t lclock) |
| 152 |
michael |
2970 |
{ |
| 153 |
|
|
static char buf[32]; |
| 154 |
|
|
char *p; |
| 155 |
michael |
6413 |
static time_t lclock_last; |
| 156 |
michael |
2970 |
|
| 157 |
michael |
6413 |
if (lclock_last == lclock) |
| 158 |
|
|
return buf; |
| 159 |
michael |
2970 |
|
| 160 |
michael |
6413 |
lclock_last = lclock; |
| 161 |
|
|
strlcpy(buf, ctime(&lclock), sizeof(buf)); |
| 162 |
|
|
|
| 163 |
michael |
3244 |
if ((p = strchr(buf, '\n'))) |
| 164 |
michael |
2970 |
*p = '\0'; |
| 165 |
|
|
return buf; |
| 166 |
|
|
} |
| 167 |
|
|
|
| 168 |
adx |
30 |
#ifdef HAVE_LIBCRYPTO |
| 169 |
michael |
1847 |
const char * |
| 170 |
michael |
967 |
ssl_get_cipher(const SSL *ssl) |
| 171 |
adx |
30 |
{ |
| 172 |
michael |
3455 |
static char buffer[IRCD_BUFSIZE]; |
| 173 |
michael |
967 |
int bits = 0; |
| 174 |
adx |
30 |
|
| 175 |
|
|
SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits); |
| 176 |
|
|
|
| 177 |
michael |
4914 |
snprintf(buffer, sizeof(buffer), "%s-%s-%d", SSL_get_version(ssl), |
| 178 |
michael |
967 |
SSL_get_cipher(ssl), bits); |
| 179 |
adx |
30 |
return buffer; |
| 180 |
|
|
} |
| 181 |
|
|
#endif |