ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/libio/misc/misc.c
Revision: 87
Committed: Wed Oct 5 20:47:43 2005 UTC (18 years, 6 months ago) by adx
Content type: text/x-csrc
File size: 4887 byte(s)
Log Message:
- integrated close_standard_fds with libio_init

File Contents

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

Properties

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