[cca5c8d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Maurizio Lombardi
|
---|
| 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
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 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.
|
---|
| 16 | *
|
---|
| 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.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | #include <stdio.h>
|
---|
[cf00f2e4] | 31 | #include <device/clock_dev.h>
|
---|
| 32 | #include <errno.h>
|
---|
| 33 | #include <loc.h>
|
---|
[9f1328cd] | 34 | #include <time.h>
|
---|
[f1abd6e] | 35 | #include <malloc.h>
|
---|
[f4ebaf3c] | 36 | #include <getopt.h>
|
---|
| 37 | #include <ctype.h>
|
---|
[cf00f2e4] | 38 |
|
---|
| 39 | #define NAME "date"
|
---|
[cca5c8d] | 40 |
|
---|
[f4ebaf3c] | 41 | static int read_date_from_arg(char *wdate, struct tm *t);
|
---|
| 42 | static int read_time_from_arg(char *wdate, struct tm *t);
|
---|
[fa18523] | 43 | static int tm_sanity_check(struct tm *t);
|
---|
| 44 | static bool is_leap_year(int year);
|
---|
| 45 |
|
---|
[f4ebaf3c] | 46 | static void usage(void);
|
---|
| 47 |
|
---|
[fa18523] | 48 | static int days_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
---|
| 49 |
|
---|
[cca5c8d] | 50 | int
|
---|
| 51 | main(int argc, char **argv)
|
---|
| 52 | {
|
---|
[f4ebaf3c] | 53 | int rc, c;
|
---|
[cf00f2e4] | 54 | category_id_t cat_id;
|
---|
[f4ebaf3c] | 55 | size_t svc_cnt;
|
---|
[cf00f2e4] | 56 | service_id_t *svc_ids = NULL;
|
---|
[f4ebaf3c] | 57 | service_id_t svc_id;
|
---|
| 58 | char *svc_name = NULL;
|
---|
| 59 | bool read_only = true;
|
---|
| 60 | char *wdate = NULL;
|
---|
| 61 | char *wtime = NULL;
|
---|
| 62 | struct tm t;
|
---|
[0885f7e] | 63 | int n_args = argc;
|
---|
[751cabc] | 64 |
|
---|
[e341110] | 65 | while ((c = getopt(argc, argv, "hd:t:")) != -1) {
|
---|
[f4ebaf3c] | 66 | switch (c) {
|
---|
[e341110] | 67 | case 'h':
|
---|
| 68 | usage();
|
---|
| 69 | return 0;
|
---|
[f4ebaf3c] | 70 | case 'd':
|
---|
[0885f7e] | 71 | if (wdate) {
|
---|
| 72 | usage();
|
---|
| 73 | return 1;
|
---|
| 74 | }
|
---|
[f4ebaf3c] | 75 | wdate = (char *)optarg;
|
---|
| 76 | read_only = false;
|
---|
[0885f7e] | 77 | n_args -= 2;
|
---|
[f4ebaf3c] | 78 | break;
|
---|
| 79 | case 't':
|
---|
[0885f7e] | 80 | if (wtime) {
|
---|
| 81 | usage();
|
---|
| 82 | return 1;
|
---|
| 83 | }
|
---|
[f4ebaf3c] | 84 | wtime = (char *)optarg;
|
---|
| 85 | read_only = false;
|
---|
[0885f7e] | 86 | n_args -= 2;
|
---|
[f4ebaf3c] | 87 | break;
|
---|
| 88 | case '?':
|
---|
| 89 | usage();
|
---|
| 90 | return 1;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[cf00f2e4] | 93 |
|
---|
[0885f7e] | 94 | if (n_args != 1) {
|
---|
| 95 | printf(NAME ": Unrecognized parameter\n");
|
---|
| 96 | usage();
|
---|
| 97 | return 1;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[cf00f2e4] | 100 | /* Get the id of the clock category */
|
---|
| 101 | rc = loc_category_get_id("clock", &cat_id, IPC_FLAG_BLOCKING);
|
---|
| 102 | if (rc != EOK) {
|
---|
| 103 | printf(NAME ": Cannot get clock category id\n");
|
---|
| 104 | goto exit;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /* Get the list of available services in the clock category */
|
---|
| 108 | rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
|
---|
| 109 | if (rc != EOK) {
|
---|
[ae827d0] | 110 | printf(NAME ": Cannot get the list of services in the clock "
|
---|
| 111 | "category\n");
|
---|
[cf00f2e4] | 112 | goto exit;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[1789023] | 115 | /* Check if there are available services in the clock category */
|
---|
[cf00f2e4] | 116 | if (svc_cnt == 0) {
|
---|
[ae827d0] | 117 | printf(NAME ": No available service found in "
|
---|
| 118 | "the clock category\n");
|
---|
[cf00f2e4] | 119 | goto exit;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /* Get the name of the clock service */
|
---|
| 123 | rc = loc_service_get_name(svc_ids[0], &svc_name);
|
---|
| 124 | if (rc != EOK) {
|
---|
| 125 | printf(NAME ": Cannot get the name of the service\n");
|
---|
| 126 | goto exit;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[8354ca6] | 129 | /* Get the service id for the device */
|
---|
| 130 | rc = loc_service_get_id(svc_name, &svc_id, 0);
|
---|
[9f1328cd] | 131 | if (rc != EOK) {
|
---|
[8354ca6] | 132 | printf(NAME ": Cannot get the service id for device %s",
|
---|
| 133 | svc_name);
|
---|
[9f1328cd] | 134 | goto exit;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[8354ca6] | 137 | /* Connect to the device */
|
---|
| 138 | async_sess_t *sess = loc_service_connect(EXCHANGE_SERIALIZE,
|
---|
| 139 | svc_id, 0);
|
---|
[9f1328cd] | 140 | if (!sess) {
|
---|
| 141 | printf(NAME ": Cannot connect to the device\n");
|
---|
| 142 | goto exit;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[8354ca6] | 145 | /* Read the current date/time */
|
---|
[9f1328cd] | 146 | rc = clock_dev_time_get(sess, &t);
|
---|
| 147 | if (rc != EOK) {
|
---|
| 148 | printf(NAME ": Cannot read the current time\n");
|
---|
| 149 | goto exit;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[f4ebaf3c] | 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;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 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;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[fa18523] | 177 | rc = tm_sanity_check(&t);
|
---|
| 178 | if (rc != EOK) {
|
---|
| 179 | printf(NAME ": error, invalid date/time\n");
|
---|
| 180 | goto exit;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[f4ebaf3c] | 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;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
[d73a56d] | 189 |
|
---|
[cf00f2e4] | 190 | exit:
|
---|
| 191 | free(svc_name);
|
---|
| 192 | free(svc_ids);
|
---|
| 193 | return rc;
|
---|
[cca5c8d] | 194 | }
|
---|
| 195 |
|
---|
[ad2718d] | 196 | /** Read the day, month and year from a string
|
---|
| 197 | * with the following format: DD/MM/YYYY
|
---|
| 198 | */
|
---|
[f4ebaf3c] | 199 | static int
|
---|
| 200 | read_date_from_arg(char *wdate, struct tm *t)
|
---|
| 201 | {
|
---|
| 202 | int rc;
|
---|
[df7f9fb5] | 203 | uint32_t tmp;
|
---|
[f4ebaf3c] | 204 |
|
---|
[ad2718d] | 205 | if (str_size(wdate) != 10) /* str_size("DD/MM/YYYY") == 10 */
|
---|
[f4ebaf3c] | 206 | return EINVAL;
|
---|
| 207 |
|
---|
| 208 | if (wdate[2] != '/' ||
|
---|
| 209 | wdate[5] != '/') {
|
---|
| 210 | return EINVAL;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[df7f9fb5] | 213 | rc = str_uint32_t(&wdate[0], NULL, 10, false, &tmp);
|
---|
[f4ebaf3c] | 214 | if (rc != EOK)
|
---|
| 215 | return rc;
|
---|
| 216 |
|
---|
[df7f9fb5] | 217 | t->tm_mday = tmp;
|
---|
| 218 |
|
---|
| 219 | rc = str_uint32_t(&wdate[3], NULL, 10, false, &tmp);
|
---|
[f4ebaf3c] | 220 | if (rc != EOK)
|
---|
| 221 | return rc;
|
---|
| 222 |
|
---|
[df7f9fb5] | 223 | t->tm_mon = tmp - 1;
|
---|
| 224 |
|
---|
| 225 | rc = str_uint32_t(&wdate[6], NULL, 10, false, &tmp);
|
---|
| 226 | t->tm_year = tmp - 1900;
|
---|
| 227 |
|
---|
[f4ebaf3c] | 228 | return rc;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[ad2718d] | 231 | /** Read the hours, minutes and seconds from a string
|
---|
| 232 | * with the following format: HH:MM:SS or HH:MM
|
---|
| 233 | */
|
---|
[f4ebaf3c] | 234 | static int
|
---|
| 235 | read_time_from_arg(char *wtime, struct tm *t)
|
---|
| 236 | {
|
---|
| 237 | int rc;
|
---|
| 238 | size_t len = str_size(wtime);
|
---|
[ad2718d] | 239 | bool sec_present = len == 8;
|
---|
[df7f9fb5] | 240 | uint32_t tmp;
|
---|
[f4ebaf3c] | 241 |
|
---|
[ad2718d] | 242 | /* str_size("HH:MM") == 5 */
|
---|
| 243 | /* str_size("HH:MM:SS") == 8 */
|
---|
| 244 | if (len != 8 && len != 5)
|
---|
[f4ebaf3c] | 245 | return EINVAL;
|
---|
| 246 |
|
---|
| 247 | if (sec_present && wtime[5] != ':')
|
---|
| 248 | return EINVAL;
|
---|
| 249 |
|
---|
| 250 | if (wtime[2] != ':')
|
---|
| 251 | return EINVAL;
|
---|
| 252 |
|
---|
[df7f9fb5] | 253 | rc = str_uint32_t(&wtime[0], NULL, 10, false, &tmp);
|
---|
[f4ebaf3c] | 254 | if (rc != EOK)
|
---|
| 255 | return rc;
|
---|
| 256 |
|
---|
[df7f9fb5] | 257 | t->tm_hour = tmp;
|
---|
| 258 |
|
---|
| 259 | rc = str_uint32_t(&wtime[3], NULL, 10, false, &tmp);
|
---|
[f4ebaf3c] | 260 | if (rc != EOK)
|
---|
| 261 | return rc;
|
---|
| 262 |
|
---|
[df7f9fb5] | 263 | t->tm_min = tmp;
|
---|
| 264 |
|
---|
| 265 | if (sec_present) {
|
---|
| 266 | rc = str_uint32_t(&wtime[6], NULL, 10, false, &tmp);
|
---|
| 267 | t->tm_sec = tmp;
|
---|
| 268 | } else
|
---|
[f4ebaf3c] | 269 | t->tm_sec = 0;
|
---|
| 270 |
|
---|
| 271 | return rc;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[fa18523] | 274 | /** Check if the tm structure contains valid values
|
---|
| 275 | *
|
---|
| 276 | * @param t The tm structure to check
|
---|
| 277 | *
|
---|
| 278 | * @return EOK on success or EINVAL
|
---|
| 279 | */
|
---|
| 280 | static int
|
---|
| 281 | tm_sanity_check(struct tm *t)
|
---|
| 282 | {
|
---|
| 283 | int ndays;
|
---|
| 284 |
|
---|
| 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;
|
---|
| 297 |
|
---|
| 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];
|
---|
| 302 |
|
---|
| 303 | if (t->tm_mday > ndays)
|
---|
| 304 | return EINVAL;
|
---|
| 305 |
|
---|
| 306 | return EOK;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /** Check if a year is a leap year
|
---|
| 310 | *
|
---|
| 311 | * @param year The year to check
|
---|
| 312 | *
|
---|
| 313 | * @return true if it is a leap year, false otherwise
|
---|
| 314 | */
|
---|
| 315 | static bool
|
---|
| 316 | is_leap_year(int year)
|
---|
| 317 | {
|
---|
| 318 | bool r = false;
|
---|
| 319 |
|
---|
| 320 | if (year % 4 == 0) {
|
---|
| 321 | if (year % 100 == 0)
|
---|
| 322 | r = year % 400 == 0;
|
---|
| 323 | else
|
---|
| 324 | r = true;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | return r;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[f4ebaf3c] | 330 | static void
|
---|
| 331 | usage(void)
|
---|
| 332 | {
|
---|
| 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");
|
---|
[a8b8086] | 336 | printf(" -h Display this information\n");
|
---|
[f4ebaf3c] | 337 | }
|
---|
| 338 |
|
---|