Changeset f4ebaf3c in mainline for uspace/app/date/date.c


Ignore:
Timestamp:
2012-04-18T00:18:26Z (12 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bfc5577a
Parents:
e5fbe06
Message:

date: Let the user set current date/time

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/date/date.c

    re5fbe06 rf4ebaf3c  
    3535#include <malloc.h>
    3636#include <ipc/clock_ctl.h>
     37#include <getopt.h>
     38#include <ctype.h>
    3739
    3840#define NAME   "date"
     41
     42static int read_date_from_arg(char *wdate, struct tm *t);
     43static int read_time_from_arg(char *wdate, struct tm *t);
     44static int read_num_from_str(char *str, size_t len, int *n);
     45static void usage(void);
    3946
    4047int
    4148main(int argc, char **argv)
    4249{
    43         int rc;
     50        int rc, c;
    4451        category_id_t cat_id;
    45         size_t svc_cnt;
     52        size_t        svc_cnt;
    4653        service_id_t  *svc_ids = NULL;
    47         service_id_t svc_id;
    48         char *svc_name = NULL;
    49 
    50         sysarg_t battery_ok;
    51         struct tm t;
     54        service_id_t  svc_id;
     55        char          *svc_name = NULL;
     56        bool          read_only = true;
     57        char          *wdate = NULL;
     58        char          *wtime = NULL;
     59        sysarg_t      battery_ok;
     60        struct tm     t;
     61
     62        while ((c = getopt(argc, argv, "d:t:")) != -1) {
     63                switch (c) {
     64                case 'd':
     65                        wdate = (char *)optarg;
     66                        read_only = false;
     67                        break;
     68                case 't':
     69                        wtime = (char *)optarg;
     70                        read_only = false;
     71                        break;
     72                case '?':
     73                        usage();
     74                        return 1;
     75                }
     76        }
    5277
    5378        /* Get the id of the clock category */
     
    96121        }
    97122
     123        /* Check the battery status (if present) */
    98124        async_exch_t *exch = async_exchange_begin(sess);
    99125        rc = async_req_0_1(exch, CLOCK_GET_BATTERY_STATUS, &battery_ok);
     
    110136        }
    111137
    112         printf("%02d/%02d/%d ", t.tm_mday, t.tm_mon + 1, 1900 + t.tm_year);
    113         printf("%02d:%02d:%02d\n", t.tm_hour, t.tm_min, t.tm_sec);
     138        if (read_only) {
     139                /* Print the current time and exit */
     140                printf("%02d/%02d/%d ", t.tm_mday,
     141                    t.tm_mon + 1, 1900 + t.tm_year);
     142                printf("%02d:%02d:%02d\n", t.tm_hour, t.tm_min, t.tm_sec);
     143        } else {
     144                if (wdate) {
     145                        rc = read_date_from_arg(wdate, &t);
     146                        if (rc != EOK) {
     147                                printf(NAME ": error, date format not "
     148                                    "recognized\n");
     149                                usage();
     150                                goto exit;
     151                        }
     152                }
     153                if (wtime) {
     154                        rc = read_time_from_arg(wtime, &t);
     155                        if (rc != EOK) {
     156                                printf(NAME ": error, time format not "
     157                                    "recognized\n");
     158                                usage();
     159                                goto exit;
     160                        }
     161                }
     162
     163                rc = clock_dev_time_set(sess, &t);
     164                if (rc != EOK) {
     165                        printf(NAME ": error, Unable to set date/time\n");
     166                        goto exit;
     167                }
     168        }
    114169
    115170exit:
     
    119174}
    120175
     176static int
     177read_date_from_arg(char *wdate, struct tm *t)
     178{
     179        int rc;
     180
     181        if (str_size(wdate) != 10) /* DD/MM/YYYY */
     182                return EINVAL;
     183
     184        if (wdate[2] != '/' ||
     185            wdate[5] != '/') {
     186                return EINVAL;
     187        }
     188
     189        rc = read_num_from_str(&wdate[0], 2, &t->tm_mday);
     190        if (rc != EOK)
     191                return rc;
     192
     193        rc = read_num_from_str(&wdate[3], 2, &t->tm_mon);
     194        if (rc != EOK)
     195                return rc;
     196        t->tm_mon--;
     197
     198        rc = read_num_from_str(&wdate[6], 4, &t->tm_year);
     199        t->tm_year -= 1900;
     200        return rc;
     201}
     202
     203static int
     204read_time_from_arg(char *wtime, struct tm *t)
     205{
     206        int rc;
     207        size_t len = str_size(wtime);
     208        bool sec_present = len > 5;
     209
     210        if (len > 8 || len < 5) /* HH:MM[:SS] */
     211                return EINVAL;
     212
     213        if (sec_present && wtime[5] != ':')
     214                return EINVAL;
     215
     216        if (wtime[2] != ':')
     217                return EINVAL;
     218
     219        rc = read_num_from_str(&wtime[0], 2, &t->tm_hour);
     220        if (rc != EOK)
     221                return rc;
     222
     223        rc = read_num_from_str(&wtime[3], 2, &t->tm_min);
     224        if (rc != EOK)
     225                return rc;
     226
     227        if (sec_present)
     228                rc = read_num_from_str(&wtime[6], 2, &t->tm_sec);
     229        else
     230                t->tm_sec = 0;
     231
     232        return rc;
     233}
     234
     235static int
     236read_num_from_str(char *str, size_t len, int *n)
     237{
     238        size_t i;
     239
     240        *n = 0;
     241
     242        for (i = 0; i < len; ++i, ++str) {
     243                if (!isdigit(*str))
     244                        return EINVAL;
     245                *n *= 10;
     246                *n += *str - '0';
     247        }
     248
     249        return EOK;
     250}
     251
     252static void
     253usage(void)
     254{
     255        printf("Usage: date [-d DD/MM/YYYY] [-t HH:MM[:SS]]\n");
     256        printf("       -d   Change the current date\n");
     257        printf("       -t   Change the current time\n");
     258}
     259
Note: See TracChangeset for help on using the changeset viewer.