Annotation of embedaddon/sudo/plugins/sudoers/timestr.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 1999, 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com>
        !             3:  *
        !             4:  * Permission to use, copy, modify, and distribute this software for any
        !             5:  * purpose with or without fee is hereby granted, provided that the above
        !             6:  * copyright notice and this permission notice appear in all copies.
        !             7:  *
        !             8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !             9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            15:  */
        !            16: 
        !            17: #include <config.h>
        !            18: 
        !            19: #include <sys/types.h>
        !            20: #include <stdio.h>
        !            21: #ifdef STDC_HEADERS
        !            22: # include <stdlib.h>
        !            23: # include <stddef.h>
        !            24: #else
        !            25: # ifdef HAVE_STDLIB_H
        !            26: #  include <stdlib.h>
        !            27: # endif
        !            28: #endif /* STDC_HEADERS */
        !            29: #include <time.h>
        !            30: 
        !            31: #include "missing.h"
        !            32: 
        !            33: char *get_timestr(time_t, int);
        !            34: 
        !            35: /*
        !            36:  * Return an ascii string with the current date + time
        !            37:  * Uses strftime() if available, else falls back to ctime().
        !            38:  */
        !            39: char *
        !            40: get_timestr(time_t tstamp, int log_year)
        !            41: {
        !            42:     char *s;
        !            43: #ifdef HAVE_STRFTIME
        !            44:     static char buf[128];
        !            45:     struct tm *timeptr;
        !            46: 
        !            47:     timeptr = localtime(&tstamp);
        !            48:     if (log_year)
        !            49:        s = "%h %e %T %Y";
        !            50:     else
        !            51:        s = "%h %e %T";
        !            52: 
        !            53:     /* strftime() does not guarantee to NUL-terminate so we must check. */
        !            54:     buf[sizeof(buf) - 1] = '\0';
        !            55:     if (strftime(buf, sizeof(buf), s, timeptr) && buf[sizeof(buf) - 1] == '\0')
        !            56:        return buf;
        !            57: 
        !            58: #endif /* HAVE_STRFTIME */
        !            59: 
        !            60:     s = ctime(&tstamp) + 4;            /* skip day of the week */
        !            61:     if (log_year)
        !            62:        s[20] = '\0';                   /* avoid the newline */
        !            63:     else
        !            64:        s[15] = '\0';                   /* don't care about year */
        !            65: 
        !            66:     return s;
        !            67: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>