Postfix3.3.1
conv_time.c
[詳解]
1 /*++
2 /* NAME
3 /* conv_time 3
4 /* SUMMARY
5 /* time value conversion
6 /* SYNOPSIS
7 /* #include <conv_time.h>
8 /*
9 /* int conv_time(strval, timval, def_unit);
10 /* const char *strval;
11 /* int *timval;
12 /* int def_unit;
13 /* DESCRIPTION
14 /* conv_time() converts a numerical time value with optional
15 /* one-letter suffix that specifies an explicit time unit: s
16 /* (seconds), m (minutes), h (hours), d (days) or w (weeks).
17 /* Internally, time is represented in seconds.
18 /*
19 /* Arguments:
20 /* .IP strval
21 /* Input value.
22 /* .IP timval
23 /* Result pointer.
24 /* .IP def_unit
25 /* The default time unit suffix character.
26 /* DIAGNOSTICS
27 /* The result value is non-zero in case of success, zero in
28 /* case of a bad time value or a bad time unit suffix.
29 /* LICENSE
30 /* .ad
31 /* .fi
32 /* The Secure Mailer license must be distributed with this software.
33 /* AUTHOR(S)
34 /* Wietse Venema
35 /* IBM T.J. Watson Research
36 /* P.O. Box 704
37 /* Yorktown Heights, NY 10598, USA
38 /*
39 /* Wietse Venema
40 /* Google, Inc.
41 /* 111 8th Avenue
42 /* New York, NY 10011, USA
43 /*--*/
44 
45 /* System library. */
46 
47 #include <sys_defs.h>
48 #include <limits.h> /* INT_MAX */
49 #include <stdlib.h>
50 #include <errno.h>
51 
52 /* Utility library. */
53 
54 #include <msg.h>
55 
56 /* Global library. */
57 
58 #include <conv_time.h>
59 
60 #define MINUTE (60)
61 #define HOUR (60 * MINUTE)
62 #define DAY (24 * HOUR)
63 #define WEEK (7 * DAY)
64 
65 /* conv_time - convert time value */
66 
67 int conv_time(const char *strval, int *timval, int def_unit)
68 {
69  char *end;
70  int intval;
71  long longval;
72 
73  errno = 0;
74  intval = longval = strtol(strval, &end, 10);
75  if (*strval == 0 || errno == ERANGE || longval != intval || intval < 0
76  /* || (*end != 0 && end[1] != 0) */)
77  return (0);
78 
79  switch (*end ? *end : def_unit) {
80  case 'w':
81  if (intval < INT_MAX / WEEK) {
82  *timval = intval * WEEK;
83  return (1);
84  } else {
85  return (0);
86  }
87  case 'd':
88  if (intval < INT_MAX / DAY) {
89  *timval = intval * DAY;
90  return (1);
91  } else {
92  return (0);
93  }
94  case 'h':
95  if (intval < INT_MAX / HOUR) {
96  *timval = intval * HOUR;
97  return (1);
98  } else {
99  return (0);
100  }
101  case 'm':
102  if (intval < INT_MAX / MINUTE) {
103  *timval = intval * MINUTE;
104  return (1);
105  } else {
106  return (0);
107  }
108  case 's':
109  *timval = intval;
110  return (1);
111  }
112  return (0);
113 }
#define WEEK
Definition: conv_time.c:63
int conv_time(const char *strval, int *timval, int def_unit)
Definition: conv_time.c:67
#define HOUR
Definition: conv_time.c:61
#define DAY
Definition: conv_time.c:62
#define MINUTE
Definition: conv_time.c:60