xref: /PHP-7.2/ext/fileinfo/libmagic/print.c (revision 08d8623d)
1 /*
2  * Copyright (c) Ian F. Darwin 1986-1995.
3  * Software written by Ian F. Darwin and others;
4  * maintained 1995-present by Christos Zoulas and others.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /*
29  * print.c - debugging printout routines
30  */
31 #define _GNU_SOURCE
32 #include "php.h"
33 
34 #include "file.h"
35 
36 #ifndef lint
37 FILE_RCSID("@(#)$File: print.c,v 1.82 2017/02/10 18:14:01 christos Exp $")
38 #endif  /* lint */
39 
40 #include <string.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 #include <time.h>
47 
48 #ifdef PHP_WIN32
49 # define asctime_r php_asctime_r
50 # define ctime_r php_ctime_r
51 #endif
52 
53 #define SZOF(a)	(sizeof(a) / sizeof(a[0]))
54 
55 #include "cdf.h"
56 
57 /*VARARGS*/
58 protected void
file_magwarn(struct magic_set * ms,const char * f,...)59 file_magwarn(struct magic_set *ms, const char *f, ...)
60 {
61 	va_list va;
62 	char *expanded_format = NULL;
63 	int expanded_len;
64 
65 	va_start(va, f);
66 	expanded_len = vasprintf(&expanded_format, f, va);
67 	va_end(va);
68 
69 	if (expanded_len >= 0 && expanded_format) {
70 		php_error_docref(NULL, E_NOTICE, "Warning: %s", expanded_format);
71 
72 		free(expanded_format);
73 	}
74 }
75 
76 protected const char *
file_fmttime(uint64_t v,int flags,char * buf)77 file_fmttime(uint64_t v, int flags, char *buf)
78 {
79 	char *pp;
80 	time_t t = (time_t)v;
81 	struct tm *tm = NULL;
82 
83 	if (flags & FILE_T_WINDOWS) {
84 		struct timeval ts;
85 		cdf_timestamp_to_timespec(&ts, t);
86 		t = ts.tv_sec;
87 	} else {
88 		// XXX: perhaps detect and print something if overflow
89 		// on 32 bit time_t?
90 		t = (time_t)v;
91 	}
92 
93 	if (flags & FILE_T_LOCAL) {
94 		pp = ctime_r(&t, buf);
95 	} else {
96 #ifndef HAVE_DAYLIGHT
97 		private int daylight = 0;
98 #ifdef HAVE_TM_ISDST
99 		private time_t now = (time_t)0;
100 
101 		if (now == (time_t)0) {
102 			struct tm *tm1;
103 			(void)time(&now);
104 			tm1 = localtime(&now);
105 			if (tm1 == NULL)
106 				goto out;
107 			daylight = tm1->tm_isdst;
108 		}
109 #endif /* HAVE_TM_ISDST */
110 #endif /* HAVE_DAYLIGHT */
111 		if (daylight)
112 			t += 3600;
113 		tm = gmtime(&t);
114 		if (tm == NULL)
115 			goto out;
116 		pp = asctime_r(tm, buf);
117 	}
118 	if (tm == NULL)
119 		goto out;
120 	pp = asctime_r(tm, buf);
121 
122 	if (pp == NULL)
123 		goto out;
124 	pp[strcspn(pp, "\n")] = '\0';
125 	return pp;
126 out:
127 	return strcpy(buf, "*Invalid time*");
128 }
129