xref: /PHP-5.4/ext/fileinfo/libmagic/print.c (revision cdc1a637)
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 
32 #define _GNU_SOURCE
33 #include "php.h"
34 
35 #include "file.h"
36 #include "cdf.h"
37 
38 #ifndef lint
39 FILE_RCSID("@(#)$File: print.c,v 1.76 2013/02/26 18:25:00 christos Exp $")
40 #endif  /* lint */
41 
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #include <time.h>
50 
51 #ifdef PHP_WIN32
52 # define asctime_r php_asctime_r
53 # define ctime_r php_ctime_r
54 #endif
55 
56 #define SZOF(a)	(sizeof(a) / sizeof(a[0]))
57 
58 /*VARARGS*/
59 protected void
file_magwarn(struct magic_set * ms,const char * f,...)60 file_magwarn(struct magic_set *ms, const char *f, ...)
61 {
62 	va_list va;
63 	char *expanded_format;
64 	TSRMLS_FETCH();
65 
66 	va_start(va, f);
67 	if (vasprintf(&expanded_format, f, va)); /* silence */
68 	va_end(va);
69 
70 	php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Warning: %s", expanded_format);
71 
72 	free(expanded_format);
73 }
74 
75 protected const char *
file_fmttime(uint64_t v,int flags,char * buf)76 file_fmttime(uint64_t v, int flags, char *buf)
77 {
78 	char *pp;
79 	time_t t = (time_t)v;
80 	struct tm *tm;
81 
82 	if (flags & FILE_T_WINDOWS) {
83 		struct timeval ts;
84 		cdf_timestamp_to_timespec(&ts, t);
85 		t = ts.tv_sec;
86 	}
87 
88 	if (flags & FILE_T_LOCAL) {
89 		pp = ctime_r(&t, buf);
90 	} else {
91 #ifndef HAVE_DAYLIGHT
92 		private int daylight = 0;
93 #ifdef HAVE_TM_ISDST
94 		private time_t now = (time_t)0;
95 
96 		if (now == (time_t)0) {
97 			struct tm *tm1;
98 			(void)time(&now);
99 			tm1 = localtime(&now);
100 			if (tm1 == NULL)
101 				goto out;
102 			daylight = tm1->tm_isdst;
103 		}
104 #endif /* HAVE_TM_ISDST */
105 #endif /* HAVE_DAYLIGHT */
106 		if (daylight)
107 			t += 3600;
108 		tm = gmtime(&t);
109 		if (tm == NULL)
110 			goto out;
111 		pp = asctime_r(tm, buf);
112 	}
113 
114 	if (pp == NULL)
115 		goto out;
116 	pp[strcspn(pp, "\n")] = '\0';
117 	return pp;
118 out:
119 	return strcpy(buf, "*Invalid time*");
120 }
121