Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / global / conv_time.c
blobc2ed508d83678442d43f3bbb593666863003f870
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* conv_time 3
6 /* SUMMARY
7 /* time value conversion
8 /* SYNOPSIS
9 /* #include <conv_time.h>
11 /* int conv_time(strval, timval, def_unit);
12 /* const char *strval;
13 /* int *timval;
14 /* int def_unit;
15 /* DESCRIPTION
16 /* conv_time() converts a numerical time value with optional
17 /* one-letter suffix that specifies an explicit time unit: s
18 /* (seconds), m (minutes), h (hours), d (days) or w (weeks).
19 /* Internally, time is represented in seconds.
21 /* Arguments:
22 /* .IP strval
23 /* Input value.
24 /* .IP timval
25 /* Result pointer.
26 /* .IP def_unit
27 /* The default time unit suffix character.
28 /* DIAGNOSTICS
29 /* The result value is non-zero in case of success, zero in
30 /* case of a bad time value or a bad time unit suffix.
31 /* LICENSE
32 /* .ad
33 /* .fi
34 /* The Secure Mailer license must be distributed with this software.
35 /* AUTHOR(S)
36 /* Wietse Venema
37 /* IBM T.J. Watson Research
38 /* P.O. Box 704
39 /* Yorktown Heights, NY 10598, USA
40 /*--*/
42 /* System library. */
44 #include <sys_defs.h>
45 #include <limits.h> /* INT_MAX */
46 #include <stdio.h> /* sscanf() */
48 /* Utility library. */
50 #include <msg.h>
52 /* Global library. */
54 #include <conv_time.h>
56 #define MINUTE (60)
57 #define HOUR (60 * MINUTE)
58 #define DAY (24 * HOUR)
59 #define WEEK (7 * DAY)
61 /* conv_time - convert time value */
63 int conv_time(const char *strval, int *timval, int def_unit)
65 char unit;
66 char junk;
67 int intval;
69 switch (sscanf(strval, "%d%c%c", &intval, &unit, &junk)) {
70 case 1:
71 unit = def_unit;
72 /* FALLTHROUGH */
73 case 2:
74 if (intval < 0)
75 return (0);
76 switch (unit) {
77 case 'w':
78 if (intval < INT_MAX / WEEK) {
79 *timval = intval * WEEK;
80 return (1);
81 } else {
82 return (0);
84 case 'd':
85 if (intval < INT_MAX / DAY) {
86 *timval = intval * DAY;
87 return (1);
88 } else {
89 return (0);
91 case 'h':
92 if (intval < INT_MAX / HOUR) {
93 *timval = intval * HOUR;
94 return (1);
95 } else {
96 return (0);
98 case 'm':
99 if (intval < INT_MAX / MINUTE) {
100 *timval = intval * MINUTE;
101 return (1);
102 } else {
103 return (0);
105 case 's':
106 *timval = intval;
107 return (1);
110 return (0);