summaryrefslogtreecommitdiffabout
path: root/src/interval.c
blob: 247df05d2e157da6c4a006dd1208fd8848877d98 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2009, 2010 Sergey Poznyakoff

   Wydawca is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Wydawca 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with wydawca. If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "wydawca.h"

static int
time_multiplier (const char *str, unsigned *m, unsigned *plen)
{
  static struct timetab
  {
    char *name;
    unsigned mul;
  } tab[] = {
    { "seconds", 1 },
    { "minutes", 60 },
    { "hours", 60*60 },
    { "days",  24*60*60 },
    { "weeks", 7*24*60*60 },
    { "months", 31*7*24*60*60 },
    { NULL }
  };
  struct timetab *p;
  int slen;

  for (slen = 0; str[slen]; slen++)
    if (isspace (str[slen]))
      break;

  for (p = tab; p->name; p++)
    {
      if (p->name[0] == tolower (str[0]))
	{
	  int nlen = strlen (p->name);

	  if (nlen > slen)
	    nlen = slen;

	  if (strncasecmp (p->name, str, nlen) == 0)
	    {
	      *m = p->mul;
	      if (plen)
		*plen = nlen;
	      return 0;
	    }
	}
    }
  return 1;
}

int
parse_time_interval (const char *str, time_t *pint, const char **endp)
{
  int rc = 0;
  time_t interval = 0;

  while (*str)
    {
      char *p;
      unsigned long n;
      unsigned mul, len;

      while (*str && isspace (*str))
	str++;

      if (!isdigit (*str) && time_multiplier (str, &mul, &len) == 0)
	{
	  n = 1;
	  str += len;
	}
      else
	{
	  n = strtoul (str, &p, 10);
	  if (*p && !isspace (*p))
	    {
	      str = p;
	      rc = 1;
	      break;
	    }

	  while (*p && isspace (*p))
	    p++;

	  str = p;
	  if (*str)
	    {
	      if (rc = time_multiplier (str, &mul, &len))
		break;
	      str += len;
	    }
	  else
	    mul = 1;
	}
      interval += n * mul;
    }

  if (rc && endp)
    *endp = str;
  *pint = interval;
  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.