1 /*        $NetBSD: conv_time.c,v 1.3 2025/02/25 19:15:45 christos Exp $         */
2 
3 /*++
4 /* NAME
5 /*        conv_time 3
6 /* SUMMARY
7 /*        time value conversion
8 /* SYNOPSIS
9 /*        #include <conv_time.h>
10 /*
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.
20 /*
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 /*
41 /*        Wietse Venema
42 /*        Google, Inc.
43 /*        111 8th Avenue
44 /*        New York, NY 10011, USA
45 /*--*/
46 
47 /* System library. */
48 
49 #include <sys_defs.h>
50 #include <limits.h>                     /* INT_MAX */
51 #include <stdlib.h>
52 #include <errno.h>
53 
54 /* Utility library. */
55 
56 #include <msg.h>
57 
58 /* Global library. */
59 
60 #include <conv_time.h>
61 
62 #define MINUTE      (60)
63 #define HOUR        (60 * MINUTE)
64 #define DAY         (24 * HOUR)
65 #define WEEK        (7 * DAY)
66 
67 /* conv_time - convert time value */
68 
conv_time(const char * strval,int * timval,int def_unit)69 int     conv_time(const char *strval, int *timval, int def_unit)
70 {
71     char   *end;
72     int     intval;
73     long    longval;
74 
75     errno = 0;
76     intval = longval = strtol(strval, &end, 10);
77     if (*strval == 0 || errno == ERANGE || longval != intval || intval < 0
78            /* || (*end != 0 && end[1] != 0) */ )
79           return (0);
80 
81     switch (*end ? *end : def_unit) {
82     case 'w':
83           if (intval < INT_MAX / WEEK) {
84               *timval = intval * WEEK;
85               return (1);
86           } else {
87               return (0);
88           }
89     case 'd':
90           if (intval < INT_MAX / DAY) {
91               *timval = intval * DAY;
92               return (1);
93           } else {
94               return (0);
95           }
96     case 'h':
97           if (intval < INT_MAX / HOUR) {
98               *timval = intval * HOUR;
99               return (1);
100           } else {
101               return (0);
102           }
103     case 'm':
104           if (intval < INT_MAX / MINUTE) {
105               *timval = intval * MINUTE;
106               return (1);
107           } else {
108               return (0);
109           }
110     case 's':
111           *timval = intval;
112           return (1);
113     }
114     return (0);
115 }
116