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

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: /*
1.1.1.2   misho      36:  * Return a static buffer with the current date + time.
1.1       misho      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: 
1.1.1.3 ! misho      47:     if ((timeptr = localtime(&tstamp)) != NULL) {
        !            48:        /* strftime() does not guarantee to NUL-terminate so we must check. */
        !            49:        buf[sizeof(buf) - 1] = '\0';
        !            50:        if (strftime(buf, sizeof(buf), log_year ? "%h %e %T %Y" : "%h %e %T",
        !            51:            timeptr) != 0 && buf[sizeof(buf) - 1] == '\0')
        !            52:            return buf;
        !            53:     }
1.1       misho      54: 
                     55: #endif /* HAVE_STRFTIME */
                     56: 
1.1.1.3 ! misho      57:     s = ctime(&tstamp);
        !            58:     if (s != NULL) {
        !            59:        s += 4;                         /* skip day of the week */
        !            60:        if (log_year)
        !            61:            s[20] = '\0';               /* avoid the newline */
        !            62:        else
        !            63:            s[15] = '\0';               /* don't care about year */
        !            64:     }
1.1       misho      65: 
                     66:     return s;
                     67: }

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