blob: dbb5dc91733313568541fd771052f80179b30c07 [file] [log] [blame]
/**
* @file common.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief common internal definitions for libyang
*
* Copyright (c) 2018 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _XOPEN_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "tree_schema.h"
void *
ly_realloc(void *ptr, size_t size)
{
void *new_mem;
new_mem = realloc(ptr, size);
if (!new_mem) {
free(ptr);
}
return new_mem;
}
int
lysp_check_date(struct ly_ctx *ctx, const char *date, int date_len)
{
int i;
struct tm tm, tm_;
char *r;
if (date_len != LY_REV_SIZE - 1) {
goto error;
}
/* check format */
for (i = 0; i < date_len; i++) {
if (i == 4 || i == 7) {
if (date[i] != '-') {
goto error;
}
} else if (!isdigit(date[i])) {
goto error;
}
}
/* check content, e.g. 2018-02-31 */
memset(&tm, 0, sizeof tm);
r = strptime(date, "%Y-%m-%d", &tm);
if (!r || r != &date[LY_REV_SIZE - 1]) {
goto error;
}
memcpy(&tm_, &tm, sizeof tm);
mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
/* checking days is enough, since other errors
* have been checked by strptime() */
goto error;
}
return 0;
error:
fprintf(stderr, "Invalid date format \"%*.s\".\n", date_len, date);
return -1;
}