xref: /freebsd/contrib/elftoolchain/libelftc/elftc_timestamp.c (revision 8f1f370da9773a69ab3f79e3efaf3c564eceeb16)
1*b6d812d2SEd Maste /*-
2*b6d812d2SEd Maste  * Copyright (c) 2016 The FreeBSD Foundation
3*b6d812d2SEd Maste  * All rights reserved.
4*b6d812d2SEd Maste  *
5*b6d812d2SEd Maste  * This software was developed by Ed Maste under sponsorship
6*b6d812d2SEd Maste  * of the FreeBSD Foundation.
7*b6d812d2SEd Maste  *
8*b6d812d2SEd Maste  * Redistribution and use in source and binary forms, with or without
9*b6d812d2SEd Maste  * modification, are permitted provided that the following conditions
10*b6d812d2SEd Maste  * are met:
11*b6d812d2SEd Maste  * 1. Redistributions of source code must retain the above copyright
12*b6d812d2SEd Maste  *    notice, this list of conditions and the following disclaimer.
13*b6d812d2SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
14*b6d812d2SEd Maste  *    notice, this list of conditions and the following disclaimer in the
15*b6d812d2SEd Maste  *    documentation and/or other materials provided with the distribution.
16*b6d812d2SEd Maste  *
17*b6d812d2SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*b6d812d2SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*b6d812d2SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*b6d812d2SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*b6d812d2SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*b6d812d2SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*b6d812d2SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*b6d812d2SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*b6d812d2SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*b6d812d2SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*b6d812d2SEd Maste  * SUCH DAMAGE.
28*b6d812d2SEd Maste  */
29*b6d812d2SEd Maste 
30*b6d812d2SEd Maste #include <errno.h>
31*b6d812d2SEd Maste #include <stdlib.h>
32*b6d812d2SEd Maste #include <time.h>
33*b6d812d2SEd Maste #include <libelftc.h>
34*b6d812d2SEd Maste 
35*b6d812d2SEd Maste int
elftc_timestamp(time_t * timestamp)36*b6d812d2SEd Maste elftc_timestamp(time_t *timestamp)
37*b6d812d2SEd Maste {
38*b6d812d2SEd Maste 	long long source_date_epoch;
39*b6d812d2SEd Maste 	char *env, *eptr;
40*b6d812d2SEd Maste 
41*b6d812d2SEd Maste 	if ((env = getenv("SOURCE_DATE_EPOCH")) != NULL) {
42*b6d812d2SEd Maste 		errno = 0;
43*b6d812d2SEd Maste 		source_date_epoch = strtoll(env, &eptr, 10);
44*b6d812d2SEd Maste 		if (*eptr != '\0')
45*b6d812d2SEd Maste 			errno = EINVAL;
46*b6d812d2SEd Maste 		if (source_date_epoch < 0)
47*b6d812d2SEd Maste 			errno = ERANGE;
48*b6d812d2SEd Maste 		if (errno != 0)
49*b6d812d2SEd Maste 			return (-1);
50*b6d812d2SEd Maste 		*timestamp = source_date_epoch;
51*b6d812d2SEd Maste 		return (0);
52*b6d812d2SEd Maste 	}
53*b6d812d2SEd Maste 	*timestamp = time(NULL);
54*b6d812d2SEd Maste 	return (0);
55*b6d812d2SEd Maste }
56