Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <Kernel/KernelInfoPage.h>
28#include <Kernel/Syscall.h>
29#include <assert.h>
30#include <errno.h>
31#include <string.h>
32#include <sys/time.h>
33#include <sys/times.h>
34#include <time.h>
35
36extern "C" {
37
38time_t time(time_t* tloc)
39{
40 struct timeval tv;
41 struct timezone tz;
42 if (gettimeofday(&tv, &tz) < 0)
43 return (time_t)-1;
44 if (tloc)
45 *tloc = tv.tv_sec;
46 return tv.tv_sec;
47}
48
49int gettimeofday(struct timeval* __restrict__ tv, void* __restrict__)
50{
51 static volatile KernelInfoPage* kernel_info;
52 if (!kernel_info)
53 kernel_info = (volatile KernelInfoPage*)syscall(SC_get_kernel_info_page);
54
55 for (;;) {
56 auto serial = kernel_info->serial;
57 *tv = const_cast<struct timeval&>(kernel_info->now);
58 if (serial == kernel_info->serial)
59 break;
60 }
61 return 0;
62}
63
64char* ctime(const time_t*)
65{
66 return const_cast<char*>("ctime() not implemented");
67}
68
69static inline bool __is_leap_year(int year)
70{
71 return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0));
72}
73
74static const int __days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
75static const int __seconds_per_day = 60 * 60 * 24;
76
77static void time_to_tm(struct tm* tm, time_t t)
78{
79 int days = t / __seconds_per_day;
80 int remaining = t % __seconds_per_day;
81 tm->tm_sec = remaining % 60;
82 remaining /= 60;
83 tm->tm_min = remaining % 60;
84 tm->tm_hour = remaining / 60;
85 tm->tm_wday = (4 + days) % 7;
86 int year;
87 for (year = 1970; days >= 365 + __is_leap_year(year); ++year)
88 days -= 365 + __is_leap_year(year);
89 tm->tm_year = year - 1900;
90 tm->tm_yday = days;
91 tm->tm_mday = 1;
92 if (__is_leap_year(year) && days == 59)
93 ++tm->tm_mday;
94 if (__is_leap_year(year) && days >= 59)
95 --days;
96 int month;
97 for (month = 0; month < 11 && days >= __days_per_month[month]; ++month)
98 days -= __days_per_month[month];
99 tm->tm_mon = month;
100 tm->tm_mday += days;
101}
102
103time_t mktime(struct tm* tm)
104{
105 int days = 0;
106 int seconds = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
107 for (int year = 70; year < tm->tm_year; ++year)
108 days += 365 + __is_leap_year(1900 + year);
109 tm->tm_yday = tm->tm_mday - 1;
110 for (int month = 0; month < tm->tm_mon; ++month)
111 tm->tm_yday += __days_per_month[month];
112 days += tm->tm_yday;
113 return days * __seconds_per_day + seconds;
114}
115
116struct tm* localtime(const time_t* t)
117{
118 if (!t)
119 return nullptr;
120 static struct tm tm_buf;
121 return localtime_r(t, &tm_buf);
122}
123
124struct tm* gmtime(const time_t* t)
125{
126 // FIXME: This is obviously not correct. What about timezones bro?
127 return localtime(t);
128}
129
130struct tm* gmtime_r(const time_t* t, struct tm* tm)
131{
132 // FIXME: This is obviously not correct. What about timezones bro?
133 return localtime_r(t, tm);
134}
135
136char* asctime(const struct tm*)
137{
138 ASSERT_NOT_REACHED();
139}
140
141size_t strftime(char* destination, size_t, const char*, const struct tm*)
142{
143 // FIXME: Stubbed function to make nasm work. Should be properly implemented
144 strcpy(destination, "strftime_unimplemented");
145 return strlen("strftime_unimplemented");
146}
147
148long timezone;
149long altzone;
150char* tzname[2];
151int daylight;
152
153void tzset()
154{
155 ASSERT_NOT_REACHED();
156}
157
158clock_t clock()
159{
160 struct tms tms;
161 times(&tms);
162 return tms.tms_utime + tms.tms_stime;
163}
164
165int clock_gettime(clockid_t clock_id, struct timespec* ts)
166{
167 int rc = syscall(SC_clock_gettime, clock_id, ts);
168 __RETURN_WITH_ERRNO(rc, rc, -1);
169}
170
171int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep)
172{
173 Syscall::SC_clock_nanosleep_params params { clock_id, flags, requested_sleep, remaining_sleep };
174 int rc = syscall(SC_clock_nanosleep, ¶ms);
175 __RETURN_WITH_ERRNO(rc, rc, -1);
176}
177
178int clock_getres(clockid_t, struct timespec*)
179{
180 ASSERT_NOT_REACHED();
181}
182
183struct tm* localtime_r(const time_t* t, struct tm* tm)
184{
185 time_to_tm(tm, *t);
186 return tm;
187}
188}