6b5e5868e7ebf1aff3a5abd7d0c4ef0e5fbf3648Garrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 2009 The NetBSD Foundation, Inc.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This code is derived from software contributed to The NetBSD Foundation
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * by Brian Ginsbach.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Redistribution and use in source and binary forms, with or without
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * modification, are permitted provided that the following conditions
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * documentation and/or other materials provided with the distribution.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * POSSIBILITY OF SUCH DAMAGE.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * getdate_err is set to one of the following values on error.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1 The DATEMSK environment variable is null or undefined.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 2 The template file cannot be opened for reading.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 3 Failed to get file status information.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 4 Template file is not a regular file.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 5 Encountered an error while reading the template file.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 6 Cannot allocate memory.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 7 Input string does not match any line in the template.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 8 Input string is invalid (for example, February 31) or could not
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * be represented in a time_t.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Note that on Solaris, getdate_err is possibly a function, to account
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * for reentrancy. See the code for getdate_err.c for details.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (((datemsk = getenv("DATEMSK")) == NULL) || *datemsk == '\0') {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* loop through datemsk file */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * The NetBSD implementation supports a rich flexible file format
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * with embedded escapes, etc. We don't need any of that. Solaris
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * just reads the template file and (undocumented!) requires that
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * each line not exceed 512 bytes, using a fixed buffer. We could
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * improve on that, but this may grow the stack unreasonably, so
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * we keep it to the same 512 limit. Some day we can be smarter.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * (Note FreeBSD doesn't even have getdate(), and IMO nobody sane
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * should be using this crufty API. strptime is better.)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore while ((line = fgets(buf, sizeof (buf), fp)) != NULL) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * If the buffer consumed the entire string, then
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * the input line was too long. We just check to
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * see if the 2nd to last byte is set. If it isn't,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * then we hit a null byte first, and the line is
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * short enough.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* initialize tmp with sentinels */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore rtm.tm_sec = rtm.tm_min = rtm.tm_hour = TMSENTINEL;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore rtm.tm_mday = rtm.tm_mon = rtm.tm_year = TMSENTINEL;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore rtm.tm_wday = rtm.tm_yday = rtm.tm_isdst = TMSENTINEL;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (feof(fp) || (rp != NULL && *rp != '\0')) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This implementation does not accept setting the broken-down time
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * to anything other than the localtime(). It is not possible to
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * change the scanned timezone with %Z.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Note IRIX and Solaris accept only the current zone for %Z.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * XXX Is there any implementation that matches the standard?
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * XXX (Or am I reading the standard wrong?)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Note: Neither XPG 6 (POSIX 2004) nor XPG 7 (POSIX 2008)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * requires strptime(3) support for %Z.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Given only a weekday find the first matching weekday starting
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * with the current weekday and moving into the future.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (rtm.tm_wday != TMSENTINEL && rtm.tm_year == TMSENTINEL &&
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore rtm.tm_mon == TMSENTINEL && rtm.tm_mday == TMSENTINEL) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Given only a month (and no year) find the first matching month
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * starting with the current month and moving into the future.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* assume the first of the month */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * XXX This isn't documented! Just observed behavior.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Given the weekday find the first matching weekday
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * starting with the weekday of the first day of the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * the month and moving into the future.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Given no time of day assume the current time of day.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore rtm.tm_min == TMSENTINEL && rtm.tm_sec == TMSENTINEL) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Given an hour and no date, find the first matching hour starting
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * with the current hour and moving into the future
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore rtm.tm_year == TMSENTINEL && rtm.tm_mon == TMSENTINEL &&
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Set to 'sane' values; mktime(3) does funny things otherwise.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * No hours, no minutes, no seconds, no service.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Given only a year the values of month, day of month, day of year,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * week day and is daylight (summer) time are unspecified.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * (Specified on the Solaris man page not POSIX.)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore rtm.tm_mon == TMSENTINEL && rtm.tm_mday == TMSENTINEL) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * XXX More undocumented functionality but observed.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Given the weekday find the first matching weekday
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * starting with the weekday of the first day of the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * month and moving into the future.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore rtm.tm_mday += (rtm.tm_wday - tm.tm_wday + 7) % 7;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Given only the century but no year within, the current year
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * is assumed. (Specified on the Solaris man page not POSIX.)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Warning ugly end case
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This is more work since strptime(3) doesn't "do the right thing".
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (rtm.tm_year != TMSENTINEL && (rtm.tm_year - 1900) >= 0) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * mktime() will normalize all values and also check that the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * value will fit into a time_t.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This is only for POSIX correctness. A date >= 1900 is
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * really ok, but using a time_t limits things.