/[svn]/ircd-hybrid/libio/misc/misc.c
ViewVC logotype

Contents of /ircd-hybrid/libio/misc/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 69 - (show annotations)
Tue Oct 4 16:09:51 2005 UTC (16 years, 3 months ago) by adx
File MIME type: text/x-chdr
File size: 4451 byte(s)
- splitted ircd/libio, all headers connected with libio sources have been
  moved for internal use only. To use libio interface, include "libio.h"
  (which is already done in "stdinc.h")


1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * s_misc.c: Yet another miscellaneous functions file.
4 *
5 * Copyright (C) 2002 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26
27 struct timeval SystemTime;
28
29 static const char *months[] =
30 {
31 "January", "February", "March", "April",
32 "May", "June", "July", "August",
33 "September", "October", "November","December"
34 };
35
36 static const char *weekdays[] =
37 {
38 "Sunday", "Monday", "Tuesday", "Wednesday",
39 "Thursday", "Friday", "Saturday"
40 };
41
42 char *
43 date(time_t lclock)
44 {
45 static char buf[80], plus;
46 struct tm *lt, *gm;
47 struct tm gmbuf;
48 int minswest;
49
50 if (!lclock)
51 lclock = CurrentTime;
52 gm = gmtime(&lclock);
53 memcpy(&gmbuf, gm, sizeof(gmbuf));
54 gm = &gmbuf;
55 lt = localtime(&lclock);
56
57 /*
58 * There is unfortunately no clean portable way to extract time zone
59 * offset information, so do ugly things.
60 */
61 minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
62
63 if (lt->tm_yday != gm->tm_yday)
64 {
65 if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
66 (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
67 minswest -= 24 * 60;
68 else
69 minswest += 24 * 60;
70 }
71
72 plus = (minswest > 0) ? '-' : '+';
73 if (minswest < 0)
74 minswest = -minswest;
75
76 ircsprintf(buf, "%s %s %d %d -- %02u:%02u:%02u %c%02u:%02u",
77 weekdays[lt->tm_wday], months[lt->tm_mon],lt->tm_mday,
78 lt->tm_year + 1900, lt->tm_hour, lt->tm_min, lt->tm_sec,
79 plus, minswest/60, minswest%60);
80 return buf;
81 }
82
83 const char *
84 smalldate(time_t lclock)
85 {
86 static char buf[MAX_DATE_STRING];
87 struct tm *lt, *gm;
88 struct tm gmbuf;
89
90 if (!lclock)
91 lclock = CurrentTime;
92
93 gm = gmtime(&lclock);
94 memcpy(&gmbuf, gm, sizeof(gmbuf));
95 gm = &gmbuf;
96 lt = localtime(&lclock);
97
98 ircsprintf(buf, "%d/%d/%d %02d.%02d",
99 lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
100 lt->tm_hour, lt->tm_min);
101
102 return buf;
103 }
104
105 /* small_file_date()
106 * Make a small YYYYMMDD formatted string suitable for a
107 * dated file stamp.
108 */
109 char *
110 small_file_date(time_t lclock)
111 {
112 static char timebuffer[MAX_DATE_STRING];
113 struct tm *tmptr;
114
115 if (!lclock)
116 time(&lclock);
117
118 tmptr = localtime(&lclock);
119 strftime(timebuffer, MAX_DATE_STRING, "%Y%m%d", tmptr);
120
121 return timebuffer;
122 }
123
124 #ifdef HAVE_LIBCRYPTO
125 char *
126 ssl_get_cipher(SSL *ssl)
127 {
128 static char buffer[128];
129 const char *name = NULL;
130 int bits;
131
132 switch (ssl->session->ssl_version)
133 {
134 case SSL2_VERSION:
135 name = "SSLv2";
136 break;
137
138 case SSL3_VERSION:
139 name = "SSLv3";
140 break;
141
142 case TLS1_VERSION:
143 name = "TLSv1";
144 break;
145
146 default:
147 name = "UNKNOWN";
148 }
149
150 SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits);
151
152 snprintf(buffer, sizeof(buffer), "%s %s-%d",
153 name, SSL_get_cipher(ssl), bits);
154
155 return buffer;
156 }
157 #endif
158
159 void
160 set_time(void)
161 {
162 struct timeval newtime;
163 #ifdef _WIN32
164 FILETIME ft;
165
166 GetSystemTimeAsFileTime(&ft);
167 if (ft.dwLowDateTime < 0xd53e8000)
168 ft.dwHighDateTime--;
169 ft.dwLowDateTime -= 0xd53e8000;
170 ft.dwHighDateTime -= 0x19db1de;
171
172 newtime.tv_sec = (*(uint64_t *) &ft) / 10000000;
173 newtime.tv_usec = (*(uint64_t *) &ft) / 10 % 1000000;
174 #else
175 newtime.tv_sec = 0;
176 newtime.tv_usec = 0;
177 gettimeofday(&newtime, NULL);
178 #endif
179
180 if (newtime.tv_sec < CurrentTime)
181 {
182 ilog(L_CRIT, "System clock is running backwards - (%lu < %lu)",
183 (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
184 set_back_events(CurrentTime - newtime.tv_sec);
185 }
186
187 SystemTime.tv_sec = newtime.tv_sec;
188 SystemTime.tv_usec = newtime.tv_usec;
189 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28