1
/* Generic support functions for Xcftools
3
* This file was written by Henning Makholm <henning@makholm.net>
4
* It is hereby in the public domain.
6
* In jurisdictions that do not recognise grants of copyright to the
7
* public domain: I, the author and (presumably, in those jurisdictions)
8
* copyright holder, hereby permit anyone to distribute and use this code,
9
* in source code or binary form, with or without modifications. This
10
* permission is world-wide and irrevocable.
12
* Of course, I will not be liable for any errors or shortcomings in the
13
* code, since I give it away without asking any compenstations.
15
* If you use or distribute this code, I would appreciate receiving
16
* credit for writing it, in whichever way you find proper and customary.
25
const char *progname = "$0" ;
29
static void __ATTRIBUTE__((noreturn))
30
vFatalGeneric(int status,const char *format,va_list args)
33
if( *format == '!' ) {
34
vfprintf(stderr,format+1,args);
35
fprintf(stderr,": %s\n",strerror(errno));
37
vfprintf(stderr,format,args);
45
FatalGeneric(int status,const char* format,...)
47
va_list v; va_start(v,format);
48
if( format ) fprintf(stderr,"%s: ",progname);
49
vFatalGeneric(status,format,v);
53
FatalUnexpected(const char* format,...)
55
va_list v; va_start(v,format);
56
fprintf(stderr,"%s: ",progname);
57
vFatalGeneric(127,format,v) ;
61
FatalBadXCF(const char* format,...)
63
va_list v; va_start(v,format);
64
fprintf(stderr,"%s: %s:\n ",progname,_("Corrupted or malformed XCF file"));
65
vFatalGeneric(125,format,v) ;
69
xcfCheckspace(uint32_t addr,int spaceafter,const char *format,...)
71
if( xcf_length < spaceafter || addr > xcf_length - spaceafter ) {
72
va_list v; va_start(v,format);
73
fprintf(stderr,"%s: %s\n ",progname,_("Corrupted or truncated XCF file"));
74
fprintf(stderr,"(0x%" PRIXPTR " bytes): ",(uintptr_t)xcf_length);
75
vFatalGeneric(125,format,v) ;
81
FatalUnsupportedXCF(const char* format,...)
83
va_list v; va_start(v,format);
84
fprintf(stderr,"%s: %s\n ",progname,
85
_("The image contains features not understood by this program:"));
86
vFatalGeneric(123,format,v) ;
92
fprintf(stderr,PACKAGE_STRING "\n");
94
_("Type \"%s -h\" to get an option summary.\n"),progname);
98
/* ******************************************************* */
101
xcfmalloc(size_t size)
103
void *ptr = malloc(size);
105
FatalUnexpected(_("Out of memory"));
113
(uint8_t*)block >= xcf_file &&
114
(uint8_t*)block < xcf_file + xcf_length )
120
/* ******************************************************* */
123
openout(const char *name)
126
if( strcmp(name,"-") == 0 )
128
newfile = fopen(name,"wb") ;
129
if( newfile == NULL )
130
FatalUnexpected(_("!Cannot create file %s"),name);
135
closeout(FILE *f,const char *name)
139
if( fflush(f) == 0 ) {
144
} else if( errno == 0 ) {
145
/* Attempt to coax a valid errno out of the standard library,
146
* following an idea by Bruno Haible
147
* http://lists.gnu.org/archive/html/bug-gnulib/2003-09/msg00157.html
149
if( fputc('\0', f) != EOF &&
151
errno = EIO ; /* Argh, everything succeds. Just call it an I/O error */
154
FatalUnexpected(_("!Error writing file %s"),name);