1
2 /* $Id$ */
3
4 #include "ssdeep.h"
5 #include <stdarg.h>
6
print_status(const char * fmt,...)7 void print_status(const char *fmt, ...)
8 {
9 va_list(ap);
10
11 va_start(ap,fmt);
12 vprintf(fmt,ap);
13 va_end(ap);
14
15 printf ("%s", NEWLINE);
16 }
17
18
print_error(const state * s,const char * fmt,...)19 void print_error(const state *s, const char *fmt, ...)
20 {
21 if (NULL == s)
22 internal_error("%s: NULL state passed to print_error", __progname);
23
24 if (s->mode & mode_silent)
25 return;
26
27 va_list(ap);
28
29 va_start(ap,fmt);
30 vfprintf(stderr,fmt,ap);
31 va_end(ap);
32
33 fprintf (stderr,"%s", NEWLINE);
34
35 }
36
37 #define MD5DEEP_PRINT_MSG(HANDLE,MSG) \
38 va_list(ap); \
39 va_start(ap,MSG); \
40 if (vfprintf(HANDLE,MSG,ap) < 0) \
41 { \
42 fprintf(stderr, "%s: %s", __progname, strerror(errno)); \
43 exit(EXIT_FAILURE); \
44 } \
45 va_end(ap); fprintf (HANDLE,"%s", NEWLINE);
46
47
print_error_unicode(state * s,const TCHAR * fn,const char * fmt,...)48 void print_error_unicode(state *s, const TCHAR *fn, const char *fmt, ...)
49 {
50 if (NULL == s)
51 internal_error("%s: NULL state passed to print_error_unicode", __progname);
52
53 if (!(s->mode & mode_silent))
54 {
55 display_filename(stderr,fn,FALSE);
56 fprintf(stderr,": ");
57 MD5DEEP_PRINT_MSG(stderr,fmt);
58 }
59 }
60
61
62
63 /* Internal errors are so serious that we ignore the user's wishes
64 about silent mode. Our need to debug the program outweighs their
65 preferences. Besides, the program is probably crashing anyway... */
internal_error(const char * fmt,...)66 void internal_error(const char *fmt, ... )
67 {
68 MD5DEEP_PRINT_MSG(stderr,fmt);
69 print_status ("%s: Internal error. Contact developer!", __progname);
70 exit (EXIT_FAILURE);
71 }
72
73
74
fatal_error(const char * fmt,...)75 void fatal_error(const char *fmt, ... )
76 {
77 va_list(ap);
78
79 va_start(ap,fmt);
80 vprintf(fmt,ap);
81 va_end(ap);
82
83 printf ("%s", NEWLINE);
84 exit (EXIT_FAILURE);
85 }
86
87
88 #ifdef _WIN32
display_filename(FILE * out,const TCHAR * fn,int escape_quotes)89 void display_filename(FILE *out, const TCHAR *fn, int escape_quotes)
90 {
91 size_t pos,len;
92
93 if (NULL == fn || NULL == out)
94 return;
95
96 len = _tcslen(fn);
97
98 for (pos = 0 ; pos < len ; ++pos)
99 {
100 // If desired, escape quotation marks. Used for CSV modes
101 if (escape_quotes && ('"' == ((fn[pos] & 0xff00) >> 16)))
102 {
103 fprintf(out,"\\\"");
104 }
105 else
106 {
107 // Windows can only display the English (00) code page
108 // on the command line.
109 if (0 == (fn[pos] & 0xff00))
110 fputc(fn[pos],out);
111 // _ftprintf(out, _TEXT("%c"), fn[pos]);
112 else
113 fputc('?',out);
114 // _ftprintf(out, _TEXT("?"));
115 }
116 }
117 }
118 #else
display_filename(FILE * out,const TCHAR * fn,int escape_quotes)119 void display_filename(FILE *out, const TCHAR *fn, int escape_quotes)
120 {
121 size_t pos, len;
122
123 if (NULL == fn || NULL == out)
124 return;
125
126 len = _tcslen(fn);
127 for (pos = 0 ; pos < len ; ++pos)
128 {
129 if (escape_quotes && '"' == fn[pos])
130 _ftprintf(out, _TEXT("\\\""));
131 else
132 _ftprintf(out, _TEXT("%c"), fn[pos]);
133 }
134 }
135 #endif
136