summaryrefslogtreecommitdiffstats
path: root/utils.c
blob: b43f4a2c15e995f71a332c443e2a18fbbb05eea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Generic support functions for Xcftools
 *
 * This file was written by Henning Makholm <henning@makholm.net>
 * It is hereby in the public domain.
 * 
 * In jurisdictions that do not recognise grants of copyright to the
 * public domain: I, the author and (presumably, in those jurisdictions)
 * copyright holder, hereby permit anyone to distribute and use this code,
 * in source code or binary form, with or without modifications. This
 * permission is world-wide and irrevocable.
 *
 * Of course, I will not be liable for any errors or shortcomings in the
 * code, since I give it away without asking any compenstations.
 *
 * If you use or distribute this code, I would appreciate receiving
 * credit for writing it, in whichever way you find proper and customary.
 */

#include "xcftools.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>

const char *progname = "$0" ;
int verboseFlag = 0 ;


static void  __ATTRIBUTE__((noreturn))
vFatalGeneric(int status,const char *format,va_list args)
{
  if( format ) {
    if( *format == '!' ) {
      vfprintf(stderr,format+1,args);
      fprintf(stderr,": %s\n",strerror(errno));
    } else {
      vfprintf(stderr,format,args);
      fputc('\n',stderr);
    }
  }
  exit(status);
}

void
FatalGeneric(int status,const char* format,...)
{
  va_list v; va_start(v,format);
  if( format ) fprintf(stderr,"%s: ",progname);
  vFatalGeneric(status,format,v);
}

void
FatalUnexpected(const char* format,...)
{
  va_list v; va_start(v,format);
  fprintf(stderr,"%s: ",progname);
  vFatalGeneric(127,format,v) ;
}

void
FatalBadXCF(const char* format,...)
{
  va_list v; va_start(v,format);
  fprintf(stderr,"%s: %s:\n ",progname,_("Corrupted or malformed XCF file"));
  vFatalGeneric(125,format,v) ;
}

void
xcfCheckspace(uint32_t addr,int spaceafter,const char *format,...)
{
  if( xcf_length < spaceafter || addr > xcf_length - spaceafter ) {
    va_list v; va_start(v,format);
    fprintf(stderr,"%s: %s\n ",progname,_("Corrupted or truncated XCF file"));
    fprintf(stderr,"(0x%" PRIXPTR " bytes): ",(uintptr_t)xcf_length);
    vFatalGeneric(125,format,v) ;
  }
}


void
FatalUnsupportedXCF(const char* format,...)
{
  va_list v; va_start(v,format);
  fprintf(stderr,"%s: %s\n ",progname,
          _("The image contains features not understood by this program:"));
  vFatalGeneric(123,format,v) ;
}

void
gpl_blurb(void)
{
  fprintf(stderr,PACKAGE_STRING "\n");
  fprintf(stderr,
          _("Type \"%s -h\" to get an option summary.\n"),progname);
  exit(1) ;
}

/* ******************************************************* */

void *
xcfmalloc(size_t size)
{
  void *ptr = malloc(size);
  if( !ptr )
    FatalUnexpected(_("Out of memory"));
  return ptr ;
}

void
xcffree(void *block)
{
  if( xcf_file &&
      (uint8_t*)block >= xcf_file &&
      (uint8_t*)block < xcf_file + xcf_length )
    ;
  else
    free(block);
}

/* ******************************************************* */

FILE *
openout(const char *name)
{
  FILE *newfile ;
  if( strcmp(name,"-") == 0 )
    return stdout ;
  newfile = fopen(name,"wb") ;
  if( newfile == NULL )
    FatalUnexpected(_("!Cannot create file %s"),name);
  return newfile ;
}

void
closeout(FILE *f,const char *name)
{
  if( f == NULL )
    return ;
  if( fflush(f) == 0 ) {
    errno = 0 ;
    if( !ferror(f) ) {
      if( fclose(f) == 0 )
        return ;
    } else if( errno == 0 ) {
      /* Attempt to coax a valid errno out of the standard library,
       * following an idea by Bruno Haible
       * http://lists.gnu.org/archive/html/bug-gnulib/2003-09/msg00157.html
       */
      if( fputc('\0', f) != EOF &&
          fflush(f) == 0 )
        errno = EIO ; /* Argh, everything succeds. Just call it an I/O error */
    }
  }
  FatalUnexpected(_("!Error writing file %s"),name);
}