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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 68 - (hide annotations)
Tue Oct 4 00:44:29 2005 UTC (16 years, 3 months ago) by adx
File MIME type: text/x-chdr
File size: 4578 byte(s)
- removed last references to the ircd core, libio is independent now
- to do: split headers

1 adx 30 /*
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 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26     #include "s_misc.h"
27     #include "irc_string.h"
28     #include "sprintf_irc.h"
29     #include "memory.h"
30 adx 68 #include "s_log.h"
31     #include "event.h"
32 adx 30
33 adx 65 struct timeval SystemTime;
34 adx 30
35     static const char *months[] =
36     {
37     "January", "February", "March", "April",
38     "May", "June", "July", "August",
39     "September", "October", "November","December"
40     };
41    
42     static const char *weekdays[] =
43     {
44     "Sunday", "Monday", "Tuesday", "Wednesday",
45     "Thursday", "Friday", "Saturday"
46     };
47    
48     char *
49     date(time_t lclock)
50     {
51     static char buf[80], plus;
52     struct tm *lt, *gm;
53     struct tm gmbuf;
54     int minswest;
55    
56     if (!lclock)
57     lclock = CurrentTime;
58     gm = gmtime(&lclock);
59     memcpy(&gmbuf, gm, sizeof(gmbuf));
60     gm = &gmbuf;
61     lt = localtime(&lclock);
62    
63     /*
64     * There is unfortunately no clean portable way to extract time zone
65     * offset information, so do ugly things.
66     */
67     minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
68    
69     if (lt->tm_yday != gm->tm_yday)
70     {
71     if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
72     (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
73     minswest -= 24 * 60;
74     else
75     minswest += 24 * 60;
76     }
77    
78     plus = (minswest > 0) ? '-' : '+';
79     if (minswest < 0)
80     minswest = -minswest;
81    
82     ircsprintf(buf, "%s %s %d %d -- %02u:%02u:%02u %c%02u:%02u",
83     weekdays[lt->tm_wday], months[lt->tm_mon],lt->tm_mday,
84     lt->tm_year + 1900, lt->tm_hour, lt->tm_min, lt->tm_sec,
85     plus, minswest/60, minswest%60);
86     return buf;
87     }
88    
89     const char *
90     smalldate(time_t lclock)
91     {
92     static char buf[MAX_DATE_STRING];
93     struct tm *lt, *gm;
94     struct tm gmbuf;
95    
96     if (!lclock)
97     lclock = CurrentTime;
98    
99     gm = gmtime(&lclock);
100     memcpy(&gmbuf, gm, sizeof(gmbuf));
101     gm = &gmbuf;
102     lt = localtime(&lclock);
103    
104     ircsprintf(buf, "%d/%d/%d %02d.%02d",
105     lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
106     lt->tm_hour, lt->tm_min);
107    
108     return buf;
109     }
110    
111     /* small_file_date()
112     * Make a small YYYYMMDD formatted string suitable for a
113     * dated file stamp.
114     */
115     char *
116     small_file_date(time_t lclock)
117     {
118     static char timebuffer[MAX_DATE_STRING];
119     struct tm *tmptr;
120    
121     if (!lclock)
122     time(&lclock);
123    
124     tmptr = localtime(&lclock);
125     strftime(timebuffer, MAX_DATE_STRING, "%Y%m%d", tmptr);
126    
127     return timebuffer;
128     }
129    
130     #ifdef HAVE_LIBCRYPTO
131     char *
132     ssl_get_cipher(SSL *ssl)
133     {
134     static char buffer[128];
135     const char *name = NULL;
136     int bits;
137    
138     switch (ssl->session->ssl_version)
139     {
140     case SSL2_VERSION:
141     name = "SSLv2";
142     break;
143    
144     case SSL3_VERSION:
145     name = "SSLv3";
146     break;
147    
148     case TLS1_VERSION:
149     name = "TLSv1";
150     break;
151    
152     default:
153     name = "UNKNOWN";
154     }
155    
156     SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits);
157    
158     snprintf(buffer, sizeof(buffer), "%s %s-%d",
159     name, SSL_get_cipher(ssl), bits);
160    
161     return buffer;
162     }
163     #endif
164 adx 68
165     void
166     set_time(void)
167     {
168     struct timeval newtime;
169     #ifdef _WIN32
170     FILETIME ft;
171    
172     GetSystemTimeAsFileTime(&ft);
173     if (ft.dwLowDateTime < 0xd53e8000)
174     ft.dwHighDateTime--;
175     ft.dwLowDateTime -= 0xd53e8000;
176     ft.dwHighDateTime -= 0x19db1de;
177    
178     newtime.tv_sec = (*(uint64_t *) &ft) / 10000000;
179     newtime.tv_usec = (*(uint64_t *) &ft) / 10 % 1000000;
180     #else
181     newtime.tv_sec = 0;
182     newtime.tv_usec = 0;
183     gettimeofday(&newtime, NULL);
184     #endif
185    
186     if (newtime.tv_sec < CurrentTime)
187     {
188     ilog(L_CRIT, "System clock is running backwards - (%lu < %lu)",
189     (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
190     set_back_events(CurrentTime - newtime.tv_sec);
191     }
192    
193     SystemTime.tv_sec = newtime.tv_sec;
194     SystemTime.tv_usec = newtime.tv_usec;
195     }

Properties

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

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