aboutsummaryrefslogtreecommitdiffstats
path: root/src/sleep-until.c
blob: 6b21cbae28389ee35fa8326979f6b09010c826b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * sleep-until – Sleeps until a specified time
 * 
 * Copyright © 2015  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <sys/timerfd.h>
#include <string.h>
#include <stdlib.h>



int main(int argc, char* argv[])
{
  char* argv0;
  char float_part[10];
  struct itimerspec value;
  struct itimerspec largest_value;
  int i;
  
  if (argc < 2)
    return 0;
  
  argv0 = argv[0];
  argc--, argv--;
  
  memset(&value, 0, sizeof(value));
  float_part[9] = '\0';
  for (i = 0; i < argc; i++)
    {
      char* p1 = argv[i];
      char* p2 = strchr(p1, '.');
      char excess;
      size_t len;
      if (p2)
	*p2++ = '\0';
      
      value.it_value.tv_sec = (time_t)atoll(p1);
      
      if (p2)
	{
	  len = strlen(p2);
	  memset(float_part, '0', 9 * sizeof(char));
	  excess = len > 9 ? p2[9] : '0';
	  len = len > 9 ? 9 : len;
	  memcpy(float_part + 9 - len, p2, len * sizeof(char));
	  value.it_value.tv_nsec = atol(float_part);
	  if ((excess >= '5') && (value.it_value.tv_nsec++ == 999999999L))
	    value.it_value.tv_nsec = 0, value.it_value.tv_sec++;
	}
      
      if (i == 0)
	largest_value = value;
      else if (value.it_value.tv_sec > largest_value.it_value.tv_sec)
	largest_value = value;
      else if (value.it_value.tv_nsec > largest_value.it_value.tv_nsec)
	largest_value = value;
    }
  
  
  
  return 0;
}