ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/misc.c
Revision: 7330
Committed: Fri Feb 19 17:50:13 2016 UTC (9 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 3332 byte(s)
Log Message:
- Now that we got time_t to work nicely on openbsd with snprintf's conversion specifiers,
  we ran into a similiar issue on Raspbian/ARMv7's time_t which is of signed 32 bit and
  doesn't cope at all with %j. Instead of doing tricks, get rid of time_t everywhere and
  forever and use uintmax_t instead which has at least a 'standardized' conversion specifier
  associated with it.

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 7006 * Copyright (c) 1997-2016 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 1847 const char *
34 michael 7330 date(uintmax_t lclock)
35 adx 30 {
36 michael 6499 static char buf[80];
37 michael 7330 static uintmax_t lclock_last;
38 adx 30
39 michael 2916 if (!lclock)
40 adx 30 lclock = CurrentTime;
41 michael 3244
42 michael 6499 if (lclock_last != lclock)
43 adx 30 {
44 michael 6499 lclock_last = lclock;
45 michael 7330 strftime(buf, sizeof(buf), "%A %B %-e %Y -- %T %z", localtime((time_t *)&lclock));
46 adx 30 }
47    
48     return buf;
49     }
50    
51     const char *
52 michael 7330 date_iso8601(uintmax_t lclock)
53 michael 6415 {
54     static char buf[MAX_DATE_STRING];
55 michael 7330 static uintmax_t lclock_last;
56 michael 6415
57     if (!lclock)
58     lclock = CurrentTime;
59    
60     if (lclock_last != lclock)
61     {
62     lclock_last = lclock;
63 michael 7330 strftime(buf, sizeof(buf), "%FT%T%z", localtime((time_t *)&lclock));
64 michael 6415 }
65    
66     return buf;
67     }
68    
69 michael 2970 /*
70     * myctime - This is like standard ctime()-function, but it zaps away
71     * the newline from the end of that string. Also, it takes
72     * the time value as parameter, instead of pointer to it.
73     * Note that it is necessary to copy the string to alternate
74     * buffer (who knows how ctime() implements it, maybe it statically
75     * has newline there and never 'refreshes' it -- zapping that
76     * might break things in other places...)
77     *
78     *
79     * Thu Nov 24 18:22:48 1986
80     */
81     const char *
82 michael 7330 date_ctime(uintmax_t lclock)
83 michael 2970 {
84 michael 6423 static char buf[MAX_DATE_STRING];
85 michael 7330 static uintmax_t lclock_last;
86 michael 2970
87 michael 6419 if (!lclock)
88     lclock = CurrentTime;
89    
90 michael 6513 if (lclock_last != lclock)
91     {
92     lclock_last = lclock;
93 michael 7330 strftime(buf, sizeof(buf), "%a %b %-e %T %Y", localtime((time_t *)&lclock));
94 michael 6513 }
95 michael 2970
96     return buf;
97     }
98    
99 michael 6569 const char *
100 michael 7330 time_dissect(uintmax_t duration)
101 michael 6569 {
102 michael 6934 static char buf[32]; /* 32 = sizeof("9999999999999999 days, 23:59:59") */
103 michael 6569 unsigned int days = 0, hours = 0, minutes = 0, seconds = 0;
104    
105 michael 6789 while (duration >= 60 * 60 * 24)
106 michael 6569 {
107 michael 6784 duration -= 60 * 60 * 24;
108 michael 6569 ++days;
109     }
110    
111 michael 6789 while (duration >= 60 * 60)
112 michael 6569 {
113 michael 6784 duration -= 60 * 60;
114 michael 6569 ++hours;
115     }
116    
117 michael 6789 while (duration >= 60)
118 michael 6569 {
119 michael 6784 duration -= 60;
120 michael 6569 ++minutes;
121     }
122    
123 michael 6784 seconds = duration;
124 michael 6569
125     snprintf(buf, sizeof(buf), "%u day%s, %02u:%02u:%02u",
126     days, days == 1 ? "" : "s", hours, minutes, seconds);
127     return buf;
128     }
129 michael 7152
130     void
131     binary_to_hex(const unsigned char *bin, char *hex, unsigned int length)
132     {
133     static const char trans[] = "0123456789ABCDEF";
134    
135     for (const unsigned char *const end = bin + length; bin < end; ++bin)
136     {
137     *hex++ = trans[*bin >> 4];
138     *hex++ = trans[*bin & 0xf];
139     }
140    
141     *hex = '\0';
142     }

Properties

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