/[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 655 - (show annotations)
Fri Jun 9 13:30:21 2006 UTC (15 years, 7 months ago) by michael
File MIME type: text/x-chdr
File size: 9350 byte(s)
- Fixed function naming convention in some places
- Moved win32 uname() to libio

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 #define IN_MISC_C
26 #include "stdinc.h"
27
28 struct timeval SystemTime;
29
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
126 #ifdef _WIN32
127 /* Copyright (C) 2001 Free Software Foundation, Inc.
128 *
129 * Get name and information about current kernel.
130 */
131 int
132 uname(struct utsname *uts)
133 {
134 enum { WinNT, Win95, Win98, WinUnknown };
135 OSVERSIONINFO osver;
136 SYSTEM_INFO sysinfo;
137 DWORD sLength;
138 DWORD os = WinUnknown;
139
140 memset(uts, 0, sizeof(*uts));
141
142 osver.dwOSVersionInfoSize = sizeof (osver);
143 GetVersionEx (&osver);
144 GetSystemInfo (&sysinfo);
145
146 switch (osver.dwPlatformId)
147 {
148 case VER_PLATFORM_WIN32_NT: /* NT, Windows 2000 or Windows XP */
149 if (osver.dwMajorVersion == 4)
150 strcpy (uts->sysname, "Windows NT4x"); /* NT4x */
151 else if (osver.dwMajorVersion <= 3)
152 strcpy (uts->sysname, "Windows NT3x"); /* NT3x */
153 else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion < 1)
154 strcpy (uts->sysname, "Windows 2000"); /* 2k */
155 else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2)
156 strcpy (uts->sysname, "Windows 2003"); /* 2003 */
157 else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1)
158 strcpy (uts->sysname, "Windows XP"); /* XP */
159 os = WinNT;
160 break;
161
162 case VER_PLATFORM_WIN32_WINDOWS: /* Win95, Win98 or WinME */
163 if ((osver.dwMajorVersion > 4) ||
164 ((osver.dwMajorVersion == 4) && (osver.dwMinorVersion > 0)))
165 {
166 if (osver.dwMinorVersion >= 90)
167 strcpy (uts->sysname, "Windows ME"); /* ME */
168 else
169 strcpy (uts->sysname, "Windows 98"); /* 98 */
170 os = Win98;
171 }
172 else
173 {
174 strcpy (uts->sysname, "Windows 95"); /* 95 */
175 os = Win95;
176 }
177 break;
178
179 case VER_PLATFORM_WIN32s: /* Windows 3.x */
180 strcpy (uts->sysname, "Windows");
181 break;
182 }
183
184 sprintf (uts->version, "%ld.%02ld",
185 osver.dwMajorVersion, osver.dwMinorVersion);
186
187 if (osver.szCSDVersion[0] != '\0' &&
188 (strlen (osver.szCSDVersion) + strlen (uts->version) + 1) <
189 sizeof (uts->version))
190 {
191 strcat (uts->version, " ");
192 strcat (uts->version, osver.szCSDVersion);
193 }
194
195 sprintf (uts->release, "build %ld", osver.dwBuildNumber & 0xFFFF);
196
197 switch (sysinfo.wProcessorArchitecture)
198 {
199 case PROCESSOR_ARCHITECTURE_PPC:
200 strcpy (uts->machine, "ppc");
201 break;
202 case PROCESSOR_ARCHITECTURE_ALPHA:
203 strcpy (uts->machine, "alpha");
204 break;
205 case PROCESSOR_ARCHITECTURE_MIPS:
206 strcpy (uts->machine, "mips");
207 break;
208 case PROCESSOR_ARCHITECTURE_INTEL:
209 /*
210 * dwProcessorType is only valid in Win95 and Win98 and WinME
211 * wProcessorLevel is only valid in WinNT
212 */
213 switch (os)
214 {
215 case Win95:
216 case Win98:
217 switch (sysinfo.dwProcessorType)
218 {
219 case PROCESSOR_INTEL_386:
220 case PROCESSOR_INTEL_486:
221 case PROCESSOR_INTEL_PENTIUM:
222 sprintf (uts->machine, "i%ld", sysinfo.dwProcessorType);
223 break;
224 default:
225 strcpy (uts->machine, "i386");
226 break;
227 }
228 break;
229 case WinNT:
230 switch(sysinfo.wProcessorArchitecture)
231 {
232 case PROCESSOR_ARCHITECTURE_INTEL:
233 sprintf (uts->machine, "x86(%d)", sysinfo.wProcessorLevel);
234 break;
235 case PROCESSOR_ARCHITECTURE_IA64:
236 sprintf (uts->machine, "ia64(%d)", sysinfo.wProcessorLevel);
237 break;
238 #ifdef PROCESSOR_ARCHITECTURE_AMD64
239 case PROCESSOR_ARCHITECTURE_AMD64:
240 sprintf (uts->machine, "x86_64(%d)", sysinfo.wProcessorLevel);
241 break;
242 #endif
243 default:
244 sprintf (uts->machine, "unknown(%d)", sysinfo.wProcessorLevel);
245 break;
246 }
247 break;
248 default:
249 strcpy (uts->machine, "unknown");
250 }
251 break;
252 default:
253 strcpy (uts->machine, "unknown");
254 break;
255 }
256
257 sLength = sizeof (uts->nodename) - 1;
258 GetComputerName (uts->nodename, &sLength);
259 return 0;
260 }
261 #endif
262
263
264 #ifdef HAVE_LIBCRYPTO
265 char *
266 ssl_get_cipher(SSL *ssl)
267 {
268 static char buffer[128];
269 const char *name = NULL;
270 int bits;
271
272 switch (ssl->session->ssl_version)
273 {
274 case SSL2_VERSION:
275 name = "SSLv2";
276 break;
277
278 case SSL3_VERSION:
279 name = "SSLv3";
280 break;
281
282 case TLS1_VERSION:
283 name = "TLSv1";
284 break;
285
286 default:
287 name = "UNKNOWN";
288 }
289
290 SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &bits);
291
292 snprintf(buffer, sizeof(buffer), "%s %s-%d",
293 name, SSL_get_cipher(ssl), bits);
294
295 return buffer;
296 }
297 #endif
298
299 void
300 set_time(void)
301 {
302 struct timeval newtime;
303 #ifdef _WIN32
304 FILETIME ft;
305
306 GetSystemTimeAsFileTime(&ft);
307 if (ft.dwLowDateTime < 0xd53e8000)
308 ft.dwHighDateTime--;
309 ft.dwLowDateTime -= 0xd53e8000;
310 ft.dwHighDateTime -= 0x19db1de;
311
312 newtime.tv_sec = (*(uint64_t *) &ft) / 10000000;
313 newtime.tv_usec = (*(uint64_t *) &ft) / 10 % 1000000;
314 #else
315 newtime.tv_sec = 0;
316 newtime.tv_usec = 0;
317 gettimeofday(&newtime, NULL);
318 #endif
319
320 if (newtime.tv_sec < CurrentTime)
321 {
322 ilog(L_CRIT, "System clock is running backwards - (%lu < %lu)",
323 (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
324 set_back_events(CurrentTime - newtime.tv_sec);
325 }
326
327 SystemTime.tv_sec = newtime.tv_sec;
328 SystemTime.tv_usec = newtime.tv_usec;
329 }
330
331 /* setup_corefile()
332 *
333 * inputs - nothing
334 * output - nothing
335 * side effects - setups corefile to system limits.
336 * -kre
337 */
338 void
339 setup_corefile(void)
340 {
341 #ifdef HAVE_SYS_RESOURCE_H
342 struct rlimit rlim; /* resource limits */
343
344 /* Set corefilesize to maximum */
345 if (!getrlimit(RLIMIT_CORE, &rlim))
346 {
347 rlim.rlim_cur = rlim.rlim_max;
348 setrlimit(RLIMIT_CORE, &rlim);
349 }
350 #endif
351 }
352
353 void
354 libio_init(int daemonn)
355 {
356 #ifndef _WIN32
357 if (daemonn)
358 close_standard_fds();
359 #endif
360
361 /* It ain't random, but it ought to be a little harder to guess */
362 srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
363
364 set_time();
365 eventInit();
366 fdlist_init();
367 init_comm();
368 #ifndef NOBALLOC
369 initBlockHeap();
370 #endif
371 init_dlink_nodes();
372 dbuf_init();
373 #ifndef _WIN32
374 init_resolver();
375 #endif
376 }

Properties

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

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