Blame sysdeps/unix/sysv/linux/adjtime.c

Packit 6c4009
/* Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
#include <sys/timex.h>
Packit 6c4009
Packit 6c4009
#define MAX_SEC	(INT_MAX / 1000000L - 2)
Packit 6c4009
#define MIN_SEC	(INT_MIN / 1000000L + 2)
Packit 6c4009
Packit 6c4009
#ifndef MOD_OFFSET
Packit 6c4009
#define modes mode
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef TIMEVAL
Packit 6c4009
#define TIMEVAL timeval
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef TIMEX
Packit 6c4009
#define TIMEX timex
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef ADJTIME
Packit 6c4009
#define ADJTIME __adjtime
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef ADJTIMEX
Packit 6c4009
#define NO_LOCAL_ADJTIME
Packit 6c4009
#define ADJTIMEX(x) __adjtimex (x)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef LINKAGE
Packit 6c4009
#define LINKAGE
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
LINKAGE int
Packit 6c4009
ADJTIME (const struct TIMEVAL *itv, struct TIMEVAL *otv)
Packit 6c4009
{
Packit 6c4009
  struct TIMEX tntx;
Packit 6c4009
Packit 6c4009
  if (itv)
Packit 6c4009
    {
Packit 6c4009
      struct TIMEVAL tmp;
Packit 6c4009
Packit 6c4009
      /* We will do some check here. */
Packit 6c4009
      tmp.tv_sec = itv->tv_sec + itv->tv_usec / 1000000L;
Packit 6c4009
      tmp.tv_usec = itv->tv_usec % 1000000L;
Packit 6c4009
      if (tmp.tv_sec > MAX_SEC || tmp.tv_sec < MIN_SEC)
Packit 6c4009
	return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
Packit 6c4009
      tntx.offset = tmp.tv_usec + tmp.tv_sec * 1000000L;
Packit 6c4009
      tntx.modes = ADJ_OFFSET_SINGLESHOT;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    tntx.modes = ADJ_OFFSET_SS_READ;
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (ADJTIMEX (&tntx) < 0))
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  if (otv)
Packit 6c4009
    {
Packit 6c4009
      if (tntx.offset < 0)
Packit 6c4009
	{
Packit 6c4009
	  otv->tv_usec = -(-tntx.offset % 1000000);
Packit 6c4009
	  otv->tv_sec  = -(-tntx.offset / 1000000);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  otv->tv_usec = tntx.offset % 1000000;
Packit 6c4009
	  otv->tv_sec  = tntx.offset / 1000000;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#ifdef NO_LOCAL_ADJTIME
Packit 6c4009
weak_alias (__adjtime, adjtime)
Packit 6c4009
#endif