Array of strings sys_dirs must be NULL-terminated
[helenos.git] / uspace / app / date / date.c
blob3d01615687a6d634b4b8cd1b6cd92101d6ca9e9a
1 /*
2 * Copyright (c) 2012 Maurizio Lombardi
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <stdio.h>
30 #include <device/clock_dev.h>
31 #include <errno.h>
32 #include <loc.h>
33 #include <time.h>
34 #include <stdlib.h>
35 #include <getopt.h>
36 #include <ctype.h>
37 #include <str.h>
39 #define NAME "date"
41 static errno_t read_date_from_arg(char *wdate, struct tm *t);
42 static errno_t read_time_from_arg(char *wdate, struct tm *t);
43 static errno_t tm_sanity_check(struct tm *t);
44 static bool is_leap_year(int year);
46 static void usage(void);
48 static int days_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
50 int
51 main(int argc, char **argv)
53 errno_t rc;
54 int c;
55 category_id_t cat_id;
56 size_t svc_cnt;
57 service_id_t *svc_ids = NULL;
58 service_id_t svc_id;
59 char *svc_name = NULL;
60 bool read_only = true;
61 char *wdate = NULL;
62 char *wtime = NULL;
63 struct tm t;
64 int n_args = argc;
66 while ((c = getopt(argc, argv, "hd:t:")) != -1) {
67 switch (c) {
68 case 'h':
69 usage();
70 return 0;
71 case 'd':
72 if (wdate) {
73 usage();
74 return 1;
76 wdate = (char *)optarg;
77 read_only = false;
78 n_args -= 2;
79 break;
80 case 't':
81 if (wtime) {
82 usage();
83 return 1;
85 wtime = (char *)optarg;
86 read_only = false;
87 n_args -= 2;
88 break;
89 case '?':
90 usage();
91 return 1;
95 if (n_args != 1) {
96 printf(NAME ": Unrecognized parameter\n");
97 usage();
98 return 1;
101 /* Get the id of the clock category */
102 rc = loc_category_get_id("clock", &cat_id, IPC_FLAG_BLOCKING);
103 if (rc != EOK) {
104 printf(NAME ": Cannot get clock category id\n");
105 goto exit;
108 /* Get the list of available services in the clock category */
109 rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
110 if (rc != EOK) {
111 printf(NAME ": Cannot get the list of services in the clock "
112 "category\n");
113 goto exit;
116 /* Check if there are available services in the clock category */
117 if (svc_cnt == 0) {
118 printf(NAME ": No available service found in "
119 "the clock category\n");
120 goto exit;
123 /* Get the name of the clock service */
124 rc = loc_service_get_name(svc_ids[0], &svc_name);
125 if (rc != EOK) {
126 printf(NAME ": Cannot get the name of the service\n");
127 goto exit;
130 /* Get the service id for the device */
131 rc = loc_service_get_id(svc_name, &svc_id, 0);
132 if (rc != EOK) {
133 printf(NAME ": Cannot get the service id for device %s",
134 svc_name);
135 goto exit;
138 /* Connect to the device */
139 async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF, 0);
140 if (!sess) {
141 printf(NAME ": Cannot connect to the device\n");
142 goto exit;
145 /* Read the current date/time */
146 rc = clock_dev_time_get(sess, &t);
147 if (rc != EOK) {
148 printf(NAME ": Cannot read the current time\n");
149 goto exit;
152 if (read_only) {
153 /* Print the current time and exit */
154 printf("%02d/%02d/%d ", t.tm_mday,
155 t.tm_mon + 1, 1900 + t.tm_year);
156 printf("%02d:%02d:%02d\n", t.tm_hour, t.tm_min, t.tm_sec);
157 } else {
158 if (wdate) {
159 rc = read_date_from_arg(wdate, &t);
160 if (rc != EOK) {
161 printf(NAME ": error, date format not "
162 "recognized\n");
163 usage();
164 goto exit;
167 if (wtime) {
168 rc = read_time_from_arg(wtime, &t);
169 if (rc != EOK) {
170 printf(NAME ": error, time format not "
171 "recognized\n");
172 usage();
173 goto exit;
177 rc = tm_sanity_check(&t);
178 if (rc != EOK) {
179 printf(NAME ": error, invalid date/time\n");
180 goto exit;
183 rc = clock_dev_time_set(sess, &t);
184 if (rc != EOK) {
185 printf(NAME ": error, Unable to set date/time\n");
186 goto exit;
190 exit:
191 free(svc_name);
192 free(svc_ids);
193 return rc;
196 /** Read the day, month and year from a string
197 * with the following format: DD/MM/YYYY
199 static errno_t
200 read_date_from_arg(char *wdate, struct tm *t)
202 errno_t rc;
203 uint32_t tmp;
205 if (str_size(wdate) != 10) /* str_size("DD/MM/YYYY") == 10 */
206 return EINVAL;
208 if (wdate[2] != '/' ||
209 wdate[5] != '/') {
210 return EINVAL;
213 rc = str_uint32_t(&wdate[0], NULL, 10, false, &tmp);
214 if (rc != EOK)
215 return rc;
217 t->tm_mday = tmp;
219 rc = str_uint32_t(&wdate[3], NULL, 10, false, &tmp);
220 if (rc != EOK)
221 return rc;
223 t->tm_mon = tmp - 1;
225 rc = str_uint32_t(&wdate[6], NULL, 10, false, &tmp);
226 t->tm_year = tmp - 1900;
228 return rc;
231 /** Read the hours, minutes and seconds from a string
232 * with the following format: HH:MM:SS or HH:MM
234 static errno_t
235 read_time_from_arg(char *wtime, struct tm *t)
237 errno_t rc;
238 size_t len = str_size(wtime);
239 bool sec_present = len == 8;
240 uint32_t tmp;
242 /* str_size("HH:MM") == 5 */
243 /* str_size("HH:MM:SS") == 8 */
244 if (len != 8 && len != 5)
245 return EINVAL;
247 if (sec_present && wtime[5] != ':')
248 return EINVAL;
250 if (wtime[2] != ':')
251 return EINVAL;
253 rc = str_uint32_t(&wtime[0], NULL, 10, false, &tmp);
254 if (rc != EOK)
255 return rc;
257 t->tm_hour = tmp;
259 rc = str_uint32_t(&wtime[3], NULL, 10, false, &tmp);
260 if (rc != EOK)
261 return rc;
263 t->tm_min = tmp;
265 if (sec_present) {
266 rc = str_uint32_t(&wtime[6], NULL, 10, false, &tmp);
267 t->tm_sec = tmp;
268 } else
269 t->tm_sec = 0;
271 return rc;
274 /** Check if the tm structure contains valid values
276 * @param t The tm structure to check
278 * @return EOK on success or EINVAL
280 static errno_t
281 tm_sanity_check(struct tm *t)
283 int ndays;
285 if (t->tm_sec < 0 || t->tm_sec > 59)
286 return EINVAL;
287 else if (t->tm_min < 0 || t->tm_min > 59)
288 return EINVAL;
289 else if (t->tm_hour < 0 || t->tm_hour > 23)
290 return EINVAL;
291 else if (t->tm_mday < 1 || t->tm_mday > 31)
292 return EINVAL;
293 else if (t->tm_mon < 0 || t->tm_mon > 11)
294 return EINVAL;
295 else if (t->tm_year < 0 || t->tm_year > 199)
296 return EINVAL;
298 if (t->tm_mon == 1/* FEB */ && is_leap_year(t->tm_year))
299 ndays = 29;
300 else
301 ndays = days_month[t->tm_mon];
303 if (t->tm_mday > ndays)
304 return EINVAL;
306 return EOK;
309 /** Check if a year is a leap year
311 * @param year The year to check
313 * @return true if it is a leap year, false otherwise
315 static bool
316 is_leap_year(int year)
318 bool r = false;
320 if (year % 4 == 0) {
321 if (year % 100 == 0)
322 r = year % 400 == 0;
323 else
324 r = true;
327 return r;
330 static void
331 usage(void)
333 printf("Usage: date [-d DD/MM/YYYY] [-t HH:MM[:SS]]\n");
334 printf(" -d Change the current date\n");
335 printf(" -t Change the current time\n");
336 printf(" -h Display this information\n");