Fix gcc80 -Wbool-operation warnings in fortune(6) and hack(6).
[dragonfly.git] / lib / libutil / login_times.c
blobf0e8ae7cc15a289df86022ebb899d17e8c301f24
1 /*-
2 * Copyright (c) 1996 by
3 * David Nugent <davidn@blaze.net.au>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, is permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. This work was done expressly for inclusion into FreeBSD. Other use
16 * is permitted provided this notation is included.
17 * 4. Absolutely no warranty of function or purpose is made by the authors.
18 * 5. Modifications may be freely made to this file providing the above
19 * conditions are met.
21 * Login period parsing and comparison functions.
23 * $FreeBSD: head/lib/libutil/login_times.c 252376 2013-06-29 15:52:48Z kientzle $
26 #include <sys/types.h>
27 #include <ctype.h>
28 #include <login_cap.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
33 static struct
35 const char *dw;
36 u_char cn;
37 u_char fl;
38 } dws[] =
40 { "su", 2, LTM_SUN }, { "mo", 2, LTM_MON }, { "tu", 2, LTM_TUE },
41 { "we", 2, LTM_WED }, { "th", 2, LTM_THU }, { "fr", 2, LTM_FRI },
42 { "sa", 2, LTM_SAT }, { "any",3, LTM_ANY }, { "all",3, LTM_ANY },
43 { "wk", 2, LTM_WK }, { "wd", 2, LTM_WD }, { NULL, 0, 0 }
46 static char *
47 parse_time(char * ptr, u_short * t)
49 u_short val;
51 for (val = 0; *ptr && isdigit(*ptr); ptr++)
52 val = (u_short)(val * 10 + (*ptr - '0'));
54 *t = (u_short)((val / 100) * 60 + (val % 100));
56 return (ptr);
60 login_time_t
61 parse_lt(const char *str)
63 login_time_t t;
65 memset(&t, 0, sizeof t);
66 t.lt_dow = LTM_NONE;
67 if (str && *str && strcmp(str, "Never") != 0 && strcmp(str, "None") != 0) {
68 int i;
69 login_time_t m = t;
70 char *p;
71 char buf[64];
73 /* Make local copy and force lowercase to simplify parsing */
74 strlcpy(buf, str, sizeof buf);
75 for (i = 0; buf[i]; i++)
76 buf[i] = (char)tolower(buf[i]);
77 p = buf;
79 while (isalpha(*p)) {
81 i = 0;
82 while (dws[i].dw && strncmp(p, dws[i].dw, dws[i].cn) != 0)
83 i++;
84 if (dws[i].dw == NULL)
85 break;
86 m.lt_dow |= dws[i].fl;
87 p += dws[i].cn;
90 if (m.lt_dow == LTM_NONE) /* No (valid) prefix, assume any */
91 m.lt_dow |= LTM_ANY;
93 if (isdigit(*p))
94 p = parse_time(p, &m.lt_start);
95 else
96 m.lt_start = 0;
97 if (*p == '-')
98 p = parse_time(p + 1, &m.lt_end);
99 else
100 m.lt_end = 1440;
102 t = m;
104 return (t);
109 in_ltm(const login_time_t *ltm, struct tm *tt, time_t *ends)
111 int rc = 0;
113 if (tt != NULL) {
114 /* First, examine the day of the week */
115 if ((u_char)(0x01 << tt->tm_wday) & ltm->lt_dow) {
116 /* Convert `current' time to minute of the day */
117 u_short now = (u_short)((tt->tm_hour * 60) + tt->tm_min);
119 if (tt->tm_sec > 30)
120 ++now;
121 if (now >= ltm->lt_start && now < ltm->lt_end) {
122 rc = 2;
123 if (ends != NULL) {
124 /* If requested, return ending time for this period */
125 tt->tm_hour = (int)(ltm->lt_end / 60);
126 tt->tm_min = (int)(ltm->lt_end % 60);
127 *ends = mktime(tt);
132 return (rc);
137 in_lt(const login_time_t *ltm, time_t *t)
140 return (in_ltm(ltm, localtime(t), t));
144 in_ltms(const login_time_t *ltm, struct tm *tm, time_t *t)
146 int i = 0;
148 while (i < LC_MAXTIMES && ltm[i].lt_dow != LTM_NONE) {
149 if (in_ltm(ltm + i, tm, t))
150 return (i);
151 i++;
153 return (-1);
157 in_lts(const login_time_t *ltm, time_t *t)
160 return (in_ltms(ltm, localtime(t), t));