1/* time.c
2   Routines to deal with UUCP time spans.
3
4   Copyright (C) 1991, 1992, 1993 Ian Lance Taylor
5
6   This file is part of the Taylor UUCP package.
7
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License as
10   published by the Free Software Foundation; either version 2 of the
11   License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
21
22   The author of the program may be contacted at ian@airs.com.
23   */
24
25#include "uucp.h"
26
27#if USE_RCS_ID
28const char time_rcsid[] = "$Id: time.c,v 1.22 2002/03/05 19:10:41 ian Rel $";
29#endif
30
31#include <ctype.h>
32
33#if TM_IN_SYS_TIME
34#include <sys/time.h>
35#else
36#include <time.h>
37#endif
38
39#include "uudefs.h"
40#include "uuconf.h"
41
42/* External functions.  */
43#ifndef time
44extern time_t time ();
45#endif
46#ifndef localtime
47extern struct tm *localtime ();
48#endif
49
50/* See if the current time matches a time span.  If it does, return
51   TRUE, set *pival to the value for the matching span, and set
52   *pcretry to the retry for the matching span.  Otherwise return
53   FALSE.  */
54
55boolean
56ftimespan_match (qspan, pival, pcretry)
57     const struct uuconf_timespan *qspan;
58     long *pival;
59     int *pcretry;
60{
61  time_t inow;
62  struct tm *qtm;
63  int itm;
64  const struct uuconf_timespan *q;
65
66  if (qspan == NULL)
67    return FALSE;
68
69  time (&inow);
70  qtm = localtime (&inow);
71
72  /* Get the number of minutes since Sunday for the time.  */
73  itm = qtm->tm_wday * 24 * 60 + qtm->tm_hour * 60 + qtm->tm_min;
74
75  for (q = qspan; q != NULL; q = q->uuconf_qnext)
76    {
77      if (q->uuconf_istart <= itm && itm <= q->uuconf_iend)
78	{
79	  if (pival != NULL)
80	    *pival = q->uuconf_ival;
81	  if (pcretry != NULL)
82	    *pcretry = q->uuconf_cretry;
83	  return TRUE;
84	}
85    }
86
87  return FALSE;
88}
89
90/* Determine the maximum size that may ever be transferred, according
91   to a timesize span.  This returns -1 if there is no limit.  */
92
93long
94cmax_size_ever (qtimesize)
95     const struct uuconf_timespan *qtimesize;
96{
97  long imax;
98  const struct uuconf_timespan *q;
99
100  if (qtimesize == NULL)
101    return -1;
102
103  /* Look through the list of spans.  If there is any gap larger than
104     1 hour, we assume there are no restrictions.  Otherwise we keep
105     track of the largest value we see.  I picked 1 hour arbitrarily,
106     on the theory that a 1 hour span to transfer large files might
107     actually occur, and is probably not an accident.  */
108  if (qtimesize->uuconf_istart >= 60)
109    return -1;
110
111  imax = -1;
112
113  for (q = qtimesize; q != NULL; q = q->uuconf_qnext)
114    {
115      if (q->uuconf_qnext == NULL)
116	{
117	  if (q->uuconf_iend <= 6 * 24 * 60 + 23 * 60)
118	    return -1;
119	}
120      else
121	{
122	  if (q->uuconf_iend + 60 <= q->uuconf_qnext->uuconf_istart)
123	    return -1;
124	}
125
126      if (imax < q->uuconf_ival)
127	imax = q->uuconf_ival;
128    }
129
130  return imax;
131}
132