2 * Copyright (c) 1996 by
3 * David Nugent <davidn@blaze.net.au>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, is permitted provided that the following conditions
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
21 * Login period parsing and comparison functions.
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <login_cap.h>
42 { "su", 2, LTM_SUN }, { "mo", 2, LTM_MON }, { "tu", 2, LTM_TUE },
43 { "we", 2, LTM_WED }, { "th", 2, LTM_THU }, { "fr", 2, LTM_FRI },
44 { "sa", 2, LTM_SAT }, { "any",3, LTM_ANY }, { "all",3, LTM_ANY },
45 { "wk", 2, LTM_WK }, { "wd", 2, LTM_WD }, { NULL, 0, 0 }
49 parse_time(char * ptr, u_short * t)
53 for (val = 0; *ptr && isdigit(*ptr); ptr++)
54 val = (u_short)(val * 10 + (*ptr - '0'));
56 *t = (u_short)((val / 100) * 60 + (val % 100));
63 parse_lt(const char * str)
67 memset(&t, 0, sizeof t);
69 if (str && *str && strcmp(str, "Never") != 0 && strcmp(str, "None") != 0) {
75 /* Make local copy and force lowercase to simplify parsing */
76 p = strncpy(buf, str, sizeof buf);
77 buf[sizeof buf - 1] = '\0';
78 for (i = 0; buf[i]; i++)
79 buf[i] = (char)tolower(buf[i]);
84 while (dws[i].dw && strncmp(p, dws[i].dw, dws[i].cn) != 0)
86 if (dws[i].dw == NULL)
88 m.lt_dow |= dws[i].fl;
92 if (m.lt_dow == LTM_NONE) /* No (valid) prefix, assume any */
96 p = parse_time(p, &m.lt_start);
100 p = parse_time(++p, &m.lt_end);
111 in_ltm(const login_time_t * ltm, struct tm * tt, time_t * ends)
116 /* First, examine the day of the week */
117 if ((u_char)(0x01 << tt->tm_wday) & ltm->lt_dow) {
118 /* Convert `current' time to minute of the day */
119 u_short now = (u_short)((tt->tm_hour * 60) + tt->tm_min);
123 if (now >= ltm->lt_start && now < ltm->lt_end) {
126 /* If requested, return ending time for this period */
127 tt->tm_hour = (int)(ltm->lt_end / 60);
128 tt->tm_min = (int)(ltm->lt_end % 60);
139 in_lt(const login_time_t * ltm, time_t * t)
141 return in_ltm(ltm, localtime(t), t);
145 in_ltms(const login_time_t * ltm, struct tm * tm, time_t * t)
149 while (i < LC_MAXTIMES && ltm[i].lt_dow != LTM_NONE) {
150 if (in_ltm(ltm + i, tm, t))
158 in_lts(const login_time_t * ltm, time_t * t)
160 return in_ltms(ltm, localtime(t), t);